Actual source code: matrix.c

petsc-master 2016-02-05
Report Typos and Errors
  2: /*
  3:    This is where the abstract matrix operations are defined
  4: */

  6: #include <petsc/private/matimpl.h>        /*I "petscmat.h" I*/
  7: #include <petsc/private/vecimpl.h>
  8: #include <petsc/private/isimpl.h>

 10: /* Logging support */
 11: PetscClassId MAT_CLASSID;
 12: PetscClassId MAT_COLORING_CLASSID;
 13: PetscClassId MAT_FDCOLORING_CLASSID;
 14: PetscClassId MAT_TRANSPOSECOLORING_CLASSID;

 16: PetscLogEvent MAT_Mult, MAT_Mults, MAT_MultConstrained, MAT_MultAdd, MAT_MultTranspose;
 17: PetscLogEvent MAT_MultTransposeConstrained, MAT_MultTransposeAdd, MAT_Solve, MAT_Solves, MAT_SolveAdd, MAT_SolveTranspose, MAT_MatSolve;
 18: PetscLogEvent MAT_SolveTransposeAdd, MAT_SOR, MAT_ForwardSolve, MAT_BackwardSolve, MAT_LUFactor, MAT_LUFactorSymbolic;
 19: PetscLogEvent MAT_LUFactorNumeric, MAT_CholeskyFactor, MAT_CholeskyFactorSymbolic, MAT_CholeskyFactorNumeric, MAT_ILUFactor;
 20: PetscLogEvent MAT_ILUFactorSymbolic, MAT_ICCFactorSymbolic, MAT_Copy, MAT_Convert, MAT_Scale, MAT_AssemblyBegin;
 21: PetscLogEvent MAT_AssemblyEnd, MAT_SetValues, MAT_GetValues, MAT_GetRow, MAT_GetRowIJ, MAT_GetSubMatrices, MAT_GetOrdering, MAT_RedundantMat, MAT_GetSeqNonzeroStructure;
 22: PetscLogEvent MAT_IncreaseOverlap, MAT_Partitioning, MAT_Coarsen, MAT_ZeroEntries, MAT_Load, MAT_View, MAT_AXPY, MAT_FDColoringCreate;
 23: PetscLogEvent MAT_FDColoringSetUp, MAT_FDColoringApply,MAT_Transpose,MAT_FDColoringFunction, MAT_GetSubMatrix;
 24: PetscLogEvent MAT_TransposeColoringCreate;
 25: PetscLogEvent MAT_MatMult, MAT_MatMultSymbolic, MAT_MatMultNumeric;
 26: PetscLogEvent MAT_PtAP, MAT_PtAPSymbolic, MAT_PtAPNumeric,MAT_RARt, MAT_RARtSymbolic, MAT_RARtNumeric;
 27: PetscLogEvent MAT_MatTransposeMult, MAT_MatTransposeMultSymbolic, MAT_MatTransposeMultNumeric;
 28: PetscLogEvent MAT_TransposeMatMult, MAT_TransposeMatMultSymbolic, MAT_TransposeMatMultNumeric;
 29: PetscLogEvent MAT_MatMatMult, MAT_MatMatMultSymbolic, MAT_MatMatMultNumeric;
 30: PetscLogEvent MAT_MultHermitianTranspose,MAT_MultHermitianTransposeAdd;
 31: PetscLogEvent MAT_Getsymtranspose, MAT_Getsymtransreduced, MAT_Transpose_SeqAIJ, MAT_GetBrowsOfAcols;
 32: PetscLogEvent MAT_GetBrowsOfAocols, MAT_Getlocalmat, MAT_Getlocalmatcondensed, MAT_Seqstompi, MAT_Seqstompinum, MAT_Seqstompisym;
 33: PetscLogEvent MAT_Applypapt, MAT_Applypapt_numeric, MAT_Applypapt_symbolic, MAT_GetSequentialNonzeroStructure;
 34: PetscLogEvent MAT_GetMultiProcBlock;
 35: PetscLogEvent MAT_CUSPCopyToGPU, MAT_CUSPARSECopyToGPU, MAT_SetValuesBatch, MAT_SetValuesBatchI, MAT_SetValuesBatchII, MAT_SetValuesBatchIII, MAT_SetValuesBatchIV;
 36: PetscLogEvent MAT_ViennaCLCopyToGPU;
 37: PetscLogEvent MAT_Merge,MAT_Residual;
 38: PetscLogEvent Mat_Coloring_Apply,Mat_Coloring_Comm,Mat_Coloring_Local,Mat_Coloring_ISCreate,Mat_Coloring_SetUp,Mat_Coloring_Weights;

 40: const char *const MatFactorTypes[] = {"NONE","LU","CHOLESKY","ILU","ICC","ILUDT","MatFactorType","MAT_FACTOR_",0};

 44: /*@
 45:    MatSetRandom - Sets all components of a matrix to random numbers. For sparse matrices that have been preallocated it randomly selects appropriate locations

 47:    Logically Collective on Vec

 49:    Input Parameters:
 50: +  x  - the vector
 51: -  rctx - the random number context, formed by PetscRandomCreate(), or NULL and
 52:           it will create one internally.

 54:    Output Parameter:
 55: .  x  - the vector

 57:    Example of Usage:
 58: .vb
 59:      PetscRandomCreate(PETSC_COMM_WORLD,&rctx);
 60:      VecSetRandom(x,rctx);
 61:      PetscRandomDestroy(rctx);
 62: .ve

 64:    Level: intermediate

 66:    Concepts: vector^setting to random
 67:    Concepts: random^vector

 69: .seealso: MatZeroEntries(), MatSetValues(), PetscRandomCreate(), PetscRandomDestroy()
 70: @*/
 71: PetscErrorCode MatSetRandom(Mat x,PetscRandom rctx)
 72: {
 74:   PetscRandom    randObj = NULL;


 81:   if (!rctx) {
 82:     MPI_Comm comm;
 83:     PetscObjectGetComm((PetscObject)x,&comm);
 84:     PetscRandomCreate(comm,&randObj);
 85:     PetscRandomSetFromOptions(randObj);
 86:     rctx = randObj;
 87:   }

 89:   PetscLogEventBegin(VEC_SetRandom,x,rctx,0,0);
 90:   (*x->ops->setrandom)(x,rctx);
 91:   PetscLogEventEnd(VEC_SetRandom,x,rctx,0,0);

 93:   x->assembled = PETSC_TRUE;
 94:   PetscRandomDestroy(&randObj);
 95:   return(0);
 96: }


101: /*@
102:       MatFindNonzeroRows - Locate all rows that are not completely zero in the matrix

104:   Input Parameter:
105: .    A  - the matrix

107:   Output Parameter:
108: .    keptrows - the rows that are not completely zero

110:   Level: intermediate

112:  @*/
113: PetscErrorCode MatFindNonzeroRows(Mat mat,IS *keptrows)
114: {

119:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
120:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
121:   if (!mat->ops->findnonzerorows) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Not coded for this matrix type");
122:   (*mat->ops->findnonzerorows)(mat,keptrows);
123:   return(0);
124: }

128: /*@
129:    MatGetDiagonalBlock - Returns the part of the matrix associated with the on-process coupling

131:    Not Collective

133:    Input Parameters:
134: .   A - the matrix

136:    Output Parameters:
137: .   a - the diagonal part (which is a SEQUENTIAL matrix)

139:    Notes: see the manual page for MatCreateAIJ() for more information on the "diagonal part" of the matrix.
140:           Use caution, as the reference count on the returned matrix is not incremented and it is used as
141:           part of the containing MPI Mat's normal operation.

143:    Level: advanced

145: @*/
146: PetscErrorCode MatGetDiagonalBlock(Mat A,Mat *a)
147: {
148:   PetscErrorCode ierr,(*f)(Mat,Mat*);
149:   PetscMPIInt    size;

155:   if (A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
156:   MPI_Comm_size(PetscObjectComm((PetscObject)A),&size);
157:   PetscObjectQueryFunction((PetscObject)A,"MatGetDiagonalBlock_C",&f);
158:   if (f) {
159:     (*f)(A,a);
160:     return(0);
161:   } else if (size == 1) {
162:     *a = A;
163:   } else {
164:     MatType mattype;
165:     MatGetType(A,&mattype);
166:     SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Matrix type %s does not support getting diagonal block",mattype);
167:   }
168:   return(0);
169: }

173: /*@
174:    MatGetTrace - Gets the trace of a matrix. The sum of the diagonal entries.

176:    Collective on Mat

178:    Input Parameters:
179: .  mat - the matrix

181:    Output Parameter:
182: .   trace - the sum of the diagonal entries

184:    Level: advanced

186: @*/
187: PetscErrorCode MatGetTrace(Mat mat,PetscScalar *trace)
188: {
190:   Vec            diag;

193:   MatCreateVecs(mat,&diag,NULL);
194:   MatGetDiagonal(mat,diag);
195:   VecSum(diag,trace);
196:   VecDestroy(&diag);
197:   return(0);
198: }

202: /*@
203:    MatRealPart - Zeros out the imaginary part of the matrix

205:    Logically Collective on Mat

207:    Input Parameters:
208: .  mat - the matrix

210:    Level: advanced


213: .seealso: MatImaginaryPart()
214: @*/
215: PetscErrorCode MatRealPart(Mat mat)
216: {

222:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
223:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
224:   if (!mat->ops->realpart) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
225:   MatCheckPreallocated(mat,1);
226:   (*mat->ops->realpart)(mat);
227: #if defined(PETSC_HAVE_CUSP)
228:   if (mat->valid_GPU_matrix != PETSC_CUSP_UNALLOCATED) {
229:     mat->valid_GPU_matrix = PETSC_CUSP_CPU;
230:   }
231: #endif
232: #if defined(PETSC_HAVE_VIENNACL)
233:   if (mat->valid_GPU_matrix != PETSC_VIENNACL_UNALLOCATED) {
234:     mat->valid_GPU_matrix = PETSC_VIENNACL_CPU;
235:   }
236: #endif
237:   return(0);
238: }

242: /*@C
243:    MatGetGhosts - Get the global index of all ghost nodes defined by the sparse matrix

245:    Collective on Mat

247:    Input Parameter:
248: .  mat - the matrix

250:    Output Parameters:
251: +   nghosts - number of ghosts (note for BAIJ matrices there is one ghost for each block)
252: -   ghosts - the global indices of the ghost points

254:    Notes: the nghosts and ghosts are suitable to pass into VecCreateGhost()

256:    Level: advanced

258: @*/
259: PetscErrorCode MatGetGhosts(Mat mat,PetscInt *nghosts,const PetscInt *ghosts[])
260: {

266:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
267:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
268:   if (!mat->ops->getghosts) {
269:     if (nghosts) *nghosts = 0;
270:     if (ghosts) *ghosts = 0;
271:   } else {
272:     (*mat->ops->getghosts)(mat,nghosts,ghosts);
273:   }
274:   return(0);
275: }


280: /*@
281:    MatImaginaryPart - Moves the imaginary part of the matrix to the real part and zeros the imaginary part

283:    Logically Collective on Mat

285:    Input Parameters:
286: .  mat - the matrix

288:    Level: advanced


291: .seealso: MatRealPart()
292: @*/
293: PetscErrorCode MatImaginaryPart(Mat mat)
294: {

300:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
301:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
302:   if (!mat->ops->imaginarypart) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
303:   MatCheckPreallocated(mat,1);
304:   (*mat->ops->imaginarypart)(mat);
305: #if defined(PETSC_HAVE_CUSP)
306:   if (mat->valid_GPU_matrix != PETSC_CUSP_UNALLOCATED) {
307:     mat->valid_GPU_matrix = PETSC_CUSP_CPU;
308:   }
309: #endif
310: #if defined(PETSC_HAVE_VIENNACL)
311:   if (mat->valid_GPU_matrix != PETSC_VIENNACL_UNALLOCATED) {
312:     mat->valid_GPU_matrix = PETSC_VIENNACL_CPU;
313:   }
314: #endif
315:   return(0);
316: }

320: /*@
321:    MatMissingDiagonal - Determine if sparse matrix is missing a diagonal entry (or block entry for BAIJ matrices)

323:    Collective on Mat

325:    Input Parameter:
326: .  mat - the matrix

328:    Output Parameters:
329: +  missing - is any diagonal missing
330: -  dd - first diagonal entry that is missing (optional)

332:    Level: advanced


335: .seealso: MatRealPart()
336: @*/
337: PetscErrorCode MatMissingDiagonal(Mat mat,PetscBool *missing,PetscInt *dd)
338: {

344:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
345:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
346:   if (!mat->ops->missingdiagonal) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
347:   (*mat->ops->missingdiagonal)(mat,missing,dd);
348:   return(0);
349: }

353: /*@C
354:    MatGetRow - Gets a row of a matrix.  You MUST call MatRestoreRow()
355:    for each row that you get to ensure that your application does
356:    not bleed memory.

358:    Not Collective

360:    Input Parameters:
361: +  mat - the matrix
362: -  row - the row to get

364:    Output Parameters:
365: +  ncols -  if not NULL, the number of nonzeros in the row
366: .  cols - if not NULL, the column numbers
367: -  vals - if not NULL, the values

369:    Notes:
370:    This routine is provided for people who need to have direct access
371:    to the structure of a matrix.  We hope that we provide enough
372:    high-level matrix routines that few users will need it.

374:    MatGetRow() always returns 0-based column indices, regardless of
375:    whether the internal representation is 0-based (default) or 1-based.

377:    For better efficiency, set cols and/or vals to NULL if you do
378:    not wish to extract these quantities.

380:    The user can only examine the values extracted with MatGetRow();
381:    the values cannot be altered.  To change the matrix entries, one
382:    must use MatSetValues().

384:    You can only have one call to MatGetRow() outstanding for a particular
385:    matrix at a time, per processor. MatGetRow() can only obtain rows
386:    associated with the given processor, it cannot get rows from the
387:    other processors; for that we suggest using MatGetSubMatrices(), then
388:    MatGetRow() on the submatrix. The row indix passed to MatGetRows()
389:    is in the global number of rows.

391:    Fortran Notes:
392:    The calling sequence from Fortran is
393: .vb
394:    MatGetRow(matrix,row,ncols,cols,values,ierr)
395:          Mat     matrix (input)
396:          integer row    (input)
397:          integer ncols  (output)
398:          integer cols(maxcols) (output)
399:          double precision (or double complex) values(maxcols) output
400: .ve
401:    where maxcols >= maximum nonzeros in any row of the matrix.


404:    Caution:
405:    Do not try to change the contents of the output arrays (cols and vals).
406:    In some cases, this may corrupt the matrix.

408:    Level: advanced

410:    Concepts: matrices^row access

412: .seealso: MatRestoreRow(), MatSetValues(), MatGetValues(), MatGetSubMatrices(), MatGetDiagonal()
413: @*/
414: PetscErrorCode MatGetRow(Mat mat,PetscInt row,PetscInt *ncols,const PetscInt *cols[],const PetscScalar *vals[])
415: {
417:   PetscInt       incols;

422:   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
423:   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
424:   if (!mat->ops->getrow) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
425:   MatCheckPreallocated(mat,1);
426:   PetscLogEventBegin(MAT_GetRow,mat,0,0,0);
427:   (*mat->ops->getrow)(mat,row,&incols,(PetscInt**)cols,(PetscScalar**)vals);
428:   if (ncols) *ncols = incols;
429:   PetscLogEventEnd(MAT_GetRow,mat,0,0,0);
430:   return(0);
431: }

435: /*@
436:    MatConjugate - replaces the matrix values with their complex conjugates

438:    Logically Collective on Mat

440:    Input Parameters:
441: .  mat - the matrix

443:    Level: advanced

445: .seealso:  VecConjugate()
446: @*/
447: PetscErrorCode MatConjugate(Mat mat)
448: {
449: #if defined(PETSC_USE_COMPLEX)

454:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
455:   if (!mat->ops->conjugate) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Not provided for this matrix format, send email to petsc-maint@mcs.anl.gov");
456:   (*mat->ops->conjugate)(mat);
457: #if defined(PETSC_HAVE_CUSP)
458:   if (mat->valid_GPU_matrix != PETSC_CUSP_UNALLOCATED) {
459:     mat->valid_GPU_matrix = PETSC_CUSP_CPU;
460:   }
461: #endif
462: #if defined(PETSC_HAVE_VIENNACL)
463:   if (mat->valid_GPU_matrix != PETSC_VIENNACL_UNALLOCATED) {
464:     mat->valid_GPU_matrix = PETSC_VIENNACL_CPU;
465:   }
466: #endif
467:   return(0);
468: #else
469:   return 0;
470: #endif
471: }

475: /*@C
476:    MatRestoreRow - Frees any temporary space allocated by MatGetRow().

478:    Not Collective

480:    Input Parameters:
481: +  mat - the matrix
482: .  row - the row to get
483: .  ncols, cols - the number of nonzeros and their columns
484: -  vals - if nonzero the column values

486:    Notes:
487:    This routine should be called after you have finished examining the entries.

489:    This routine zeros out ncols, cols, and vals. This is to prevent accidental
490:    us of the array after it has been restored. If you pass NULL, it will
491:    not zero the pointers.  Use of cols or vals after MatRestoreRow is invalid.

493:    Fortran Notes:
494:    The calling sequence from Fortran is
495: .vb
496:    MatRestoreRow(matrix,row,ncols,cols,values,ierr)
497:       Mat     matrix (input)
498:       integer row    (input)
499:       integer ncols  (output)
500:       integer cols(maxcols) (output)
501:       double precision (or double complex) values(maxcols) output
502: .ve
503:    Where maxcols >= maximum nonzeros in any row of the matrix.

505:    In Fortran MatRestoreRow() MUST be called after MatGetRow()
506:    before another call to MatGetRow() can be made.

508:    Level: advanced

510: .seealso:  MatGetRow()
511: @*/
512: PetscErrorCode MatRestoreRow(Mat mat,PetscInt row,PetscInt *ncols,const PetscInt *cols[],const PetscScalar *vals[])
513: {

519:   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
520:   if (!mat->ops->restorerow) return(0);
521:   (*mat->ops->restorerow)(mat,row,ncols,(PetscInt **)cols,(PetscScalar **)vals);
522:   if (ncols) *ncols = 0;
523:   if (cols)  *cols = NULL;
524:   if (vals)  *vals = NULL;
525:   return(0);
526: }

530: /*@
531:    MatGetRowUpperTriangular - Sets a flag to enable calls to MatGetRow() for matrix in MATSBAIJ format.
532:    You should call MatRestoreRowUpperTriangular() after calling MatGetRow/MatRestoreRow() to disable the flag.

534:    Not Collective

536:    Input Parameters:
537: +  mat - the matrix

539:    Notes:
540:    The flag is to ensure that users are aware of MatGetRow() only provides the upper trianglular part of the row for the matrices in MATSBAIJ format.

542:    Level: advanced

544:    Concepts: matrices^row access

546: .seealso: MatRestoreRowRowUpperTriangular()
547: @*/
548: PetscErrorCode MatGetRowUpperTriangular(Mat mat)
549: {

555:   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
556:   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
557:   if (!mat->ops->getrowuppertriangular) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
558:   MatCheckPreallocated(mat,1);
559:   (*mat->ops->getrowuppertriangular)(mat);
560:   return(0);
561: }

565: /*@
566:    MatRestoreRowUpperTriangular - Disable calls to MatGetRow() for matrix in MATSBAIJ format.

568:    Not Collective

570:    Input Parameters:
571: +  mat - the matrix

573:    Notes:
574:    This routine should be called after you have finished MatGetRow/MatRestoreRow().


577:    Level: advanced

579: .seealso:  MatGetRowUpperTriangular()
580: @*/
581: PetscErrorCode MatRestoreRowUpperTriangular(Mat mat)
582: {

587:   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
588:   if (!mat->ops->restorerowuppertriangular) return(0);
589:   (*mat->ops->restorerowuppertriangular)(mat);
590:   return(0);
591: }

595: /*@C
596:    MatSetOptionsPrefix - Sets the prefix used for searching for all
597:    Mat options in the database.

599:    Logically Collective on Mat

601:    Input Parameter:
602: +  A - the Mat context
603: -  prefix - the prefix to prepend to all option names

605:    Notes:
606:    A hyphen (-) must NOT be given at the beginning of the prefix name.
607:    The first character of all runtime options is AUTOMATICALLY the hyphen.

609:    Level: advanced

611: .keywords: Mat, set, options, prefix, database

613: .seealso: MatSetFromOptions()
614: @*/
615: PetscErrorCode MatSetOptionsPrefix(Mat A,const char prefix[])
616: {

621:   PetscObjectSetOptionsPrefix((PetscObject)A,prefix);
622:   return(0);
623: }

627: /*@C
628:    MatAppendOptionsPrefix - Appends to the prefix used for searching for all
629:    Mat options in the database.

631:    Logically Collective on Mat

633:    Input Parameters:
634: +  A - the Mat context
635: -  prefix - the prefix to prepend to all option names

637:    Notes:
638:    A hyphen (-) must NOT be given at the beginning of the prefix name.
639:    The first character of all runtime options is AUTOMATICALLY the hyphen.

641:    Level: advanced

643: .keywords: Mat, append, options, prefix, database

645: .seealso: MatGetOptionsPrefix()
646: @*/
647: PetscErrorCode MatAppendOptionsPrefix(Mat A,const char prefix[])
648: {

653:   PetscObjectAppendOptionsPrefix((PetscObject)A,prefix);
654:   return(0);
655: }

659: /*@C
660:    MatGetOptionsPrefix - Sets the prefix used for searching for all
661:    Mat options in the database.

663:    Not Collective

665:    Input Parameter:
666: .  A - the Mat context

668:    Output Parameter:
669: .  prefix - pointer to the prefix string used

671:    Notes: On the fortran side, the user should pass in a string 'prefix' of
672:    sufficient length to hold the prefix.

674:    Level: advanced

676: .keywords: Mat, get, options, prefix, database

678: .seealso: MatAppendOptionsPrefix()
679: @*/
680: PetscErrorCode MatGetOptionsPrefix(Mat A,const char *prefix[])
681: {

686:   PetscObjectGetOptionsPrefix((PetscObject)A,prefix);
687:   return(0);
688: }

692: /*@
693:    MatSetUp - Sets up the internal matrix data structures for the later use.

695:    Collective on Mat

697:    Input Parameters:
698: .  A - the Mat context

700:    Notes:
701:    If the user has not set preallocation for this matrix then a default preallocation that is likely to be inefficient is used.

703:    If a suitable preallocation routine is used, this function does not need to be called.

705:    See the Performance chapter of the PETSc users manual for how to preallocate matrices

707:    Level: beginner

709: .keywords: Mat, setup

711: .seealso: MatCreate(), MatDestroy()
712: @*/
713: PetscErrorCode MatSetUp(Mat A)
714: {
715:   PetscMPIInt    size;

720:   if (!((PetscObject)A)->type_name) {
721:     MPI_Comm_size(PetscObjectComm((PetscObject)A), &size);
722:     if (size == 1) {
723:       MatSetType(A, MATSEQAIJ);
724:     } else {
725:       MatSetType(A, MATMPIAIJ);
726:     }
727:   }
728:   if (!A->preallocated && A->ops->setup) {
729:     PetscInfo(A,"Warning not preallocating matrix storage\n");
730:     (*A->ops->setup)(A);
731:   }
732:   A->preallocated = PETSC_TRUE;
733:   return(0);
734: }

736: #if defined(PETSC_HAVE_SAWS)
737: #include <petscviewersaws.h>
738: #endif
741: /*@C
742:    MatView - Visualizes a matrix object.

744:    Collective on Mat

746:    Input Parameters:
747: +  mat - the matrix
748: -  viewer - visualization context

750:   Notes:
751:   The available visualization contexts include
752: +    PETSC_VIEWER_STDOUT_SELF - for sequential matrices
753: .    PETSC_VIEWER_STDOUT_WORLD - for parallel matrices created on PETSC_COMM_WORLD
754: .    PETSC_VIEWER_STDOUT_(comm) - for matrices created on MPI communicator comm
755: -     PETSC_VIEWER_DRAW_WORLD - graphical display of nonzero structure

757:    The user can open alternative visualization contexts with
758: +    PetscViewerASCIIOpen() - Outputs matrix to a specified file
759: .    PetscViewerBinaryOpen() - Outputs matrix in binary to a
760:          specified file; corresponding input uses MatLoad()
761: .    PetscViewerDrawOpen() - Outputs nonzero matrix structure to
762:          an X window display
763: -    PetscViewerSocketOpen() - Outputs matrix to Socket viewer.
764:          Currently only the sequential dense and AIJ
765:          matrix types support the Socket viewer.

767:    The user can call PetscViewerPushFormat() to specify the output
768:    format of ASCII printed objects (when using PETSC_VIEWER_STDOUT_SELF,
769:    PETSC_VIEWER_STDOUT_WORLD and PetscViewerASCIIOpen).  Available formats include
770: +    PETSC_VIEWER_DEFAULT - default, prints matrix contents
771: .    PETSC_VIEWER_ASCII_MATLAB - prints matrix contents in Matlab format
772: .    PETSC_VIEWER_ASCII_DENSE - prints entire matrix including zeros
773: .    PETSC_VIEWER_ASCII_COMMON - prints matrix contents, using a sparse
774:          format common among all matrix types
775: .    PETSC_VIEWER_ASCII_IMPL - prints matrix contents, using an implementation-specific
776:          format (which is in many cases the same as the default)
777: .    PETSC_VIEWER_ASCII_INFO - prints basic information about the matrix
778:          size and structure (not the matrix entries)
779: .    PETSC_VIEWER_ASCII_INFO_DETAIL - prints more detailed information about
780:          the matrix structure

782:    Options Database Keys:
783: +  -mat_view ::ascii_info - Prints info on matrix at conclusion of MatEndAssembly()
784: .  -mat_view ::ascii_info_detail - Prints more detailed info
785: .  -mat_view - Prints matrix in ASCII format
786: .  -mat_view ::ascii_matlab - Prints matrix in Matlab format
787: .  -mat_view draw - PetscDraws nonzero structure of matrix, using MatView() and PetscDrawOpenX().
788: .  -display <name> - Sets display name (default is host)
789: .  -draw_pause <sec> - Sets number of seconds to pause after display
790: .  -mat_view socket - Sends matrix to socket, can be accessed from Matlab (see Users-Manual: Chapter 11 Using MATLAB with PETSc for details)
791: .  -viewer_socket_machine <machine> -
792: .  -viewer_socket_port <port> -
793: .  -mat_view binary - save matrix to file in binary format
794: -  -viewer_binary_filename <name> -
795:    Level: beginner

797:    Notes: see the manual page for MatLoad() for the exact format of the binary file when the binary
798:       viewer is used.

800:       See share/petsc/matlab/PetscBinaryRead.m for a Matlab code that can read in the binary file when the binary
801:       viewer is used.

803:       One can use '-mat_view draw -draw_pause -1' to pause the graphical display of matrix nonzero structure.
804:       And then use the following mouse functions:
805:           left mouse: zoom in
806:           middle mouse: zoom out
807:           right mouse: continue with the simulation

809:    Concepts: matrices^viewing
810:    Concepts: matrices^plotting
811:    Concepts: matrices^printing

813: .seealso: PetscViewerPushFormat(), PetscViewerASCIIOpen(), PetscViewerDrawOpen(),
814:           PetscViewerSocketOpen(), PetscViewerBinaryOpen(), MatLoad()
815: @*/
816: PetscErrorCode MatView(Mat mat,PetscViewer viewer)
817: {
818:   PetscErrorCode    ierr;
819:   PetscInt          rows,cols,rbs,cbs;
820:   PetscBool         iascii;
821:   PetscViewerFormat format;
822: #if defined(PETSC_HAVE_SAWS)
823:   PetscBool         issaws;
824: #endif

829:   if (!viewer) {
830:     PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)mat),&viewer);
831:   }
834:   MatCheckPreallocated(mat,1);

836:   PetscLogEventBegin(MAT_View,mat,viewer,0,0);
837:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
838:   PetscViewerGetFormat(viewer,&format);
839:   if ((!iascii || (format != PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL)) && mat->factortype) {
840:     SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"No viewers for factored matrix except ASCII info or info_detailed");
841:   }

843: #if defined(PETSC_HAVE_SAWS)
844:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);
845: #endif
846:   if (iascii) {
847:     if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ORDER,"Must call MatAssemblyBegin/End() before viewing matrix");
848:     PetscObjectPrintClassNamePrefixType((PetscObject)mat,viewer);
849:     if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
850:       PetscViewerASCIIPushTab(viewer);
851:       MatGetSize(mat,&rows,&cols);
852:       MatGetBlockSizes(mat,&rbs,&cbs);
853:       if (rbs != 1 || cbs != 1) {
854:         if (rbs != cbs) {PetscViewerASCIIPrintf(viewer,"rows=%D, cols=%D, rbs=%D, cbs = %D\n",rows,cols,rbs,cbs);}
855:         else            {PetscViewerASCIIPrintf(viewer,"rows=%D, cols=%D, bs=%D\n",rows,cols,rbs);}
856:       } else {
857:         PetscViewerASCIIPrintf(viewer,"rows=%D, cols=%D\n",rows,cols);
858:       }
859:       if (mat->factortype) {
860:         const MatSolverPackage solver;
861:         MatFactorGetSolverPackage(mat,&solver);
862:         PetscViewerASCIIPrintf(viewer,"package used to perform factorization: %s\n",solver);
863:       }
864:       if (mat->ops->getinfo) {
865:         MatInfo info;
866:         MatGetInfo(mat,MAT_GLOBAL_SUM,&info);
867:         PetscViewerASCIIPrintf(viewer,"total: nonzeros=%.f, allocated nonzeros=%.f\n",info.nz_used,info.nz_allocated);
868:         PetscViewerASCIIPrintf(viewer,"total number of mallocs used during MatSetValues calls =%D\n",(PetscInt)info.mallocs);
869:       }
870:       if (mat->nullsp) {PetscViewerASCIIPrintf(viewer,"  has attached null space\n");}
871:       if (mat->nearnullsp) {PetscViewerASCIIPrintf(viewer,"  has attached near null space\n");}
872:     }
873: #if defined(PETSC_HAVE_SAWS)
874:   } else if (issaws) {
875:     PetscMPIInt rank;

877:     PetscObjectName((PetscObject)mat);
878:     MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
879:     if (!((PetscObject)mat)->amsmem && !rank) {
880:       PetscObjectViewSAWs((PetscObject)mat,viewer);
881:     }
882: #endif
883:   }
884:   if (mat->ops->view) {
885:     PetscViewerASCIIPushTab(viewer);
886:     (*mat->ops->view)(mat,viewer);
887:     PetscViewerASCIIPopTab(viewer);
888:   }
889:   if (iascii) {
890:     if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ORDER,"Must call MatAssemblyBegin/End() before viewing matrix");
891:     PetscViewerGetFormat(viewer,&format);
892:     if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
893:       PetscViewerASCIIPopTab(viewer);
894:     }
895:   }
896:   PetscLogEventEnd(MAT_View,mat,viewer,0,0);
897:   return(0);
898: }

900: #if defined(PETSC_USE_DEBUG)
901: #include <../src/sys/totalview/tv_data_display.h>
902: PETSC_UNUSED static int TV_display_type(const struct _p_Mat *mat)
903: {
904:   TV_add_row("Local rows", "int", &mat->rmap->n);
905:   TV_add_row("Local columns", "int", &mat->cmap->n);
906:   TV_add_row("Global rows", "int", &mat->rmap->N);
907:   TV_add_row("Global columns", "int", &mat->cmap->N);
908:   TV_add_row("Typename", TV_ascii_string_type, ((PetscObject)mat)->type_name);
909:   return TV_format_OK;
910: }
911: #endif

915: /*@C
916:    MatLoad - Loads a matrix that has been stored in binary format
917:    with MatView().  The matrix format is determined from the options database.
918:    Generates a parallel MPI matrix if the communicator has more than one
919:    processor.  The default matrix type is AIJ.

921:    Collective on PetscViewer

923:    Input Parameters:
924: +  newmat - the newly loaded matrix, this needs to have been created with MatCreate()
925:             or some related function before a call to MatLoad()
926: -  viewer - binary file viewer, created with PetscViewerBinaryOpen()

928:    Options Database Keys:
929:    Used with block matrix formats (MATSEQBAIJ,  ...) to specify
930:    block size
931: .    -matload_block_size <bs>

933:    Level: beginner

935:    Notes:
936:    If the Mat type has not yet been given then MATAIJ is used, call MatSetFromOptions() on the
937:    Mat before calling this routine if you wish to set it from the options database.

939:    MatLoad() automatically loads into the options database any options
940:    given in the file filename.info where filename is the name of the file
941:    that was passed to the PetscViewerBinaryOpen(). The options in the info
942:    file will be ignored if you use the -viewer_binary_skip_info option.

944:    If the type or size of newmat is not set before a call to MatLoad, PETSc
945:    sets the default matrix type AIJ and sets the local and global sizes.
946:    If type and/or size is already set, then the same are used.

948:    In parallel, each processor can load a subset of rows (or the
949:    entire matrix).  This routine is especially useful when a large
950:    matrix is stored on disk and only part of it is desired on each
951:    processor.  For example, a parallel solver may access only some of
952:    the rows from each processor.  The algorithm used here reads
953:    relatively small blocks of data rather than reading the entire
954:    matrix and then subsetting it.

956:    Notes for advanced users:
957:    Most users should not need to know the details of the binary storage
958:    format, since MatLoad() and MatView() completely hide these details.
959:    But for anyone who's interested, the standard binary matrix storage
960:    format is

962: $    int    MAT_FILE_CLASSID
963: $    int    number of rows
964: $    int    number of columns
965: $    int    total number of nonzeros
966: $    int    *number nonzeros in each row
967: $    int    *column indices of all nonzeros (starting index is zero)
968: $    PetscScalar *values of all nonzeros

970:    PETSc automatically does the byte swapping for
971: machines that store the bytes reversed, e.g.  DEC alpha, freebsd,
972: linux, Windows and the paragon; thus if you write your own binary
973: read/write routines you have to swap the bytes; see PetscBinaryRead()
974: and PetscBinaryWrite() to see how this may be done.

976: .keywords: matrix, load, binary, input

978: .seealso: PetscViewerBinaryOpen(), MatView(), VecLoad()

980:  @*/
981: PetscErrorCode MatLoad(Mat newmat,PetscViewer viewer)
982: {
984:   PetscBool      isbinary,flg;

989:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
990:   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");

992:   if (!((PetscObject)newmat)->type_name) {
993:     MatSetType(newmat,MATAIJ);
994:   }

996:   if (!newmat->ops->load) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"MatLoad is not supported for type");
997:   PetscLogEventBegin(MAT_Load,viewer,0,0,0);
998:   (*newmat->ops->load)(newmat,viewer);
999:   PetscLogEventEnd(MAT_Load,viewer,0,0,0);

1001:   flg  = PETSC_FALSE;
1002:   PetscOptionsGetBool(((PetscObject)newmat)->options,((PetscObject)newmat)->prefix,"-matload_symmetric",&flg,NULL);
1003:   if (flg) {
1004:     MatSetOption(newmat,MAT_SYMMETRIC,PETSC_TRUE);
1005:     MatSetOption(newmat,MAT_SYMMETRY_ETERNAL,PETSC_TRUE);
1006:   }
1007:   flg  = PETSC_FALSE;
1008:   PetscOptionsGetBool(((PetscObject)newmat)->options,((PetscObject)newmat)->prefix,"-matload_spd",&flg,NULL);
1009:   if (flg) {
1010:     MatSetOption(newmat,MAT_SPD,PETSC_TRUE);
1011:   }
1012:   return(0);
1013: }

1017: PetscErrorCode MatDestroy_Redundant(Mat_Redundant **redundant)
1018: {
1020:   Mat_Redundant  *redund = *redundant;
1021:   PetscInt       i;

1024:   if (redund){
1025:     if (redund->matseq) { /* via MatGetSubMatrices()  */
1026:       ISDestroy(&redund->isrow);
1027:       ISDestroy(&redund->iscol);
1028:       MatDestroy(&redund->matseq[0]);
1029:       PetscFree(redund->matseq);
1030:     } else {
1031:       PetscFree2(redund->send_rank,redund->recv_rank);
1032:       PetscFree(redund->sbuf_j);
1033:       PetscFree(redund->sbuf_a);
1034:       for (i=0; i<redund->nrecvs; i++) {
1035:         PetscFree(redund->rbuf_j[i]);
1036:         PetscFree(redund->rbuf_a[i]);
1037:       }
1038:       PetscFree4(redund->sbuf_nz,redund->rbuf_nz,redund->rbuf_j,redund->rbuf_a);
1039:     }

1041:     if (redund->subcomm) {
1042:       PetscCommDestroy(&redund->subcomm);
1043:     }
1044:     PetscFree(redund);
1045:   }
1046:   return(0);
1047: }

1051: /*@
1052:    MatDestroy - Frees space taken by a matrix.

1054:    Collective on Mat

1056:    Input Parameter:
1057: .  A - the matrix

1059:    Level: beginner

1061: @*/
1062: PetscErrorCode MatDestroy(Mat *A)
1063: {

1067:   if (!*A) return(0);
1069:   if (--((PetscObject)(*A))->refct > 0) {*A = NULL; return(0);}

1071:   /* if memory was published with SAWs then destroy it */
1072:   PetscObjectSAWsViewOff((PetscObject)*A);
1073:   if ((*A)->ops->destroy) {
1074:     (*(*A)->ops->destroy)(*A);
1075:   }
1076:   MatDestroy_Redundant(&(*A)->redundant);
1077:   MatNullSpaceDestroy(&(*A)->nullsp);
1078:   MatNullSpaceDestroy(&(*A)->transnullsp);
1079:   MatNullSpaceDestroy(&(*A)->nearnullsp);
1080:   PetscLayoutDestroy(&(*A)->rmap);
1081:   PetscLayoutDestroy(&(*A)->cmap);
1082:   PetscHeaderDestroy(A);
1083:   return(0);
1084: }

1088: /*@
1089:    MatSetValues - Inserts or adds a block of values into a matrix.
1090:    These values may be cached, so MatAssemblyBegin() and MatAssemblyEnd()
1091:    MUST be called after all calls to MatSetValues() have been completed.

1093:    Not Collective

1095:    Input Parameters:
1096: +  mat - the matrix
1097: .  v - a logically two-dimensional array of values
1098: .  m, idxm - the number of rows and their global indices
1099: .  n, idxn - the number of columns and their global indices
1100: -  addv - either ADD_VALUES or INSERT_VALUES, where
1101:    ADD_VALUES adds values to any existing entries, and
1102:    INSERT_VALUES replaces existing entries with new values

1104:    Notes:
1105:    If you create the matrix yourself (that is not with a call to DMCreateMatrix()) then you MUST call MatXXXXSetPreallocation() or
1106:       MatSetUp() before using this routine

1108:    By default the values, v, are row-oriented. See MatSetOption() for other options.

1110:    Calls to MatSetValues() with the INSERT_VALUES and ADD_VALUES
1111:    options cannot be mixed without intervening calls to the assembly
1112:    routines.

1114:    MatSetValues() uses 0-based row and column numbers in Fortran
1115:    as well as in C.

1117:    Negative indices may be passed in idxm and idxn, these rows and columns are
1118:    simply ignored. This allows easily inserting element stiffness matrices
1119:    with homogeneous Dirchlet boundary conditions that you don't want represented
1120:    in the matrix.

1122:    Efficiency Alert:
1123:    The routine MatSetValuesBlocked() may offer much better efficiency
1124:    for users of block sparse formats (MATSEQBAIJ and MATMPIBAIJ).

1126:    Level: beginner

1128:    Concepts: matrices^putting entries in

1130: .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(),
1131:           InsertMode, INSERT_VALUES, ADD_VALUES
1132: @*/
1133: PetscErrorCode MatSetValues(Mat mat,PetscInt m,const PetscInt idxm[],PetscInt n,const PetscInt idxn[],const PetscScalar v[],InsertMode addv)
1134: {
1136: #if defined(PETSC_USE_DEBUG)
1137:   PetscInt       i,j;
1138: #endif

1143:   if (!m || !n) return(0); /* no values to insert */
1147:   MatCheckPreallocated(mat,1);
1148:   if (mat->insertmode == NOT_SET_VALUES) {
1149:     mat->insertmode = addv;
1150:   }
1151: #if defined(PETSC_USE_DEBUG)
1152:   else if (mat->insertmode != addv) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values");
1153:   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1154:   if (!mat->ops->setvalues) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);

1156:   for (i=0; i<m; i++) {
1157:     for (j=0; j<n; j++) {
1158:       if (mat->erroriffailure && PetscIsInfOrNanScalar(v[i*n+j]))
1159: #if defined(PETSC_USE_COMPLEX)
1160:         SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_FP,"Inserting %g+ig at matrix entry (%D,%D)",(double)PetscRealPart(v[i*n+j]),(double)PetscImaginaryPart(v[i*n+j]),idxm[i],idxn[j]);
1161: #else
1162:         SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_FP,"Inserting %g at matrix entry (%D,%D)",(double)v[i*n+j],idxm[i],idxn[j]);
1163: #endif
1164:     }
1165:   }
1166: #endif

1168:   if (mat->assembled) {
1169:     mat->was_assembled = PETSC_TRUE;
1170:     mat->assembled     = PETSC_FALSE;
1171:   }
1172:   PetscLogEventBegin(MAT_SetValues,mat,0,0,0);
1173:   (*mat->ops->setvalues)(mat,m,idxm,n,idxn,v,addv);
1174:   PetscLogEventEnd(MAT_SetValues,mat,0,0,0);
1175: #if defined(PETSC_HAVE_CUSP)
1176:   if (mat->valid_GPU_matrix != PETSC_CUSP_UNALLOCATED) {
1177:     mat->valid_GPU_matrix = PETSC_CUSP_CPU;
1178:   }
1179: #endif
1180: #if defined(PETSC_HAVE_VIENNACL)
1181:   if (mat->valid_GPU_matrix != PETSC_VIENNACL_UNALLOCATED) {
1182:     mat->valid_GPU_matrix = PETSC_VIENNACL_CPU;
1183:   }
1184: #endif
1185:   return(0);
1186: }


1191: /*@
1192:    MatSetValuesRowLocal - Inserts a row (block row for BAIJ matrices) of nonzero
1193:         values into a matrix

1195:    Not Collective

1197:    Input Parameters:
1198: +  mat - the matrix
1199: .  row - the (block) row to set
1200: -  v - a logically two-dimensional array of values

1202:    Notes:
1203:    By the values, v, are column-oriented (for the block version) and sorted

1205:    All the nonzeros in the row must be provided

1207:    The matrix must have previously had its column indices set

1209:    The row must belong to this process

1211:    Level: intermediate

1213:    Concepts: matrices^putting entries in

1215: .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(),
1216:           InsertMode, INSERT_VALUES, ADD_VALUES, MatSetValues(), MatSetValuesRow(), MatSetLocalToGlobalMapping()
1217: @*/
1218: PetscErrorCode MatSetValuesRowLocal(Mat mat,PetscInt row,const PetscScalar v[])
1219: {
1221:   PetscInt       globalrow;

1227:   ISLocalToGlobalMappingApply(mat->rmap->mapping,1,&row,&globalrow);
1228:   MatSetValuesRow(mat,globalrow,v);
1229: #if defined(PETSC_HAVE_CUSP)
1230:   if (mat->valid_GPU_matrix != PETSC_CUSP_UNALLOCATED) {
1231:     mat->valid_GPU_matrix = PETSC_CUSP_CPU;
1232:   }
1233: #endif
1234: #if defined(PETSC_HAVE_VIENNACL)
1235:   if (mat->valid_GPU_matrix != PETSC_VIENNACL_UNALLOCATED) {
1236:     mat->valid_GPU_matrix = PETSC_VIENNACL_CPU;
1237:   }
1238: #endif
1239:   return(0);
1240: }

1244: /*@
1245:    MatSetValuesRow - Inserts a row (block row for BAIJ matrices) of nonzero
1246:         values into a matrix

1248:    Not Collective

1250:    Input Parameters:
1251: +  mat - the matrix
1252: .  row - the (block) row to set
1253: -  v - a logically two-dimensional array of values

1255:    Notes:
1256:    The values, v, are column-oriented for the block version.

1258:    All the nonzeros in the row must be provided

1260:    THE MATRIX MUSAT HAVE PREVIOUSLY HAD ITS COLUMN INDICES SET. IT IS RARE THAT THIS ROUTINE IS USED, usually MatSetValues() is used.

1262:    The row must belong to this process

1264:    Level: advanced

1266:    Concepts: matrices^putting entries in

1268: .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(),
1269:           InsertMode, INSERT_VALUES, ADD_VALUES, MatSetValues()
1270: @*/
1271: PetscErrorCode MatSetValuesRow(Mat mat,PetscInt row,const PetscScalar v[])
1272: {

1278:   MatCheckPreallocated(mat,1);
1280: #if defined(PETSC_USE_DEBUG)
1281:   if (mat->insertmode == ADD_VALUES) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add and insert values");
1282:   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1283: #endif
1284:   mat->insertmode = INSERT_VALUES;

1286:   if (mat->assembled) {
1287:     mat->was_assembled = PETSC_TRUE;
1288:     mat->assembled     = PETSC_FALSE;
1289:   }
1290:   PetscLogEventBegin(MAT_SetValues,mat,0,0,0);
1291:   if (!mat->ops->setvaluesrow) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
1292:   (*mat->ops->setvaluesrow)(mat,row,v);
1293:   PetscLogEventEnd(MAT_SetValues,mat,0,0,0);
1294: #if defined(PETSC_HAVE_CUSP)
1295:   if (mat->valid_GPU_matrix != PETSC_CUSP_UNALLOCATED) {
1296:     mat->valid_GPU_matrix = PETSC_CUSP_CPU;
1297:   }
1298: #endif
1299: #if defined(PETSC_HAVE_VIENNACL)
1300:   if (mat->valid_GPU_matrix != PETSC_VIENNACL_UNALLOCATED) {
1301:     mat->valid_GPU_matrix = PETSC_VIENNACL_CPU;
1302:   }
1303: #endif
1304:   return(0);
1305: }

1309: /*@
1310:    MatSetValuesStencil - Inserts or adds a block of values into a matrix.
1311:      Using structured grid indexing

1313:    Not Collective

1315:    Input Parameters:
1316: +  mat - the matrix
1317: .  m - number of rows being entered
1318: .  idxm - grid coordinates (and component number when dof > 1) for matrix rows being entered
1319: .  n - number of columns being entered
1320: .  idxn - grid coordinates (and component number when dof > 1) for matrix columns being entered
1321: .  v - a logically two-dimensional array of values
1322: -  addv - either ADD_VALUES or INSERT_VALUES, where
1323:    ADD_VALUES adds values to any existing entries, and
1324:    INSERT_VALUES replaces existing entries with new values

1326:    Notes:
1327:    By default the values, v, are row-oriented.  See MatSetOption() for other options.

1329:    Calls to MatSetValuesStencil() with the INSERT_VALUES and ADD_VALUES
1330:    options cannot be mixed without intervening calls to the assembly
1331:    routines.

1333:    The grid coordinates are across the entire grid, not just the local portion

1335:    MatSetValuesStencil() uses 0-based row and column numbers in Fortran
1336:    as well as in C.

1338:    For setting/accessing vector values via array coordinates you can use the DMDAVecGetArray() routine

1340:    In order to use this routine you must either obtain the matrix with DMCreateMatrix()
1341:    or call MatSetLocalToGlobalMapping() and MatSetStencil() first.

1343:    The columns and rows in the stencil passed in MUST be contained within the
1344:    ghost region of the given process as set with DMDACreateXXX() or MatSetStencil(). For example,
1345:    if you create a DMDA with an overlap of one grid level and on a particular process its first
1346:    local nonghost x logical coordinate is 6 (so its first ghost x logical coordinate is 5) the
1347:    first i index you can use in your column and row indices in MatSetStencil() is 5.

1349:    In Fortran idxm and idxn should be declared as
1350: $     MatStencil idxm(4,m),idxn(4,n)
1351:    and the values inserted using
1352: $    idxm(MatStencil_i,1) = i
1353: $    idxm(MatStencil_j,1) = j
1354: $    idxm(MatStencil_k,1) = k
1355: $    idxm(MatStencil_c,1) = c
1356:    etc

1358:    For periodic boundary conditions use negative indices for values to the left (below 0; that are to be
1359:    obtained by wrapping values from right edge). For values to the right of the last entry using that index plus one
1360:    etc to obtain values that obtained by wrapping the values from the left edge. This does not work for anything but the
1361:    DM_BOUNDARY_PERIODIC boundary type.

1363:    For indices that don't mean anything for your case (like the k index when working in 2d) or the c index when you have
1364:    a single value per point) you can skip filling those indices.

1366:    Inspired by the structured grid interface to the HYPRE package
1367:    (http://www.llnl.gov/CASC/hypre)

1369:    Efficiency Alert:
1370:    The routine MatSetValuesBlockedStencil() may offer much better efficiency
1371:    for users of block sparse formats (MATSEQBAIJ and MATMPIBAIJ).

1373:    Level: beginner

1375:    Concepts: matrices^putting entries in

1377: .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal()
1378:           MatSetValues(), MatSetValuesBlockedStencil(), MatSetStencil(), DMCreateMatrix(), DMDAVecGetArray(), MatStencil
1379: @*/
1380: PetscErrorCode MatSetValuesStencil(Mat mat,PetscInt m,const MatStencil idxm[],PetscInt n,const MatStencil idxn[],const PetscScalar v[],InsertMode addv)
1381: {
1383:   PetscInt       buf[8192],*bufm=0,*bufn=0,*jdxm,*jdxn;
1384:   PetscInt       j,i,dim = mat->stencil.dim,*dims = mat->stencil.dims+1,tmp;
1385:   PetscInt       *starts = mat->stencil.starts,*dxm = (PetscInt*)idxm,*dxn = (PetscInt*)idxn,sdim = dim - (1 - (PetscInt)mat->stencil.noc);

1388:   if (!m || !n) return(0); /* no values to insert */

1395:   if ((m+n) <= (PetscInt)(sizeof(buf)/sizeof(PetscInt))) {
1396:     jdxm = buf; jdxn = buf+m;
1397:   } else {
1398:     PetscMalloc2(m,&bufm,n,&bufn);
1399:     jdxm = bufm; jdxn = bufn;
1400:   }
1401:   for (i=0; i<m; i++) {
1402:     for (j=0; j<3-sdim; j++) dxm++;
1403:     tmp = *dxm++ - starts[0];
1404:     for (j=0; j<dim-1; j++) {
1405:       if ((*dxm++ - starts[j+1]) < 0 || tmp < 0) tmp = -1;
1406:       else                                       tmp = tmp*dims[j] + *(dxm-1) - starts[j+1];
1407:     }
1408:     if (mat->stencil.noc) dxm++;
1409:     jdxm[i] = tmp;
1410:   }
1411:   for (i=0; i<n; i++) {
1412:     for (j=0; j<3-sdim; j++) dxn++;
1413:     tmp = *dxn++ - starts[0];
1414:     for (j=0; j<dim-1; j++) {
1415:       if ((*dxn++ - starts[j+1]) < 0 || tmp < 0) tmp = -1;
1416:       else                                       tmp = tmp*dims[j] + *(dxn-1) - starts[j+1];
1417:     }
1418:     if (mat->stencil.noc) dxn++;
1419:     jdxn[i] = tmp;
1420:   }
1421:   MatSetValuesLocal(mat,m,jdxm,n,jdxn,v,addv);
1422:   PetscFree2(bufm,bufn);
1423:   return(0);
1424: }

1428: /*@
1429:    MatSetValuesBlockedStencil - Inserts or adds a block of values into a matrix.
1430:      Using structured grid indexing

1432:    Not Collective

1434:    Input Parameters:
1435: +  mat - the matrix
1436: .  m - number of rows being entered
1437: .  idxm - grid coordinates for matrix rows being entered
1438: .  n - number of columns being entered
1439: .  idxn - grid coordinates for matrix columns being entered
1440: .  v - a logically two-dimensional array of values
1441: -  addv - either ADD_VALUES or INSERT_VALUES, where
1442:    ADD_VALUES adds values to any existing entries, and
1443:    INSERT_VALUES replaces existing entries with new values

1445:    Notes:
1446:    By default the values, v, are row-oriented and unsorted.
1447:    See MatSetOption() for other options.

1449:    Calls to MatSetValuesBlockedStencil() with the INSERT_VALUES and ADD_VALUES
1450:    options cannot be mixed without intervening calls to the assembly
1451:    routines.

1453:    The grid coordinates are across the entire grid, not just the local portion

1455:    MatSetValuesBlockedStencil() uses 0-based row and column numbers in Fortran
1456:    as well as in C.

1458:    For setting/accessing vector values via array coordinates you can use the DMDAVecGetArray() routine

1460:    In order to use this routine you must either obtain the matrix with DMCreateMatrix()
1461:    or call MatSetBlockSize(), MatSetLocalToGlobalMapping() and MatSetStencil() first.

1463:    The columns and rows in the stencil passed in MUST be contained within the
1464:    ghost region of the given process as set with DMDACreateXXX() or MatSetStencil(). For example,
1465:    if you create a DMDA with an overlap of one grid level and on a particular process its first
1466:    local nonghost x logical coordinate is 6 (so its first ghost x logical coordinate is 5) the
1467:    first i index you can use in your column and row indices in MatSetStencil() is 5.

1469:    In Fortran idxm and idxn should be declared as
1470: $     MatStencil idxm(4,m),idxn(4,n)
1471:    and the values inserted using
1472: $    idxm(MatStencil_i,1) = i
1473: $    idxm(MatStencil_j,1) = j
1474: $    idxm(MatStencil_k,1) = k
1475:    etc

1477:    Negative indices may be passed in idxm and idxn, these rows and columns are
1478:    simply ignored. This allows easily inserting element stiffness matrices
1479:    with homogeneous Dirchlet boundary conditions that you don't want represented
1480:    in the matrix.

1482:    Inspired by the structured grid interface to the HYPRE package
1483:    (http://www.llnl.gov/CASC/hypre)

1485:    Level: beginner

1487:    Concepts: matrices^putting entries in

1489: .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal()
1490:           MatSetValues(), MatSetValuesStencil(), MatSetStencil(), DMCreateMatrix(), DMDAVecGetArray(), MatStencil,
1491:           MatSetBlockSize(), MatSetLocalToGlobalMapping()
1492: @*/
1493: PetscErrorCode MatSetValuesBlockedStencil(Mat mat,PetscInt m,const MatStencil idxm[],PetscInt n,const MatStencil idxn[],const PetscScalar v[],InsertMode addv)
1494: {
1496:   PetscInt       buf[8192],*bufm=0,*bufn=0,*jdxm,*jdxn;
1497:   PetscInt       j,i,dim = mat->stencil.dim,*dims = mat->stencil.dims+1,tmp;
1498:   PetscInt       *starts = mat->stencil.starts,*dxm = (PetscInt*)idxm,*dxn = (PetscInt*)idxn,sdim = dim - (1 - (PetscInt)mat->stencil.noc);

1501:   if (!m || !n) return(0); /* no values to insert */

1508:   if ((m+n) <= (PetscInt)(sizeof(buf)/sizeof(PetscInt))) {
1509:     jdxm = buf; jdxn = buf+m;
1510:   } else {
1511:     PetscMalloc2(m,&bufm,n,&bufn);
1512:     jdxm = bufm; jdxn = bufn;
1513:   }
1514:   for (i=0; i<m; i++) {
1515:     for (j=0; j<3-sdim; j++) dxm++;
1516:     tmp = *dxm++ - starts[0];
1517:     for (j=0; j<sdim-1; j++) {
1518:       if ((*dxm++ - starts[j+1]) < 0 || tmp < 0) tmp = -1;
1519:       else                                       tmp = tmp*dims[j] + *(dxm-1) - starts[j+1];
1520:     }
1521:     dxm++;
1522:     jdxm[i] = tmp;
1523:   }
1524:   for (i=0; i<n; i++) {
1525:     for (j=0; j<3-sdim; j++) dxn++;
1526:     tmp = *dxn++ - starts[0];
1527:     for (j=0; j<sdim-1; j++) {
1528:       if ((*dxn++ - starts[j+1]) < 0 || tmp < 0) tmp = -1;
1529:       else                                       tmp = tmp*dims[j] + *(dxn-1) - starts[j+1];
1530:     }
1531:     dxn++;
1532:     jdxn[i] = tmp;
1533:   }
1534:   MatSetValuesBlockedLocal(mat,m,jdxm,n,jdxn,v,addv);
1535:   PetscFree2(bufm,bufn);
1536: #if defined(PETSC_HAVE_CUSP)
1537:   if (mat->valid_GPU_matrix != PETSC_CUSP_UNALLOCATED) {
1538:     mat->valid_GPU_matrix = PETSC_CUSP_CPU;
1539:   }
1540: #endif
1541: #if defined(PETSC_HAVE_VIENNACL)
1542:   if (mat->valid_GPU_matrix != PETSC_VIENNACL_UNALLOCATED) {
1543:     mat->valid_GPU_matrix = PETSC_VIENNACL_CPU;
1544:   }
1545: #endif
1546:   return(0);
1547: }

1551: /*@
1552:    MatSetStencil - Sets the grid information for setting values into a matrix via
1553:         MatSetValuesStencil()

1555:    Not Collective

1557:    Input Parameters:
1558: +  mat - the matrix
1559: .  dim - dimension of the grid 1, 2, or 3
1560: .  dims - number of grid points in x, y, and z direction, including ghost points on your processor
1561: .  starts - starting point of ghost nodes on your processor in x, y, and z direction
1562: -  dof - number of degrees of freedom per node


1565:    Inspired by the structured grid interface to the HYPRE package
1566:    (www.llnl.gov/CASC/hyper)

1568:    For matrices generated with DMCreateMatrix() this routine is automatically called and so not needed by the
1569:    user.

1571:    Level: beginner

1573:    Concepts: matrices^putting entries in

1575: .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal()
1576:           MatSetValues(), MatSetValuesBlockedStencil(), MatSetValuesStencil()
1577: @*/
1578: PetscErrorCode MatSetStencil(Mat mat,PetscInt dim,const PetscInt dims[],const PetscInt starts[],PetscInt dof)
1579: {
1580:   PetscInt i;


1587:   mat->stencil.dim = dim + (dof > 1);
1588:   for (i=0; i<dim; i++) {
1589:     mat->stencil.dims[i]   = dims[dim-i-1];      /* copy the values in backwards */
1590:     mat->stencil.starts[i] = starts[dim-i-1];
1591:   }
1592:   mat->stencil.dims[dim]   = dof;
1593:   mat->stencil.starts[dim] = 0;
1594:   mat->stencil.noc         = (PetscBool)(dof == 1);
1595:   return(0);
1596: }

1600: /*@
1601:    MatSetValuesBlocked - Inserts or adds a block of values into a matrix.

1603:    Not Collective

1605:    Input Parameters:
1606: +  mat - the matrix
1607: .  v - a logically two-dimensional array of values
1608: .  m, idxm - the number of block rows and their global block indices
1609: .  n, idxn - the number of block columns and their global block indices
1610: -  addv - either ADD_VALUES or INSERT_VALUES, where
1611:    ADD_VALUES adds values to any existing entries, and
1612:    INSERT_VALUES replaces existing entries with new values

1614:    Notes:
1615:    If you create the matrix yourself (that is not with a call to DMCreateMatrix()) then you MUST call
1616:    MatXXXXSetPreallocation() or MatSetUp() before using this routine.

1618:    The m and n count the NUMBER of blocks in the row direction and column direction,
1619:    NOT the total number of rows/columns; for example, if the block size is 2 and
1620:    you are passing in values for rows 2,3,4,5  then m would be 2 (not 4).
1621:    The values in idxm would be 1 2; that is the first index for each block divided by
1622:    the block size.

1624:    Note that you must call MatSetBlockSize() when constructing this matrix (before
1625:    preallocating it).

1627:    By default the values, v, are row-oriented, so the layout of
1628:    v is the same as for MatSetValues(). See MatSetOption() for other options.

1630:    Calls to MatSetValuesBlocked() with the INSERT_VALUES and ADD_VALUES
1631:    options cannot be mixed without intervening calls to the assembly
1632:    routines.

1634:    MatSetValuesBlocked() uses 0-based row and column numbers in Fortran
1635:    as well as in C.

1637:    Negative indices may be passed in idxm and idxn, these rows and columns are
1638:    simply ignored. This allows easily inserting element stiffness matrices
1639:    with homogeneous Dirchlet boundary conditions that you don't want represented
1640:    in the matrix.

1642:    Each time an entry is set within a sparse matrix via MatSetValues(),
1643:    internal searching must be done to determine where to place the the
1644:    data in the matrix storage space.  By instead inserting blocks of
1645:    entries via MatSetValuesBlocked(), the overhead of matrix assembly is
1646:    reduced.

1648:    Example:
1649: $   Suppose m=n=2 and block size(bs) = 2 The array is
1650: $
1651: $   1  2  | 3  4
1652: $   5  6  | 7  8
1653: $   - - - | - - -
1654: $   9  10 | 11 12
1655: $   13 14 | 15 16
1656: $
1657: $   v[] should be passed in like
1658: $   v[] = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
1659: $
1660: $  If you are not using row oriented storage of v (that is you called MatSetOption(mat,MAT_ROW_ORIENTED,PETSC_FALSE)) then
1661: $   v[] = [1,5,9,13,2,6,10,14,3,7,11,15,4,8,12,16]

1663:    Level: intermediate

1665:    Concepts: matrices^putting entries in blocked

1667: .seealso: MatSetBlockSize(), MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetValuesBlockedLocal()
1668: @*/
1669: PetscErrorCode MatSetValuesBlocked(Mat mat,PetscInt m,const PetscInt idxm[],PetscInt n,const PetscInt idxn[],const PetscScalar v[],InsertMode addv)
1670: {

1676:   if (!m || !n) return(0); /* no values to insert */
1680:   MatCheckPreallocated(mat,1);
1681:   if (mat->insertmode == NOT_SET_VALUES) {
1682:     mat->insertmode = addv;
1683:   }
1684: #if defined(PETSC_USE_DEBUG)
1685:   else if (mat->insertmode != addv) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values");
1686:   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1687:   if (!mat->ops->setvaluesblocked && !mat->ops->setvalues) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
1688: #endif

1690:   if (mat->assembled) {
1691:     mat->was_assembled = PETSC_TRUE;
1692:     mat->assembled     = PETSC_FALSE;
1693:   }
1694:   PetscLogEventBegin(MAT_SetValues,mat,0,0,0);
1695:   if (mat->ops->setvaluesblocked) {
1696:     (*mat->ops->setvaluesblocked)(mat,m,idxm,n,idxn,v,addv);
1697:   } else {
1698:     PetscInt buf[8192],*bufr=0,*bufc=0,*iidxm,*iidxn;
1699:     PetscInt i,j,bs,cbs;
1700:     MatGetBlockSizes(mat,&bs,&cbs);
1701:     if (m*bs+n*cbs <= (PetscInt)(sizeof(buf)/sizeof(PetscInt))) {
1702:       iidxm = buf; iidxn = buf + m*bs;
1703:     } else {
1704:       PetscMalloc2(m*bs,&bufr,n*cbs,&bufc);
1705:       iidxm = bufr; iidxn = bufc;
1706:     }
1707:     for (i=0; i<m; i++) {
1708:       for (j=0; j<bs; j++) {
1709:         iidxm[i*bs+j] = bs*idxm[i] + j;
1710:       }
1711:     }
1712:     for (i=0; i<n; i++) {
1713:       for (j=0; j<cbs; j++) {
1714:         iidxn[i*cbs+j] = cbs*idxn[i] + j;
1715:       }
1716:     }
1717:     MatSetValues(mat,m*bs,iidxm,n*cbs,iidxn,v,addv);
1718:     PetscFree2(bufr,bufc);
1719:   }
1720:   PetscLogEventEnd(MAT_SetValues,mat,0,0,0);
1721: #if defined(PETSC_HAVE_CUSP)
1722:   if (mat->valid_GPU_matrix != PETSC_CUSP_UNALLOCATED) {
1723:     mat->valid_GPU_matrix = PETSC_CUSP_CPU;
1724:   }
1725: #endif
1726: #if defined(PETSC_HAVE_VIENNACL)
1727:   if (mat->valid_GPU_matrix != PETSC_VIENNACL_UNALLOCATED) {
1728:     mat->valid_GPU_matrix = PETSC_VIENNACL_CPU;
1729:   }
1730: #endif
1731:   return(0);
1732: }

1736: /*@
1737:    MatGetValues - Gets a block of values from a matrix.

1739:    Not Collective; currently only returns a local block

1741:    Input Parameters:
1742: +  mat - the matrix
1743: .  v - a logically two-dimensional array for storing the values
1744: .  m, idxm - the number of rows and their global indices
1745: -  n, idxn - the number of columns and their global indices

1747:    Notes:
1748:    The user must allocate space (m*n PetscScalars) for the values, v.
1749:    The values, v, are then returned in a row-oriented format,
1750:    analogous to that used by default in MatSetValues().

1752:    MatGetValues() uses 0-based row and column numbers in
1753:    Fortran as well as in C.

1755:    MatGetValues() requires that the matrix has been assembled
1756:    with MatAssemblyBegin()/MatAssemblyEnd().  Thus, calls to
1757:    MatSetValues() and MatGetValues() CANNOT be made in succession
1758:    without intermediate matrix assembly.

1760:    Negative row or column indices will be ignored and those locations in v[] will be
1761:    left unchanged.

1763:    Level: advanced

1765:    Concepts: matrices^accessing values

1767: .seealso: MatGetRow(), MatGetSubMatrices(), MatSetValues()
1768: @*/
1769: PetscErrorCode MatGetValues(Mat mat,PetscInt m,const PetscInt idxm[],PetscInt n,const PetscInt idxn[],PetscScalar v[])
1770: {

1776:   if (!m || !n) return(0);
1780:   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
1781:   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1782:   if (!mat->ops->getvalues) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
1783:   MatCheckPreallocated(mat,1);

1785:   PetscLogEventBegin(MAT_GetValues,mat,0,0,0);
1786:   (*mat->ops->getvalues)(mat,m,idxm,n,idxn,v);
1787:   PetscLogEventEnd(MAT_GetValues,mat,0,0,0);
1788:   return(0);
1789: }

1793: /*@
1794:   MatSetValuesBatch - Adds (ADD_VALUES) many blocks of values into a matrix at once. The blocks must all be square and
1795:   the same size. Currently, this can only be called once and creates the given matrix.

1797:   Not Collective

1799:   Input Parameters:
1800: + mat - the matrix
1801: . nb - the number of blocks
1802: . bs - the number of rows (and columns) in each block
1803: . rows - a concatenation of the rows for each block
1804: - v - a concatenation of logically two-dimensional arrays of values

1806:   Notes:
1807:   In the future, we will extend this routine to handle rectangular blocks, and to allow multiple calls for a given matrix.

1809:   Level: advanced

1811:   Concepts: matrices^putting entries in

1813: .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(),
1814:           InsertMode, INSERT_VALUES, ADD_VALUES, MatSetValues()
1815: @*/
1816: PetscErrorCode MatSetValuesBatch(Mat mat, PetscInt nb, PetscInt bs, PetscInt rows[], const PetscScalar v[])
1817: {

1825: #if defined(PETSC_USE_DEBUG)
1826:   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1827: #endif

1829:   PetscLogEventBegin(MAT_SetValuesBatch,mat,0,0,0);
1830:   if (mat->ops->setvaluesbatch) {
1831:     (*mat->ops->setvaluesbatch)(mat,nb,bs,rows,v);
1832:   } else {
1833:     PetscInt b;
1834:     for (b = 0; b < nb; ++b) {
1835:       MatSetValues(mat, bs, &rows[b*bs], bs, &rows[b*bs], &v[b*bs*bs], ADD_VALUES);
1836:     }
1837:   }
1838:   PetscLogEventEnd(MAT_SetValuesBatch,mat,0,0,0);
1839:   return(0);
1840: }

1844: /*@
1845:    MatSetLocalToGlobalMapping - Sets a local-to-global numbering for use by
1846:    the routine MatSetValuesLocal() to allow users to insert matrix entries
1847:    using a local (per-processor) numbering.

1849:    Not Collective

1851:    Input Parameters:
1852: +  x - the matrix
1853: .  rmapping - row mapping created with ISLocalToGlobalMappingCreate()   or ISLocalToGlobalMappingCreateIS()
1854: - cmapping - column mapping

1856:    Level: intermediate

1858:    Concepts: matrices^local to global mapping
1859:    Concepts: local to global mapping^for matrices

1861: .seealso:  MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetValuesLocal()
1862: @*/
1863: PetscErrorCode MatSetLocalToGlobalMapping(Mat x,ISLocalToGlobalMapping rmapping,ISLocalToGlobalMapping cmapping)
1864: {


1873:   if (x->ops->setlocaltoglobalmapping) {
1874:     (*x->ops->setlocaltoglobalmapping)(x,rmapping,cmapping);
1875:   } else {
1876:     PetscLayoutSetISLocalToGlobalMapping(x->rmap,rmapping);
1877:     PetscLayoutSetISLocalToGlobalMapping(x->cmap,cmapping);
1878:   }
1879:   return(0);
1880: }


1885: /*@
1886:    MatGetLocalToGlobalMapping - Gets the local-to-global numbering set by MatSetLocalToGlobalMapping()

1888:    Not Collective

1890:    Input Parameters:
1891: .  A - the matrix

1893:    Output Parameters:
1894: + rmapping - row mapping
1895: - cmapping - column mapping

1897:    Level: advanced

1899:    Concepts: matrices^local to global mapping
1900:    Concepts: local to global mapping^for matrices

1902: .seealso:  MatSetValuesLocal()
1903: @*/
1904: PetscErrorCode MatGetLocalToGlobalMapping(Mat A,ISLocalToGlobalMapping *rmapping,ISLocalToGlobalMapping *cmapping)
1905: {
1911:   if (rmapping) *rmapping = A->rmap->mapping;
1912:   if (cmapping) *cmapping = A->cmap->mapping;
1913:   return(0);
1914: }

1918: /*@
1919:    MatGetLayouts - Gets the PetscLayout objects for rows and columns

1921:    Not Collective

1923:    Input Parameters:
1924: .  A - the matrix

1926:    Output Parameters:
1927: + rmap - row layout
1928: - cmap - column layout

1930:    Level: advanced

1932: .seealso:  MatCreateVecs(), MatGetLocalToGlobalMapping()
1933: @*/
1934: PetscErrorCode MatGetLayouts(Mat A,PetscLayout *rmap,PetscLayout *cmap)
1935: {
1941:   if (rmap) *rmap = A->rmap;
1942:   if (cmap) *cmap = A->cmap;
1943:   return(0);
1944: }

1948: /*@
1949:    MatSetValuesLocal - Inserts or adds values into certain locations of a matrix,
1950:    using a local ordering of the nodes.

1952:    Not Collective

1954:    Input Parameters:
1955: +  x - the matrix
1956: .  nrow, irow - number of rows and their local indices
1957: .  ncol, icol - number of columns and their local indices
1958: .  y -  a logically two-dimensional array of values
1959: -  addv - either INSERT_VALUES or ADD_VALUES, where
1960:    ADD_VALUES adds values to any existing entries, and
1961:    INSERT_VALUES replaces existing entries with new values

1963:    Notes:
1964:    If you create the matrix yourself (that is not with a call to DMCreateMatrix()) then you MUST call MatXXXXSetPreallocation() or
1965:       MatSetUp() before using this routine

1967:    If you create the matrix yourself (that is not with a call to DMCreateMatrix()) then you MUST call MatSetLocalToGlobalMapping() before using this routine

1969:    Calls to MatSetValuesLocal() with the INSERT_VALUES and ADD_VALUES
1970:    options cannot be mixed without intervening calls to the assembly
1971:    routines.

1973:    These values may be cached, so MatAssemblyBegin() and MatAssemblyEnd()
1974:    MUST be called after all calls to MatSetValuesLocal() have been completed.

1976:    Level: intermediate

1978:    Concepts: matrices^putting entries in with local numbering

1980: .seealso:  MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetLocalToGlobalMapping(),
1981:            MatSetValueLocal()
1982: @*/
1983: PetscErrorCode MatSetValuesLocal(Mat mat,PetscInt nrow,const PetscInt irow[],PetscInt ncol,const PetscInt icol[],const PetscScalar y[],InsertMode addv)
1984: {

1990:   MatCheckPreallocated(mat,1);
1991:   if (!nrow || !ncol) return(0); /* no values to insert */
1995:   if (mat->insertmode == NOT_SET_VALUES) {
1996:     mat->insertmode = addv;
1997:   }
1998: #if defined(PETSC_USE_DEBUG)
1999:   else if (mat->insertmode != addv) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values");
2000:   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2001:   if (!mat->ops->setvalueslocal && !mat->ops->setvalues) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
2002: #endif

2004:   if (mat->assembled) {
2005:     mat->was_assembled = PETSC_TRUE;
2006:     mat->assembled     = PETSC_FALSE;
2007:   }
2008:   PetscLogEventBegin(MAT_SetValues,mat,0,0,0);
2009:   if (mat->ops->setvalueslocal) {
2010:     (*mat->ops->setvalueslocal)(mat,nrow,irow,ncol,icol,y,addv);
2011:   } else {
2012:     PetscInt buf[8192],*bufr=0,*bufc=0,*irowm,*icolm;
2013:     if ((nrow+ncol) <= (PetscInt)(sizeof(buf)/sizeof(PetscInt))) {
2014:       irowm = buf; icolm = buf+nrow;
2015:     } else {
2016:       PetscMalloc2(nrow,&bufr,ncol,&bufc);
2017:       irowm = bufr; icolm = bufc;
2018:     }
2019:     ISLocalToGlobalMappingApply(mat->rmap->mapping,nrow,irow,irowm);
2020:     ISLocalToGlobalMappingApply(mat->cmap->mapping,ncol,icol,icolm);
2021:     MatSetValues(mat,nrow,irowm,ncol,icolm,y,addv);
2022:     PetscFree2(bufr,bufc);
2023:   }
2024:   PetscLogEventEnd(MAT_SetValues,mat,0,0,0);
2025: #if defined(PETSC_HAVE_CUSP)
2026:   if (mat->valid_GPU_matrix != PETSC_CUSP_UNALLOCATED) {
2027:     mat->valid_GPU_matrix = PETSC_CUSP_CPU;
2028:   }
2029: #endif
2030: #if defined(PETSC_HAVE_VIENNACL)
2031:   if (mat->valid_GPU_matrix != PETSC_VIENNACL_UNALLOCATED) {
2032:     mat->valid_GPU_matrix = PETSC_VIENNACL_CPU;
2033:   }
2034: #endif
2035:   return(0);
2036: }

2040: /*@
2041:    MatSetValuesBlockedLocal - Inserts or adds values into certain locations of a matrix,
2042:    using a local ordering of the nodes a block at a time.

2044:    Not Collective

2046:    Input Parameters:
2047: +  x - the matrix
2048: .  nrow, irow - number of rows and their local indices
2049: .  ncol, icol - number of columns and their local indices
2050: .  y -  a logically two-dimensional array of values
2051: -  addv - either INSERT_VALUES or ADD_VALUES, where
2052:    ADD_VALUES adds values to any existing entries, and
2053:    INSERT_VALUES replaces existing entries with new values

2055:    Notes:
2056:    If you create the matrix yourself (that is not with a call to DMCreateMatrix()) then you MUST call MatXXXXSetPreallocation() or
2057:       MatSetUp() before using this routine

2059:    If you create the matrix yourself (that is not with a call to DMCreateMatrix()) then you MUST call MatSetBlockSize() and MatSetLocalToGlobalMapping()
2060:       before using this routineBefore calling MatSetValuesLocal(), the user must first set the

2062:    Calls to MatSetValuesBlockedLocal() with the INSERT_VALUES and ADD_VALUES
2063:    options cannot be mixed without intervening calls to the assembly
2064:    routines.

2066:    These values may be cached, so MatAssemblyBegin() and MatAssemblyEnd()
2067:    MUST be called after all calls to MatSetValuesBlockedLocal() have been completed.

2069:    Level: intermediate

2071:    Concepts: matrices^putting blocked values in with local numbering

2073: .seealso:  MatSetBlockSize(), MatSetLocalToGlobalMapping(), MatAssemblyBegin(), MatAssemblyEnd(),
2074:            MatSetValuesLocal(),  MatSetValuesBlocked()
2075: @*/
2076: PetscErrorCode MatSetValuesBlockedLocal(Mat mat,PetscInt nrow,const PetscInt irow[],PetscInt ncol,const PetscInt icol[],const PetscScalar y[],InsertMode addv)
2077: {

2083:   MatCheckPreallocated(mat,1);
2084:   if (!nrow || !ncol) return(0); /* no values to insert */
2088:   if (mat->insertmode == NOT_SET_VALUES) {
2089:     mat->insertmode = addv;
2090:   }
2091: #if defined(PETSC_USE_DEBUG)
2092:   else if (mat->insertmode != addv) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values");
2093:   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2094:   if (!mat->ops->setvaluesblockedlocal && !mat->ops->setvaluesblocked && !mat->ops->setvalueslocal && !mat->ops->setvalues) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
2095: #endif

2097:   if (mat->assembled) {
2098:     mat->was_assembled = PETSC_TRUE;
2099:     mat->assembled     = PETSC_FALSE;
2100:   }
2101:   PetscLogEventBegin(MAT_SetValues,mat,0,0,0);
2102:   if (mat->ops->setvaluesblockedlocal) {
2103:     (*mat->ops->setvaluesblockedlocal)(mat,nrow,irow,ncol,icol,y,addv);
2104:   } else {
2105:     PetscInt buf[8192],*bufr=0,*bufc=0,*irowm,*icolm;
2106:     if ((nrow+ncol) <= (PetscInt)(sizeof(buf)/sizeof(PetscInt))) {
2107:       irowm = buf; icolm = buf + nrow;
2108:     } else {
2109:       PetscMalloc2(nrow,&bufr,ncol,&bufc);
2110:       irowm = bufr; icolm = bufc;
2111:     }
2112:     ISLocalToGlobalMappingApplyBlock(mat->rmap->mapping,nrow,irow,irowm);
2113:     ISLocalToGlobalMappingApplyBlock(mat->cmap->mapping,ncol,icol,icolm);
2114:     MatSetValuesBlocked(mat,nrow,irowm,ncol,icolm,y,addv);
2115:     PetscFree2(bufr,bufc);
2116:   }
2117:   PetscLogEventEnd(MAT_SetValues,mat,0,0,0);
2118: #if defined(PETSC_HAVE_CUSP)
2119:   if (mat->valid_GPU_matrix != PETSC_CUSP_UNALLOCATED) {
2120:     mat->valid_GPU_matrix = PETSC_CUSP_CPU;
2121:   }
2122: #endif
2123: #if defined(PETSC_HAVE_VIENNACL)
2124:   if (mat->valid_GPU_matrix != PETSC_VIENNACL_UNALLOCATED) {
2125:     mat->valid_GPU_matrix = PETSC_VIENNACL_CPU;
2126:   }
2127: #endif
2128:   return(0);
2129: }

2133: /*@
2134:    MatMultDiagonalBlock - Computes the matrix-vector product, y = Dx. Where D is defined by the inode or block structure of the diagonal

2136:    Collective on Mat and Vec

2138:    Input Parameters:
2139: +  mat - the matrix
2140: -  x   - the vector to be multiplied

2142:    Output Parameters:
2143: .  y - the result

2145:    Notes:
2146:    The vectors x and y cannot be the same.  I.e., one cannot
2147:    call MatMult(A,y,y).

2149:    Level: developer

2151:    Concepts: matrix-vector product

2153: .seealso: MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd()
2154: @*/
2155: PetscErrorCode MatMultDiagonalBlock(Mat mat,Vec x,Vec y)
2156: {


2165:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2166:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2167:   if (x == y) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors");
2168:   MatCheckPreallocated(mat,1);

2170:   if (!mat->ops->multdiagonalblock) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"This matrix type does not have a multiply defined");
2171:   (*mat->ops->multdiagonalblock)(mat,x,y);
2172:   PetscObjectStateIncrease((PetscObject)y);
2173:   return(0);
2174: }

2176: /* --------------------------------------------------------*/
2179: /*@
2180:    MatMult - Computes the matrix-vector product, y = Ax.

2182:    Neighbor-wise Collective on Mat and Vec

2184:    Input Parameters:
2185: +  mat - the matrix
2186: -  x   - the vector to be multiplied

2188:    Output Parameters:
2189: .  y - the result

2191:    Notes:
2192:    The vectors x and y cannot be the same.  I.e., one cannot
2193:    call MatMult(A,y,y).

2195:    Level: beginner

2197:    Concepts: matrix-vector product

2199: .seealso: MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd()
2200: @*/
2201: PetscErrorCode MatMult(Mat mat,Vec x,Vec y)
2202: {

2210:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2211:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2212:   if (x == y) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors");
2213: #if !defined(PETSC_HAVE_CONSTRAINTS)
2214:   if (mat->cmap->N != x->map->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %D %D",mat->cmap->N,x->map->N);
2215:   if (mat->rmap->N != y->map->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %D %D",mat->rmap->N,y->map->N);
2216:   if (mat->rmap->n != y->map->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: local dim %D %D",mat->rmap->n,y->map->n);
2217: #endif
2218:   VecLocked(y,3);
2219:   if (mat->erroriffailure) {VecValidValues(x,2,PETSC_TRUE);}
2220:   MatCheckPreallocated(mat,1);

2222:   VecLockPush(x);
2223:   if (!mat->ops->mult) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"This matrix type does not have a multiply defined");
2224:   PetscLogEventBegin(MAT_Mult,mat,x,y,0);
2225:   (*mat->ops->mult)(mat,x,y);
2226:   PetscLogEventEnd(MAT_Mult,mat,x,y,0);
2227:   if (mat->erroriffailure) {VecValidValues(y,3,PETSC_FALSE);}
2228:   VecLockPop(x);
2229:   return(0);
2230: }

2234: /*@
2235:    MatMultTranspose - Computes matrix transpose times a vector.

2237:    Neighbor-wise Collective on Mat and Vec

2239:    Input Parameters:
2240: +  mat - the matrix
2241: -  x   - the vector to be multilplied

2243:    Output Parameters:
2244: .  y - the result

2246:    Notes:
2247:    The vectors x and y cannot be the same.  I.e., one cannot
2248:    call MatMultTranspose(A,y,y).

2250:    For complex numbers this does NOT compute the Hermitian (complex conjugate) transpose multiple,
2251:    use MatMultHermitianTranspose()

2253:    Level: beginner

2255:    Concepts: matrix vector product^transpose

2257: .seealso: MatMult(), MatMultAdd(), MatMultTransposeAdd(), MatMultHermitianTranspose(), MatTranspose()
2258: @*/
2259: PetscErrorCode MatMultTranspose(Mat mat,Vec x,Vec y)
2260: {


2269:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2270:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2271:   if (x == y) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors");
2272: #if !defined(PETSC_HAVE_CONSTRAINTS)
2273:   if (mat->rmap->N != x->map->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %D %D",mat->rmap->N,x->map->N);
2274:   if (mat->cmap->N != y->map->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %D %D",mat->cmap->N,y->map->N);
2275: #endif
2276:   if (mat->erroriffailure) {VecValidValues(x,2,PETSC_TRUE);}
2277:   MatCheckPreallocated(mat,1);

2279:   if (!mat->ops->multtranspose) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"This matrix type does not have a multiply tranpose defined");
2280:   PetscLogEventBegin(MAT_MultTranspose,mat,x,y,0);
2281:   VecLockPush(x);
2282:   (*mat->ops->multtranspose)(mat,x,y);
2283:   VecLockPop(x);
2284:   PetscLogEventEnd(MAT_MultTranspose,mat,x,y,0);
2285:   PetscObjectStateIncrease((PetscObject)y);
2286:   if (mat->erroriffailure) {VecValidValues(y,3,PETSC_FALSE);}
2287:   return(0);
2288: }

2292: /*@
2293:    MatMultHermitianTranspose - Computes matrix Hermitian transpose times a vector.

2295:    Neighbor-wise Collective on Mat and Vec

2297:    Input Parameters:
2298: +  mat - the matrix
2299: -  x   - the vector to be multilplied

2301:    Output Parameters:
2302: .  y - the result

2304:    Notes:
2305:    The vectors x and y cannot be the same.  I.e., one cannot
2306:    call MatMultHermitianTranspose(A,y,y).

2308:    Also called the conjugate transpose, complex conjugate transpose, or adjoint.

2310:    For real numbers MatMultTranspose() and MatMultHermitianTranspose() are identical.

2312:    Level: beginner

2314:    Concepts: matrix vector product^transpose

2316: .seealso: MatMult(), MatMultAdd(), MatMultHermitianTransposeAdd(), MatMultTranspose()
2317: @*/
2318: PetscErrorCode MatMultHermitianTranspose(Mat mat,Vec x,Vec y)
2319: {
2321:   Vec            w;


2329:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2330:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2331:   if (x == y) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors");
2332: #if !defined(PETSC_HAVE_CONSTRAINTS)
2333:   if (mat->rmap->N != x->map->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %D %D",mat->rmap->N,x->map->N);
2334:   if (mat->cmap->N != y->map->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %D %D",mat->cmap->N,y->map->N);
2335: #endif
2336:   MatCheckPreallocated(mat,1);

2338:   PetscLogEventBegin(MAT_MultHermitianTranspose,mat,x,y,0);
2339:   if (mat->ops->multhermitiantranspose) {
2340:     VecLockPush(x);
2341:     (*mat->ops->multhermitiantranspose)(mat,x,y);
2342:     VecLockPop(x);
2343:   } else {
2344:     VecDuplicate(x,&w);
2345:     VecCopy(x,w);
2346:     VecConjugate(w);
2347:     MatMultTranspose(mat,w,y);
2348:     VecDestroy(&w);
2349:     VecConjugate(y);
2350:   }
2351:   PetscLogEventEnd(MAT_MultHermitianTranspose,mat,x,y,0);
2352:   PetscObjectStateIncrease((PetscObject)y);
2353:   return(0);
2354: }

2358: /*@
2359:     MatMultAdd -  Computes v3 = v2 + A * v1.

2361:     Neighbor-wise Collective on Mat and Vec

2363:     Input Parameters:
2364: +   mat - the matrix
2365: -   v1, v2 - the vectors

2367:     Output Parameters:
2368: .   v3 - the result

2370:     Notes:
2371:     The vectors v1 and v3 cannot be the same.  I.e., one cannot
2372:     call MatMultAdd(A,v1,v2,v1).

2374:     Level: beginner

2376:     Concepts: matrix vector product^addition

2378: .seealso: MatMultTranspose(), MatMult(), MatMultTransposeAdd()
2379: @*/
2380: PetscErrorCode MatMultAdd(Mat mat,Vec v1,Vec v2,Vec v3)
2381: {


2391:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2392:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2393:   if (mat->cmap->N != v1->map->N) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec v1: global dim %D %D",mat->cmap->N,v1->map->N);
2394:   /* if (mat->rmap->N != v2->map->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec v2: global dim %D %D",mat->rmap->N,v2->map->N);
2395:      if (mat->rmap->N != v3->map->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec v3: global dim %D %D",mat->rmap->N,v3->map->N); */
2396:   if (mat->rmap->n != v3->map->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec v3: local dim %D %D",mat->rmap->n,v3->map->n);
2397:   if (mat->rmap->n != v2->map->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec v2: local dim %D %D",mat->rmap->n,v2->map->n);
2398:   if (v1 == v3) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"v1 and v3 must be different vectors");
2399:   MatCheckPreallocated(mat,1);

2401:   if (!mat->ops->multadd) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"No MatMultAdd() for matrix type '%s'",((PetscObject)mat)->type_name);
2402:   PetscLogEventBegin(MAT_MultAdd,mat,v1,v2,v3);
2403:   VecLockPush(v1);
2404:   (*mat->ops->multadd)(mat,v1,v2,v3);
2405:   VecLockPop(v1);
2406:   PetscLogEventEnd(MAT_MultAdd,mat,v1,v2,v3);
2407:   PetscObjectStateIncrease((PetscObject)v3);
2408:   return(0);
2409: }

2413: /*@
2414:    MatMultTransposeAdd - Computes v3 = v2 + A' * v1.

2416:    Neighbor-wise Collective on Mat and Vec

2418:    Input Parameters:
2419: +  mat - the matrix
2420: -  v1, v2 - the vectors

2422:    Output Parameters:
2423: .  v3 - the result

2425:    Notes:
2426:    The vectors v1 and v3 cannot be the same.  I.e., one cannot
2427:    call MatMultTransposeAdd(A,v1,v2,v1).

2429:    Level: beginner

2431:    Concepts: matrix vector product^transpose and addition

2433: .seealso: MatMultTranspose(), MatMultAdd(), MatMult()
2434: @*/
2435: PetscErrorCode MatMultTransposeAdd(Mat mat,Vec v1,Vec v2,Vec v3)
2436: {


2446:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2447:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2448:   if (!mat->ops->multtransposeadd) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
2449:   if (v1 == v3) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"v1 and v3 must be different vectors");
2450:   if (mat->rmap->N != v1->map->N) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec v1: global dim %D %D",mat->rmap->N,v1->map->N);
2451:   if (mat->cmap->N != v2->map->N) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec v2: global dim %D %D",mat->cmap->N,v2->map->N);
2452:   if (mat->cmap->N != v3->map->N) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec v3: global dim %D %D",mat->cmap->N,v3->map->N);
2453:   MatCheckPreallocated(mat,1);

2455:   PetscLogEventBegin(MAT_MultTransposeAdd,mat,v1,v2,v3);
2456:   VecLockPush(v1);
2457:   (*mat->ops->multtransposeadd)(mat,v1,v2,v3);
2458:   VecLockPop(v1);
2459:   PetscLogEventEnd(MAT_MultTransposeAdd,mat,v1,v2,v3);
2460:   PetscObjectStateIncrease((PetscObject)v3);
2461:   return(0);
2462: }

2466: /*@
2467:    MatMultHermitianTransposeAdd - Computes v3 = v2 + A^H * v1.

2469:    Neighbor-wise Collective on Mat and Vec

2471:    Input Parameters:
2472: +  mat - the matrix
2473: -  v1, v2 - the vectors

2475:    Output Parameters:
2476: .  v3 - the result

2478:    Notes:
2479:    The vectors v1 and v3 cannot be the same.  I.e., one cannot
2480:    call MatMultHermitianTransposeAdd(A,v1,v2,v1).

2482:    Level: beginner

2484:    Concepts: matrix vector product^transpose and addition

2486: .seealso: MatMultHermitianTranspose(), MatMultTranspose(), MatMultAdd(), MatMult()
2487: @*/
2488: PetscErrorCode MatMultHermitianTransposeAdd(Mat mat,Vec v1,Vec v2,Vec v3)
2489: {


2499:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2500:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2501:   if (!mat->ops->multhermitiantransposeadd) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
2502:   if (v1 == v3) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"v1 and v3 must be different vectors");
2503:   if (mat->rmap->N != v1->map->N) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec v1: global dim %D %D",mat->rmap->N,v1->map->N);
2504:   if (mat->cmap->N != v2->map->N) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec v2: global dim %D %D",mat->cmap->N,v2->map->N);
2505:   if (mat->cmap->N != v3->map->N) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec v3: global dim %D %D",mat->cmap->N,v3->map->N);
2506:   MatCheckPreallocated(mat,1);

2508:   PetscLogEventBegin(MAT_MultHermitianTransposeAdd,mat,v1,v2,v3);
2509:   VecLockPush(v1);
2510:   (*mat->ops->multhermitiantransposeadd)(mat,v1,v2,v3);
2511:   VecLockPop(v1);
2512:   PetscLogEventEnd(MAT_MultHermitianTransposeAdd,mat,v1,v2,v3);
2513:   PetscObjectStateIncrease((PetscObject)v3);
2514:   return(0);
2515: }

2519: /*@
2520:    MatMultConstrained - The inner multiplication routine for a
2521:    constrained matrix P^T A P.

2523:    Neighbor-wise Collective on Mat and Vec

2525:    Input Parameters:
2526: +  mat - the matrix
2527: -  x   - the vector to be multilplied

2529:    Output Parameters:
2530: .  y - the result

2532:    Notes:
2533:    The vectors x and y cannot be the same.  I.e., one cannot
2534:    call MatMult(A,y,y).

2536:    Level: beginner

2538: .keywords: matrix, multiply, matrix-vector product, constraint
2539: .seealso: MatMult(), MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd()
2540: @*/
2541: PetscErrorCode MatMultConstrained(Mat mat,Vec x,Vec y)
2542: {

2549:   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2550:   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2551:   if (x == y) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors");
2552:   if (mat->cmap->N != x->map->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %D %D",mat->cmap->N,x->map->N);
2553:   if (mat->rmap->N != y->map->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %D %D",mat->rmap->N,y->map->N);
2554:   if (mat->rmap->n != y->map->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: local dim %D %D",mat->rmap->n,y->map->n);

2556:   PetscLogEventBegin(MAT_MultConstrained,mat,x,y,0);
2557:   VecLockPush(x);
2558:   (*mat->ops->multconstrained)(mat,x,y);
2559:   VecLockPop(x);
2560:   PetscLogEventEnd(MAT_MultConstrained,mat,x,y,0);
2561:   PetscObjectStateIncrease((PetscObject)y);
2562:   return(0);
2563: }

2567: /*@
2568:    MatMultTransposeConstrained - The inner multiplication routine for a
2569:    constrained matrix P^T A^T P.

2571:    Neighbor-wise Collective on Mat and Vec

2573:    Input Parameters:
2574: +  mat - the matrix
2575: -  x   - the vector to be multilplied

2577:    Output Parameters:
2578: .  y - the result

2580:    Notes:
2581:    The vectors x and y cannot be the same.  I.e., one cannot
2582:    call MatMult(A,y,y).

2584:    Level: beginner

2586: .keywords: matrix, multiply, matrix-vector product, constraint
2587: .seealso: MatMult(), MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd()
2588: @*/
2589: PetscErrorCode MatMultTransposeConstrained(Mat mat,Vec x,Vec y)
2590: {

2597:   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2598:   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2599:   if (x == y) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors");
2600:   if (mat->rmap->N != x->map->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %D %D",mat->cmap->N,x->map->N);
2601:   if (mat->cmap->N != y->map->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %D %D",mat->rmap->N,y->map->N);

2603:   PetscLogEventBegin(MAT_MultConstrained,mat,x,y,0);
2604:   (*mat->ops->multtransposeconstrained)(mat,x,y);
2605:   PetscLogEventEnd(MAT_MultConstrained,mat,x,y,0);
2606:   PetscObjectStateIncrease((PetscObject)y);
2607:   return(0);
2608: }

2612: /*@C
2613:    MatGetFactorType - gets the type of factorization it is

2615:    Note Collective
2616:    as the flag

2618:    Input Parameters:
2619: .  mat - the matrix

2621:    Output Parameters:
2622: .  t - the type, one of MAT_FACTOR_NONE, MAT_FACTOR_LU, MAT_FACTOR_CHOLESKY, MAT_FACTOR_ILU, MAT_FACTOR_ICC,MAT_FACTOR_ILUDT

2624:     Level: intermediate

2626: .seealso:    MatFactorType, MatGetFactor()
2627: @*/
2628: PetscErrorCode MatGetFactorType(Mat mat,MatFactorType *t)
2629: {
2633:   *t = mat->factortype;
2634:   return(0);
2635: }

2637: /* ------------------------------------------------------------*/
2640: /*@C
2641:    MatGetInfo - Returns information about matrix storage (number of
2642:    nonzeros, memory, etc.).

2644:    Collective on Mat if MAT_GLOBAL_MAX or MAT_GLOBAL_SUM is used as the flag

2646:    Input Parameters:
2647: .  mat - the matrix

2649:    Output Parameters:
2650: +  flag - flag indicating the type of parameters to be returned
2651:    (MAT_LOCAL - local matrix, MAT_GLOBAL_MAX - maximum over all processors,
2652:    MAT_GLOBAL_SUM - sum over all processors)
2653: -  info - matrix information context

2655:    Notes:
2656:    The MatInfo context contains a variety of matrix data, including
2657:    number of nonzeros allocated and used, number of mallocs during
2658:    matrix assembly, etc.  Additional information for factored matrices
2659:    is provided (such as the fill ratio, number of mallocs during
2660:    factorization, etc.).  Much of this info is printed to PETSC_STDOUT
2661:    when using the runtime options
2662: $       -info -mat_view ::ascii_info

2664:    Example for C/C++ Users:
2665:    See the file ${PETSC_DIR}/include/petscmat.h for a complete list of
2666:    data within the MatInfo context.  For example,
2667: .vb
2668:       MatInfo info;
2669:       Mat     A;
2670:       double  mal, nz_a, nz_u;

2672:       MatGetInfo(A,MAT_LOCAL,&info);
2673:       mal  = info.mallocs;
2674:       nz_a = info.nz_allocated;
2675: .ve

2677:    Example for Fortran Users:
2678:    Fortran users should declare info as a double precision
2679:    array of dimension MAT_INFO_SIZE, and then extract the parameters
2680:    of interest.  See the file ${PETSC_DIR}/include/petsc/finclude/petscmat.h
2681:    a complete list of parameter names.
2682: .vb
2683:       double  precision info(MAT_INFO_SIZE)
2684:       double  precision mal, nz_a
2685:       Mat     A
2686:       integer ierr

2688:       call MatGetInfo(A,MAT_LOCAL,info,ierr)
2689:       mal = info(MAT_INFO_MALLOCS)
2690:       nz_a = info(MAT_INFO_NZ_ALLOCATED)
2691: .ve

2693:     Level: intermediate

2695:     Concepts: matrices^getting information on

2697:     Developer Note: fortran interface is not autogenerated as the f90
2698:     interface defintion cannot be generated correctly [due to MatInfo]

2700: .seealso: MatStashGetInfo()

2702: @*/
2703: PetscErrorCode MatGetInfo(Mat mat,MatInfoType flag,MatInfo *info)
2704: {

2711:   if (!mat->ops->getinfo) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
2712:   MatCheckPreallocated(mat,1);
2713:   (*mat->ops->getinfo)(mat,flag,info);
2714:   return(0);
2715: }

2717: /* ----------------------------------------------------------*/

2721: /*@C
2722:    MatLUFactor - Performs in-place LU factorization of matrix.

2724:    Collective on Mat

2726:    Input Parameters:
2727: +  mat - the matrix
2728: .  row - row permutation
2729: .  col - column permutation
2730: -  info - options for factorization, includes
2731: $          fill - expected fill as ratio of original fill.
2732: $          dtcol - pivot tolerance (0 no pivot, 1 full column pivoting)
2733: $                   Run with the option -info to determine an optimal value to use

2735:    Notes:
2736:    Most users should employ the simplified KSP interface for linear solvers
2737:    instead of working directly with matrix algebra routines such as this.
2738:    See, e.g., KSPCreate().

2740:    This changes the state of the matrix to a factored matrix; it cannot be used
2741:    for example with MatSetValues() unless one first calls MatSetUnfactored().

2743:    Level: developer

2745:    Concepts: matrices^LU factorization

2747: .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor(),
2748:           MatGetOrdering(), MatSetUnfactored(), MatFactorInfo, MatGetFactor()

2750:     Developer Note: fortran interface is not autogenerated as the f90
2751:     interface defintion cannot be generated correctly [due to MatFactorInfo]

2753: @*/
2754: PetscErrorCode MatLUFactor(Mat mat,IS row,IS col,const MatFactorInfo *info)
2755: {
2757:   MatFactorInfo  tinfo;

2765:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2766:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2767:   if (!mat->ops->lufactor) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
2768:   MatCheckPreallocated(mat,1);
2769:   if (!info) {
2770:     MatFactorInfoInitialize(&tinfo);
2771:     info = &tinfo;
2772:   }

2774:   PetscLogEventBegin(MAT_LUFactor,mat,row,col,0);
2775:   (*mat->ops->lufactor)(mat,row,col,info);
2776:   PetscLogEventEnd(MAT_LUFactor,mat,row,col,0);
2777:   PetscObjectStateIncrease((PetscObject)mat);
2778:   return(0);
2779: }

2783: /*@C
2784:    MatILUFactor - Performs in-place ILU factorization of matrix.

2786:    Collective on Mat

2788:    Input Parameters:
2789: +  mat - the matrix
2790: .  row - row permutation
2791: .  col - column permutation
2792: -  info - structure containing
2793: $      levels - number of levels of fill.
2794: $      expected fill - as ratio of original fill.
2795: $      1 or 0 - indicating force fill on diagonal (improves robustness for matrices
2796:                 missing diagonal entries)

2798:    Notes:
2799:    Probably really in-place only when level of fill is zero, otherwise allocates
2800:    new space to store factored matrix and deletes previous memory.

2802:    Most users should employ the simplified KSP interface for linear solvers
2803:    instead of working directly with matrix algebra routines such as this.
2804:    See, e.g., KSPCreate().

2806:    Level: developer

2808:    Concepts: matrices^ILU factorization

2810: .seealso: MatILUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor(), MatFactorInfo

2812:     Developer Note: fortran interface is not autogenerated as the f90
2813:     interface defintion cannot be generated correctly [due to MatFactorInfo]

2815: @*/
2816: PetscErrorCode MatILUFactor(Mat mat,IS row,IS col,const MatFactorInfo *info)
2817: {

2826:   if (mat->rmap->N != mat->cmap->N) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONG,"matrix must be square");
2827:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2828:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2829:   if (!mat->ops->ilufactor) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
2830:   MatCheckPreallocated(mat,1);

2832:   PetscLogEventBegin(MAT_ILUFactor,mat,row,col,0);
2833:   (*mat->ops->ilufactor)(mat,row,col,info);
2834:   PetscLogEventEnd(MAT_ILUFactor,mat,row,col,0);
2835:   PetscObjectStateIncrease((PetscObject)mat);
2836:   return(0);
2837: }

2841: /*@C
2842:    MatLUFactorSymbolic - Performs symbolic LU factorization of matrix.
2843:    Call this routine before calling MatLUFactorNumeric().

2845:    Collective on Mat

2847:    Input Parameters:
2848: +  fact - the factor matrix obtained with MatGetFactor()
2849: .  mat - the matrix
2850: .  row, col - row and column permutations
2851: -  info - options for factorization, includes
2852: $          fill - expected fill as ratio of original fill.
2853: $          dtcol - pivot tolerance (0 no pivot, 1 full column pivoting)
2854: $                   Run with the option -info to determine an optimal value to use


2857:    Notes: See Users-Manual: ch_mat for additional information about choosing the fill factor for better efficiency.

2859:    Most users should employ the simplified KSP interface for linear solvers
2860:    instead of working directly with matrix algebra routines such as this.
2861:    See, e.g., KSPCreate().

2863:    Level: developer

2865:    Concepts: matrices^LU symbolic factorization

2867: .seealso: MatLUFactor(), MatLUFactorNumeric(), MatCholeskyFactor(), MatFactorInfo, MatFactorInfoInitialize()

2869:     Developer Note: fortran interface is not autogenerated as the f90
2870:     interface defintion cannot be generated correctly [due to MatFactorInfo]

2872: @*/
2873: PetscErrorCode MatLUFactorSymbolic(Mat fact,Mat mat,IS row,IS col,const MatFactorInfo *info)
2874: {

2884:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2885:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2886:   if (!(fact)->ops->lufactorsymbolic) {
2887:     const MatSolverPackage spackage;
2888:     MatFactorGetSolverPackage(fact,&spackage);
2889:     SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Matrix type %s symbolic LU using solver package %s",((PetscObject)mat)->type_name,spackage);
2890:   }
2891:   MatCheckPreallocated(mat,2);

2893:   PetscLogEventBegin(MAT_LUFactorSymbolic,mat,row,col,0);
2894:   (fact->ops->lufactorsymbolic)(fact,mat,row,col,info);
2895:   PetscLogEventEnd(MAT_LUFactorSymbolic,mat,row,col,0);
2896:   PetscObjectStateIncrease((PetscObject)fact);
2897:   return(0);
2898: }

2902: /*@C
2903:    MatLUFactorNumeric - Performs numeric LU factorization of a matrix.
2904:    Call this routine after first calling MatLUFactorSymbolic().

2906:    Collective on Mat

2908:    Input Parameters:
2909: +  fact - the factor matrix obtained with MatGetFactor()
2910: .  mat - the matrix
2911: -  info - options for factorization

2913:    Notes:
2914:    See MatLUFactor() for in-place factorization.  See
2915:    MatCholeskyFactorNumeric() for the symmetric, positive definite case.

2917:    Most users should employ the simplified KSP interface for linear solvers
2918:    instead of working directly with matrix algebra routines such as this.
2919:    See, e.g., KSPCreate().

2921:    Level: developer

2923:    Concepts: matrices^LU numeric factorization

2925: .seealso: MatLUFactorSymbolic(), MatLUFactor(), MatCholeskyFactor()

2927:     Developer Note: fortran interface is not autogenerated as the f90
2928:     interface defintion cannot be generated correctly [due to MatFactorInfo]

2930: @*/
2931: PetscErrorCode MatLUFactorNumeric(Mat fact,Mat mat,const MatFactorInfo *info)
2932: {

2940:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2941:   if (mat->rmap->N != (fact)->rmap->N || mat->cmap->N != (fact)->cmap->N) SETERRQ4(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Mat fact: global dimensions are different %D should = %D %D should = %D",mat->rmap->N,(fact)->rmap->N,mat->cmap->N,(fact)->cmap->N);

2943:   if (!(fact)->ops->lufactornumeric) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s numeric LU",((PetscObject)mat)->type_name);
2944:   MatCheckPreallocated(mat,2);
2945:   PetscLogEventBegin(MAT_LUFactorNumeric,mat,fact,0,0);
2946:   (fact->ops->lufactornumeric)(fact,mat,info);
2947:   PetscLogEventEnd(MAT_LUFactorNumeric,mat,fact,0,0);
2948:   MatViewFromOptions(fact,NULL,"-mat_factor_view");
2949:   PetscObjectStateIncrease((PetscObject)fact);
2950:   return(0);
2951: }

2955: /*@C
2956:    MatCholeskyFactor - Performs in-place Cholesky factorization of a
2957:    symmetric matrix.

2959:    Collective on Mat

2961:    Input Parameters:
2962: +  mat - the matrix
2963: .  perm - row and column permutations
2964: -  f - expected fill as ratio of original fill

2966:    Notes:
2967:    See MatLUFactor() for the nonsymmetric case.  See also
2968:    MatCholeskyFactorSymbolic(), and MatCholeskyFactorNumeric().

2970:    Most users should employ the simplified KSP interface for linear solvers
2971:    instead of working directly with matrix algebra routines such as this.
2972:    See, e.g., KSPCreate().

2974:    Level: developer

2976:    Concepts: matrices^Cholesky factorization

2978: .seealso: MatLUFactor(), MatCholeskyFactorSymbolic(), MatCholeskyFactorNumeric()
2979:           MatGetOrdering()

2981:     Developer Note: fortran interface is not autogenerated as the f90
2982:     interface defintion cannot be generated correctly [due to MatFactorInfo]

2984: @*/
2985: PetscErrorCode MatCholeskyFactor(Mat mat,IS perm,const MatFactorInfo *info)
2986: {

2994:   if (mat->rmap->N != mat->cmap->N) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONG,"Matrix must be square");
2995:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2996:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2997:   if (!mat->ops->choleskyfactor) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
2998:   MatCheckPreallocated(mat,1);

3000:   PetscLogEventBegin(MAT_CholeskyFactor,mat,perm,0,0);
3001:   (*mat->ops->choleskyfactor)(mat,perm,info);
3002:   PetscLogEventEnd(MAT_CholeskyFactor,mat,perm,0,0);
3003:   PetscObjectStateIncrease((PetscObject)mat);
3004:   return(0);
3005: }

3009: /*@C
3010:    MatCholeskyFactorSymbolic - Performs symbolic Cholesky factorization
3011:    of a symmetric matrix.

3013:    Collective on Mat

3015:    Input Parameters:
3016: +  fact - the factor matrix obtained with MatGetFactor()
3017: .  mat - the matrix
3018: .  perm - row and column permutations
3019: -  info - options for factorization, includes
3020: $          fill - expected fill as ratio of original fill.
3021: $          dtcol - pivot tolerance (0 no pivot, 1 full column pivoting)
3022: $                   Run with the option -info to determine an optimal value to use

3024:    Notes:
3025:    See MatLUFactorSymbolic() for the nonsymmetric case.  See also
3026:    MatCholeskyFactor() and MatCholeskyFactorNumeric().

3028:    Most users should employ the simplified KSP interface for linear solvers
3029:    instead of working directly with matrix algebra routines such as this.
3030:    See, e.g., KSPCreate().

3032:    Level: developer

3034:    Concepts: matrices^Cholesky symbolic factorization

3036: .seealso: MatLUFactorSymbolic(), MatCholeskyFactor(), MatCholeskyFactorNumeric()
3037:           MatGetOrdering()

3039:     Developer Note: fortran interface is not autogenerated as the f90
3040:     interface defintion cannot be generated correctly [due to MatFactorInfo]

3042: @*/
3043: PetscErrorCode MatCholeskyFactorSymbolic(Mat fact,Mat mat,IS perm,const MatFactorInfo *info)
3044: {

3053:   if (mat->rmap->N != mat->cmap->N) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONG,"Matrix must be square");
3054:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3055:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3056:   if (!(fact)->ops->choleskyfactorsymbolic) {
3057:     const MatSolverPackage spackage;
3058:     MatFactorGetSolverPackage(fact,&spackage);
3059:     SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s symbolic factor Cholesky using solver package %s",((PetscObject)mat)->type_name,spackage);
3060:   }
3061:   MatCheckPreallocated(mat,2);

3063:   PetscLogEventBegin(MAT_CholeskyFactorSymbolic,mat,perm,0,0);
3064:   (fact->ops->choleskyfactorsymbolic)(fact,mat,perm,info);
3065:   PetscLogEventEnd(MAT_CholeskyFactorSymbolic,mat,perm,0,0);
3066:   PetscObjectStateIncrease((PetscObject)fact);
3067:   return(0);
3068: }

3072: /*@C
3073:    MatCholeskyFactorNumeric - Performs numeric Cholesky factorization
3074:    of a symmetric matrix. Call this routine after first calling
3075:    MatCholeskyFactorSymbolic().

3077:    Collective on Mat

3079:    Input Parameters:
3080: +  fact - the factor matrix obtained with MatGetFactor()
3081: .  mat - the initial matrix
3082: .  info - options for factorization
3083: -  fact - the symbolic factor of mat


3086:    Notes:
3087:    Most users should employ the simplified KSP interface for linear solvers
3088:    instead of working directly with matrix algebra routines such as this.
3089:    See, e.g., KSPCreate().

3091:    Level: developer

3093:    Concepts: matrices^Cholesky numeric factorization

3095: .seealso: MatCholeskyFactorSymbolic(), MatCholeskyFactor(), MatLUFactorNumeric()

3097:     Developer Note: fortran interface is not autogenerated as the f90
3098:     interface defintion cannot be generated correctly [due to MatFactorInfo]

3100: @*/
3101: PetscErrorCode MatCholeskyFactorNumeric(Mat fact,Mat mat,const MatFactorInfo *info)
3102: {

3110:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3111:   if (!(fact)->ops->choleskyfactornumeric) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s numeric factor Cholesky",((PetscObject)mat)->type_name);
3112:   if (mat->rmap->N != (fact)->rmap->N || mat->cmap->N != (fact)->cmap->N) SETERRQ4(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Mat fact: global dim %D should = %D %D should = %D",mat->rmap->N,(fact)->rmap->N,mat->cmap->N,(fact)->cmap->N);
3113:   MatCheckPreallocated(mat,2);

3115:   PetscLogEventBegin(MAT_CholeskyFactorNumeric,mat,fact,0,0);
3116:   (fact->ops->choleskyfactornumeric)(fact,mat,info);
3117:   PetscLogEventEnd(MAT_CholeskyFactorNumeric,mat,fact,0,0);
3118:   MatViewFromOptions(fact,NULL,"-mat_factor_view");
3119:   PetscObjectStateIncrease((PetscObject)fact);
3120:   return(0);
3121: }

3123: /* ----------------------------------------------------------------*/
3126: /*@
3127:    MatSolve - Solves A x = b, given a factored matrix.

3129:    Neighbor-wise Collective on Mat and Vec

3131:    Input Parameters:
3132: +  mat - the factored matrix
3133: -  b - the right-hand-side vector

3135:    Output Parameter:
3136: .  x - the result vector

3138:    Notes:
3139:    The vectors b and x cannot be the same.  I.e., one cannot
3140:    call MatSolve(A,x,x).

3142:    Notes:
3143:    Most users should employ the simplified KSP interface for linear solvers
3144:    instead of working directly with matrix algebra routines such as this.
3145:    See, e.g., KSPCreate().

3147:    Level: developer

3149:    Concepts: matrices^triangular solves

3151: .seealso: MatSolveAdd(), MatSolveTranspose(), MatSolveTransposeAdd()
3152: @*/
3153: PetscErrorCode MatSolve(Mat mat,Vec b,Vec x)
3154: {

3164:   if (x == b) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"x and b must be different vectors");
3165:   if (!mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
3166:   if (mat->cmap->N != x->map->N) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %D %D",mat->cmap->N,x->map->N);
3167:   if (mat->rmap->N != b->map->N) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %D %D",mat->rmap->N,b->map->N);
3168:   if (mat->rmap->n != b->map->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: local dim %D %D",mat->rmap->n,b->map->n);
3169:   if (!mat->rmap->N && !mat->cmap->N) return(0);
3170:   if (!mat->ops->solve) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
3171:   MatCheckPreallocated(mat,1);

3173:   PetscLogEventBegin(MAT_Solve,mat,b,x,0);
3174:   if (mat->errortype) {
3175:     PetscInfo1(mat,"MatFactorError %D\n",mat->errortype);
3176:     VecSetInf(x);
3177:   } else {
3178:     (*mat->ops->solve)(mat,b,x);
3179:   }
3180:   PetscLogEventEnd(MAT_Solve,mat,b,x,0);
3181:   PetscObjectStateIncrease((PetscObject)x);
3182:   return(0);
3183: }

3187: PetscErrorCode MatMatSolve_Basic(Mat A,Mat B,Mat X)
3188: {
3190:   Vec            b,x;
3191:   PetscInt       m,N,i;
3192:   PetscScalar    *bb,*xx;
3193:   PetscBool      flg;

3196:   PetscObjectTypeCompareAny((PetscObject)B,&flg,MATSEQDENSE,MATMPIDENSE,NULL);
3197:   if (!flg) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONG,"Matrix B must be MATDENSE matrix");
3198:   PetscObjectTypeCompareAny((PetscObject)X,&flg,MATSEQDENSE,MATMPIDENSE,NULL);
3199:   if (!flg) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONG,"Matrix X must be MATDENSE matrix");

3201:   MatDenseGetArray(B,&bb);
3202:   MatDenseGetArray(X,&xx);
3203:   MatGetLocalSize(B,&m,NULL);  /* number local rows */
3204:   MatGetSize(B,NULL,&N);       /* total columns in dense matrix */
3205:   MatCreateVecs(A,&x,&b);
3206:   for (i=0; i<N; i++) {
3207:     VecPlaceArray(b,bb + i*m);
3208:     VecPlaceArray(x,xx + i*m);
3209:     MatSolve(A,b,x);
3210:     VecResetArray(x);
3211:     VecResetArray(b);
3212:   }
3213:   VecDestroy(&b);
3214:   VecDestroy(&x);
3215:   MatDenseRestoreArray(B,&bb);
3216:   MatDenseRestoreArray(X,&xx);
3217:   return(0);
3218: }

3222: /*@
3223:    MatMatSolve - Solves A X = B, given a factored matrix.

3225:    Neighbor-wise Collective on Mat

3227:    Input Parameters:
3228: +  A - the factored matrix
3229: -  B - the right-hand-side matrix  (dense matrix)

3231:    Output Parameter:
3232: .  X - the result matrix (dense matrix)

3234:    Notes:
3235:    The matrices b and x cannot be the same.  I.e., one cannot
3236:    call MatMatSolve(A,x,x).

3238:    Notes:
3239:    Most users should usually employ the simplified KSP interface for linear solvers
3240:    instead of working directly with matrix algebra routines such as this.
3241:    See, e.g., KSPCreate(). However KSP can only solve for one vector (column of X)
3242:    at a time.

3244:    When using SuperLU_Dist as a parallel solver PETSc will use the SuperLU_Dist functionality to solve multiple right hand sides simultaneously. For MUMPS
3245:    it calls a separate solve for each right hand side since MUMPS does not yet support distributed right hand sides.

3247:    Since the resulting matrix X must always be dense we do not support sparse representation of the matrix B.

3249:    Level: developer

3251:    Concepts: matrices^triangular solves

3253: .seealso: MatMatSolveAdd(), MatMatSolveTranspose(), MatMatSolveTransposeAdd(), MatLUFactor(), MatCholeskyFactor()
3254: @*/
3255: PetscErrorCode MatMatSolve(Mat A,Mat B,Mat X)
3256: {

3266:   if (X == B) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_IDN,"X and B must be different matrices");
3267:   if (!A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
3268:   if (A->cmap->N != X->rmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Mat A,Mat X: global dim %D %D",A->cmap->N,X->rmap->N);
3269:   if (A->rmap->N != B->rmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Mat A,Mat B: global dim %D %D",A->rmap->N,B->rmap->N);
3270:   if (A->rmap->n != B->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat A,Mat B: local dim %D %D",A->rmap->n,B->rmap->n);
3271:   if (X->cmap->N < B->cmap->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Solution matrix must have same number of columns as rhs matrix");
3272:   if (!A->rmap->N && !A->cmap->N) return(0);
3273:   MatCheckPreallocated(A,1);

3275:   PetscLogEventBegin(MAT_MatSolve,A,B,X,0);
3276:   if (!A->ops->matsolve) {
3277:     PetscInfo1(A,"Mat type %s using basic MatMatSolve\n",((PetscObject)A)->type_name);
3278:     MatMatSolve_Basic(A,B,X);
3279:   } else {
3280:     (*A->ops->matsolve)(A,B,X);
3281:   }
3282:   PetscLogEventEnd(MAT_MatSolve,A,B,X,0);
3283:   PetscObjectStateIncrease((PetscObject)X);
3284:   return(0);
3285: }


3290: /*@
3291:    MatForwardSolve - Solves L x = b, given a factored matrix, A = LU, or
3292:                             U^T*D^(1/2) x = b, given a factored symmetric matrix, A = U^T*D*U,

3294:    Neighbor-wise Collective on Mat and Vec

3296:    Input Parameters:
3297: +  mat - the factored matrix
3298: -  b - the right-hand-side vector

3300:    Output Parameter:
3301: .  x - the result vector

3303:    Notes:
3304:    MatSolve() should be used for most applications, as it performs
3305:    a forward solve followed by a backward solve.

3307:    The vectors b and x cannot be the same,  i.e., one cannot
3308:    call MatForwardSolve(A,x,x).

3310:    For matrix in seqsbaij format with block size larger than 1,
3311:    the diagonal blocks are not implemented as D = D^(1/2) * D^(1/2) yet.
3312:    MatForwardSolve() solves U^T*D y = b, and
3313:    MatBackwardSolve() solves U x = y.
3314:    Thus they do not provide a symmetric preconditioner.

3316:    Most users should employ the simplified KSP interface for linear solvers
3317:    instead of working directly with matrix algebra routines such as this.
3318:    See, e.g., KSPCreate().

3320:    Level: developer

3322:    Concepts: matrices^forward solves

3324: .seealso: MatSolve(), MatBackwardSolve()
3325: @*/
3326: PetscErrorCode MatForwardSolve(Mat mat,Vec b,Vec x)
3327: {

3337:   if (x == b) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"x and b must be different vectors");
3338:   if (!mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
3339:   if (!mat->ops->forwardsolve) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
3340:   if (mat->cmap->N != x->map->N) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %D %D",mat->cmap->N,x->map->N);
3341:   if (mat->rmap->N != b->map->N) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %D %D",mat->rmap->N,b->map->N);
3342:   if (mat->rmap->n != b->map->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: local dim %D %D",mat->rmap->n,b->map->n);
3343:   MatCheckPreallocated(mat,1);
3344:   PetscLogEventBegin(MAT_ForwardSolve,mat,b,x,0);
3345:   (*mat->ops->forwardsolve)(mat,b,x);
3346:   PetscLogEventEnd(MAT_ForwardSolve,mat,b,x,0);
3347:   PetscObjectStateIncrease((PetscObject)x);
3348:   return(0);
3349: }

3353: /*@
3354:    MatBackwardSolve - Solves U x = b, given a factored matrix, A = LU.
3355:                              D^(1/2) U x = b, given a factored symmetric matrix, A = U^T*D*U,

3357:    Neighbor-wise Collective on Mat and Vec

3359:    Input Parameters:
3360: +  mat - the factored matrix
3361: -  b - the right-hand-side vector

3363:    Output Parameter:
3364: .  x - the result vector

3366:    Notes:
3367:    MatSolve() should be used for most applications, as it performs
3368:    a forward solve followed by a backward solve.

3370:    The vectors b and x cannot be the same.  I.e., one cannot
3371:    call MatBackwardSolve(A,x,x).

3373:    For matrix in seqsbaij format with block size larger than 1,
3374:    the diagonal blocks are not implemented as D = D^(1/2) * D^(1/2) yet.
3375:    MatForwardSolve() solves U^T*D y = b, and
3376:    MatBackwardSolve() solves U x = y.
3377:    Thus they do not provide a symmetric preconditioner.

3379:    Most users should employ the simplified KSP interface for linear solvers
3380:    instead of working directly with matrix algebra routines such as this.
3381:    See, e.g., KSPCreate().

3383:    Level: developer

3385:    Concepts: matrices^backward solves

3387: .seealso: MatSolve(), MatForwardSolve()
3388: @*/
3389: PetscErrorCode MatBackwardSolve(Mat mat,Vec b,Vec x)
3390: {

3400:   if (x == b) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"x and b must be different vectors");
3401:   if (!mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
3402:   if (!mat->ops->backwardsolve) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
3403:   if (mat->cmap->N != x->map->N) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %D %D",mat->cmap->N,x->map->N);
3404:   if (mat->rmap->N != b->map->N) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %D %D",mat->rmap->N,b->map->N);
3405:   if (mat->rmap->n != b->map->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: local dim %D %D",mat->rmap->n,b->map->n);
3406:   MatCheckPreallocated(mat,1);

3408:   PetscLogEventBegin(MAT_BackwardSolve,mat,b,x,0);
3409:   (*mat->ops->backwardsolve)(mat,b,x);
3410:   PetscLogEventEnd(MAT_BackwardSolve,mat,b,x,0);
3411:   PetscObjectStateIncrease((PetscObject)x);
3412:   return(0);
3413: }

3417: /*@
3418:    MatSolveAdd - Computes x = y + inv(A)*b, given a factored matrix.

3420:    Neighbor-wise Collective on Mat and Vec

3422:    Input Parameters:
3423: +  mat - the factored matrix
3424: .  b - the right-hand-side vector
3425: -  y - the vector to be added to

3427:    Output Parameter:
3428: .  x - the result vector

3430:    Notes:
3431:    The vectors b and x cannot be the same.  I.e., one cannot
3432:    call MatSolveAdd(A,x,y,x).

3434:    Most users should employ the simplified KSP interface for linear solvers
3435:    instead of working directly with matrix algebra routines such as this.
3436:    See, e.g., KSPCreate().

3438:    Level: developer

3440:    Concepts: matrices^triangular solves

3442: .seealso: MatSolve(), MatSolveTranspose(), MatSolveTransposeAdd()
3443: @*/
3444: PetscErrorCode MatSolveAdd(Mat mat,Vec b,Vec y,Vec x)
3445: {
3446:   PetscScalar    one = 1.0;
3447:   Vec            tmp;

3459:   if (x == b) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"x and b must be different vectors");
3460:   if (!mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
3461:   if (mat->cmap->N != x->map->N) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %D %D",mat->cmap->N,x->map->N);
3462:   if (mat->rmap->N != b->map->N) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %D %D",mat->rmap->N,b->map->N);
3463:   if (mat->rmap->N != y->map->N) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %D %D",mat->rmap->N,y->map->N);
3464:   if (mat->rmap->n != b->map->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: local dim %D %D",mat->rmap->n,b->map->n);
3465:   if (x->map->n != y->map->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Vec x,Vec y: local dim %D %D",x->map->n,y->map->n);
3466:   MatCheckPreallocated(mat,1);

3468:   PetscLogEventBegin(MAT_SolveAdd,mat,b,x,y);
3469:   if (mat->ops->solveadd) {
3470:     (*mat->ops->solveadd)(mat,b,y,x);
3471:   } else {
3472:     /* do the solve then the add manually */
3473:     if (x != y) {
3474:       MatSolve(mat,b,x);
3475:       VecAXPY(x,one,y);
3476:     } else {
3477:       VecDuplicate(x,&tmp);
3478:       PetscLogObjectParent((PetscObject)mat,(PetscObject)tmp);
3479:       VecCopy(x,tmp);
3480:       MatSolve(mat,b,x);
3481:       VecAXPY(x,one,tmp);
3482:       VecDestroy(&tmp);
3483:     }
3484:   }
3485:   PetscLogEventEnd(MAT_SolveAdd,mat,b,x,y);
3486:   PetscObjectStateIncrease((PetscObject)x);
3487:   return(0);
3488: }

3492: /*@
3493:    MatSolveTranspose - Solves A' x = b, given a factored matrix.

3495:    Neighbor-wise Collective on Mat and Vec

3497:    Input Parameters:
3498: +  mat - the factored matrix
3499: -  b - the right-hand-side vector

3501:    Output Parameter:
3502: .  x - the result vector

3504:    Notes:
3505:    The vectors b and x cannot be the same.  I.e., one cannot
3506:    call MatSolveTranspose(A,x,x).

3508:    Most users should employ the simplified KSP interface for linear solvers
3509:    instead of working directly with matrix algebra routines such as this.
3510:    See, e.g., KSPCreate().

3512:    Level: developer

3514:    Concepts: matrices^triangular solves

3516: .seealso: MatSolve(), MatSolveAdd(), MatSolveTransposeAdd()
3517: @*/
3518: PetscErrorCode MatSolveTranspose(Mat mat,Vec b,Vec x)
3519: {

3529:   if (!mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
3530:   if (x == b) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"x and b must be different vectors");
3531:   if (!mat->ops->solvetranspose) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Matrix type %s",((PetscObject)mat)->type_name);
3532:   if (mat->rmap->N != x->map->N) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %D %D",mat->rmap->N,x->map->N);
3533:   if (mat->cmap->N != b->map->N) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %D %D",mat->cmap->N,b->map->N);
3534:   MatCheckPreallocated(mat,1);
3535:   PetscLogEventBegin(MAT_SolveTranspose,mat,b,x,0);
3536:   if (mat->errortype) {
3537:     PetscInfo1(mat,"MatFactorError %D\n",mat->errortype);
3538:     VecSetInf(x);
3539:   } else {
3540:     (*mat->ops->solvetranspose)(mat,b,x);
3541:   }
3542:   PetscLogEventEnd(MAT_SolveTranspose,mat,b,x,0);
3543:   PetscObjectStateIncrease((PetscObject)x);
3544:   return(0);
3545: }

3549: /*@
3550:    MatSolveTransposeAdd - Computes x = y + inv(Transpose(A)) b, given a
3551:                       factored matrix.

3553:    Neighbor-wise Collective on Mat and Vec

3555:    Input Parameters:
3556: +  mat - the factored matrix
3557: .  b - the right-hand-side vector
3558: -  y - the vector to be added to

3560:    Output Parameter:
3561: .  x - the result vector

3563:    Notes:
3564:    The vectors b and x cannot be the same.  I.e., one cannot
3565:    call MatSolveTransposeAdd(A,x,y,x).

3567:    Most users should employ the simplified KSP interface for linear solvers
3568:    instead of working directly with matrix algebra routines such as this.
3569:    See, e.g., KSPCreate().

3571:    Level: developer

3573:    Concepts: matrices^triangular solves

3575: .seealso: MatSolve(), MatSolveAdd(), MatSolveTranspose()
3576: @*/
3577: PetscErrorCode MatSolveTransposeAdd(Mat mat,Vec b,Vec y,Vec x)
3578: {
3579:   PetscScalar    one = 1.0;
3581:   Vec            tmp;

3592:   if (x == b) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"x and b must be different vectors");
3593:   if (!mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
3594:   if (mat->rmap->N != x->map->N) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %D %D",mat->rmap->N,x->map->N);
3595:   if (mat->cmap->N != b->map->N) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %D %D",mat->cmap->N,b->map->N);
3596:   if (mat->cmap->N != y->map->N) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %D %D",mat->cmap->N,y->map->N);
3597:   if (x->map->n != y->map->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Vec x,Vec y: local dim %D %D",x->map->n,y->map->n);
3598:   MatCheckPreallocated(mat,1);

3600:   PetscLogEventBegin(MAT_SolveTransposeAdd,mat,b,x,y);
3601:   if (mat->ops->solvetransposeadd) {
3602:     if (mat->errortype) {
3603:       PetscInfo1(mat,"MatFactorError %D\n",mat->errortype);
3604:       VecSetInf(x);
3605:     } else {
3606:       (*mat->ops->solvetransposeadd)(mat,b,y,x);
3607:     }
3608:   } else {
3609:     /* do the solve then the add manually */
3610:     if (x != y) {
3611:       MatSolveTranspose(mat,b,x);
3612:       VecAXPY(x,one,y);
3613:     } else {
3614:       VecDuplicate(x,&tmp);
3615:       PetscLogObjectParent((PetscObject)mat,(PetscObject)tmp);
3616:       VecCopy(x,tmp);
3617:       MatSolveTranspose(mat,b,x);
3618:       VecAXPY(x,one,tmp);
3619:       VecDestroy(&tmp);
3620:     }
3621:   }
3622:   PetscLogEventEnd(MAT_SolveTransposeAdd,mat,b,x,y);
3623:   PetscObjectStateIncrease((PetscObject)x);
3624:   return(0);
3625: }
3626: /* ----------------------------------------------------------------*/

3630: /*@
3631:    MatSOR - Computes relaxation (SOR, Gauss-Seidel) sweeps.

3633:    Neighbor-wise Collective on Mat and Vec

3635:    Input Parameters:
3636: +  mat - the matrix
3637: .  b - the right hand side
3638: .  omega - the relaxation factor
3639: .  flag - flag indicating the type of SOR (see below)
3640: .  shift -  diagonal shift
3641: .  its - the number of iterations
3642: -  lits - the number of local iterations

3644:    Output Parameters:
3645: .  x - the solution (can contain an initial guess, use option SOR_ZERO_INITIAL_GUESS to indicate no guess)

3647:    SOR Flags:
3648: .     SOR_FORWARD_SWEEP - forward SOR
3649: .     SOR_BACKWARD_SWEEP - backward SOR
3650: .     SOR_SYMMETRIC_SWEEP - SSOR (symmetric SOR)
3651: .     SOR_LOCAL_FORWARD_SWEEP - local forward SOR
3652: .     SOR_LOCAL_BACKWARD_SWEEP - local forward SOR
3653: .     SOR_LOCAL_SYMMETRIC_SWEEP - local SSOR
3654: .     SOR_APPLY_UPPER, SOR_APPLY_LOWER - applies
3655:          upper/lower triangular part of matrix to
3656:          vector (with omega)
3657: .     SOR_ZERO_INITIAL_GUESS - zero initial guess

3659:    Notes:
3660:    SOR_LOCAL_FORWARD_SWEEP, SOR_LOCAL_BACKWARD_SWEEP, and
3661:    SOR_LOCAL_SYMMETRIC_SWEEP perform separate independent smoothings
3662:    on each processor.

3664:    Application programmers will not generally use MatSOR() directly,
3665:    but instead will employ the KSP/PC interface.

3667:    Notes: for BAIJ, SBAIJ, and AIJ matrices with Inodes this does a block SOR smoothing, otherwise it does a pointwise smoothing

3669:    Notes for Advanced Users:
3670:    The flags are implemented as bitwise inclusive or operations.
3671:    For example, use (SOR_ZERO_INITIAL_GUESS | SOR_SYMMETRIC_SWEEP)
3672:    to specify a zero initial guess for SSOR.

3674:    Most users should employ the simplified KSP interface for linear solvers
3675:    instead of working directly with matrix algebra routines such as this.
3676:    See, e.g., KSPCreate().

3678:    Vectors x and b CANNOT be the same

3680:    Developer Note: We should add block SOR support for AIJ matrices with block size set to great than one and no inodes

3682:    Level: developer

3684:    Concepts: matrices^relaxation
3685:    Concepts: matrices^SOR
3686:    Concepts: matrices^Gauss-Seidel

3688: @*/
3689: PetscErrorCode MatSOR(Mat mat,Vec b,PetscReal omega,MatSORType flag,PetscReal shift,PetscInt its,PetscInt lits,Vec x)
3690: {

3700:   if (!mat->ops->sor) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
3701:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3702:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3703:   if (mat->cmap->N != x->map->N) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %D %D",mat->cmap->N,x->map->N);
3704:   if (mat->rmap->N != b->map->N) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %D %D",mat->rmap->N,b->map->N);
3705:   if (mat->rmap->n != b->map->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: local dim %D %D",mat->rmap->n,b->map->n);
3706:   if (its <= 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Relaxation requires global its %D positive",its);
3707:   if (lits <= 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Relaxation requires local its %D positive",lits);
3708:   if (b == x) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"b and x vector cannot be the same");

3710:   MatCheckPreallocated(mat,1);
3711:   PetscLogEventBegin(MAT_SOR,mat,b,x,0);
3712:   ierr =(*mat->ops->sor)(mat,b,omega,flag,shift,its,lits,x);
3713:   PetscLogEventEnd(MAT_SOR,mat,b,x,0);
3714:   PetscObjectStateIncrease((PetscObject)x);
3715:   return(0);
3716: }

3720: /*
3721:       Default matrix copy routine.
3722: */
3723: PetscErrorCode MatCopy_Basic(Mat A,Mat B,MatStructure str)
3724: {
3725:   PetscErrorCode    ierr;
3726:   PetscInt          i,rstart = 0,rend = 0,nz;
3727:   const PetscInt    *cwork;
3728:   const PetscScalar *vwork;

3731:   if (B->assembled) {
3732:     MatZeroEntries(B);
3733:   }
3734:   MatGetOwnershipRange(A,&rstart,&rend);
3735:   for (i=rstart; i<rend; i++) {
3736:     MatGetRow(A,i,&nz,&cwork,&vwork);
3737:     MatSetValues(B,1,&i,nz,cwork,vwork,INSERT_VALUES);
3738:     MatRestoreRow(A,i,&nz,&cwork,&vwork);
3739:   }
3740:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
3741:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
3742:   PetscObjectStateIncrease((PetscObject)B);
3743:   return(0);
3744: }

3748: /*@
3749:    MatCopy - Copys a matrix to another matrix.

3751:    Collective on Mat

3753:    Input Parameters:
3754: +  A - the matrix
3755: -  str - SAME_NONZERO_PATTERN or DIFFERENT_NONZERO_PATTERN

3757:    Output Parameter:
3758: .  B - where the copy is put

3760:    Notes:
3761:    If you use SAME_NONZERO_PATTERN then the two matrices had better have the
3762:    same nonzero pattern or the routine will crash.

3764:    MatCopy() copies the matrix entries of a matrix to another existing
3765:    matrix (after first zeroing the second matrix).  A related routine is
3766:    MatConvert(), which first creates a new matrix and then copies the data.

3768:    Level: intermediate

3770:    Concepts: matrices^copying

3772: .seealso: MatConvert(), MatDuplicate()

3774: @*/
3775: PetscErrorCode MatCopy(Mat A,Mat B,MatStructure str)
3776: {
3778:   PetscInt       i;

3786:   MatCheckPreallocated(B,2);
3787:   if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3788:   if (A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3789:   if (A->rmap->N != B->rmap->N || A->cmap->N != B->cmap->N) SETERRQ4(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Mat A,Mat B: global dim (%D,%D) (%D,%D)",A->rmap->N,B->rmap->N,A->cmap->N,B->cmap->N);
3790:   MatCheckPreallocated(A,1);

3792:   PetscLogEventBegin(MAT_Copy,A,B,0,0);
3793:   if (A->ops->copy) {
3794:     (*A->ops->copy)(A,B,str);
3795:   } else { /* generic conversion */
3796:     MatCopy_Basic(A,B,str);
3797:   }

3799:   B->stencil.dim = A->stencil.dim;
3800:   B->stencil.noc = A->stencil.noc;
3801:   for (i=0; i<=A->stencil.dim; i++) {
3802:     B->stencil.dims[i]   = A->stencil.dims[i];
3803:     B->stencil.starts[i] = A->stencil.starts[i];
3804:   }

3806:   PetscLogEventEnd(MAT_Copy,A,B,0,0);
3807:   PetscObjectStateIncrease((PetscObject)B);
3808:   return(0);
3809: }

3813: /*@C
3814:    MatConvert - Converts a matrix to another matrix, either of the same
3815:    or different type.

3817:    Collective on Mat

3819:    Input Parameters:
3820: +  mat - the matrix
3821: .  newtype - new matrix type.  Use MATSAME to create a new matrix of the
3822:    same type as the original matrix.
3823: -  reuse - denotes if the destination matrix is to be created or reused.
3824:    Use MAT_INPLACE_MATRIX for inplace conversion, otherwise use
3825:    MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX.

3827:    Output Parameter:
3828: .  M - pointer to place new matrix

3830:    Notes:
3831:    MatConvert() first creates a new matrix and then copies the data from
3832:    the first matrix.  A related routine is MatCopy(), which copies the matrix
3833:    entries of one matrix to another already existing matrix context.

3835:    Cannot be used to convert a sequential matrix to parallel or parallel to sequential,
3836:    the MPI communicator of the generated matrix is always the same as the communicator
3837:    of the input matrix.

3839:    Level: intermediate

3841:    Concepts: matrices^converting between storage formats

3843: .seealso: MatCopy(), MatDuplicate()
3844: @*/
3845: PetscErrorCode MatConvert(Mat mat, MatType newtype,MatReuse reuse,Mat *M)
3846: {
3848:   PetscBool      sametype,issame,flg;
3849:   char           convname[256],mtype[256];
3850:   Mat            B;

3856:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3857:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3858:   MatCheckPreallocated(mat,1);
3859:   MatSetOption(mat,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_FALSE);

3861:   PetscOptionsGetString(((PetscObject)mat)->options,((PetscObject)mat)->prefix,"-matconvert_type",mtype,256,&flg);
3862:   if (flg) {
3863:     newtype = mtype;
3864:   }
3865:   PetscObjectTypeCompare((PetscObject)mat,newtype,&sametype);
3866:   PetscStrcmp(newtype,"same",&issame);
3867:   if ((reuse == MAT_INPLACE_MATRIX) && (mat != *M)) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"MAT_INPLACE_MATRIX requires same input and output matrix");

3869:   if ((reuse == MAT_INPLACE_MATRIX) && (issame || sametype)) return(0);

3871:   if ((sametype || issame) && (reuse==MAT_INITIAL_MATRIX) && mat->ops->duplicate) {
3872:     (*mat->ops->duplicate)(mat,MAT_COPY_VALUES,M);
3873:   } else {
3874:     PetscErrorCode (*conv)(Mat, MatType,MatReuse,Mat*)=NULL;
3875:     const char     *prefix[3] = {"seq","mpi",""};
3876:     PetscInt       i;
3877:     /*
3878:        Order of precedence:
3879:        1) See if a specialized converter is known to the current matrix.
3880:        2) See if a specialized converter is known to the desired matrix class.
3881:        3) See if a good general converter is registered for the desired class
3882:           (as of 6/27/03 only MATMPIADJ falls into this category).
3883:        4) See if a good general converter is known for the current matrix.
3884:        5) Use a really basic converter.
3885:     */

3887:     /* 1) See if a specialized converter is known to the current matrix and the desired class */
3888:     for (i=0; i<3; i++) {
3889:       PetscStrcpy(convname,"MatConvert_");
3890:       PetscStrcat(convname,((PetscObject)mat)->type_name);
3891:       PetscStrcat(convname,"_");
3892:       PetscStrcat(convname,prefix[i]);
3893:       PetscStrcat(convname,issame ? ((PetscObject)mat)->type_name : newtype);
3894:       PetscStrcat(convname,"_C");
3895:       PetscObjectQueryFunction((PetscObject)mat,convname,&conv);
3896:       if (conv) goto foundconv;
3897:     }

3899:     /* 2)  See if a specialized converter is known to the desired matrix class. */
3900:     MatCreate(PetscObjectComm((PetscObject)mat),&B);
3901:     MatSetSizes(B,mat->rmap->n,mat->cmap->n,mat->rmap->N,mat->cmap->N);
3902:     MatSetType(B,newtype);
3903:     for (i=0; i<3; i++) {
3904:       PetscStrcpy(convname,"MatConvert_");
3905:       PetscStrcat(convname,((PetscObject)mat)->type_name);
3906:       PetscStrcat(convname,"_");
3907:       PetscStrcat(convname,prefix[i]);
3908:       PetscStrcat(convname,newtype);
3909:       PetscStrcat(convname,"_C");
3910:       PetscObjectQueryFunction((PetscObject)B,convname,&conv);
3911:       if (conv) {
3912:         MatDestroy(&B);
3913:         goto foundconv;
3914:       }
3915:     }

3917:     /* 3) See if a good general converter is registered for the desired class */
3918:     conv = B->ops->convertfrom;
3919:     MatDestroy(&B);
3920:     if (conv) goto foundconv;

3922:     /* 4) See if a good general converter is known for the current matrix */
3923:     if (mat->ops->convert) {
3924:       conv = mat->ops->convert;
3925:     }
3926:     if (conv) goto foundconv;

3928:     /* 5) Use a really basic converter. */
3929:     conv = MatConvert_Basic;

3931: foundconv:
3932:     PetscLogEventBegin(MAT_Convert,mat,0,0,0);
3933:     (*conv)(mat,newtype,reuse,M);
3934:     PetscLogEventEnd(MAT_Convert,mat,0,0,0);
3935:   }
3936:   PetscObjectStateIncrease((PetscObject)*M);

3938:   /* Copy Mat options */
3939:   if (mat->symmetric) {MatSetOption(*M,MAT_SYMMETRIC,PETSC_TRUE);}
3940:   if (mat->hermitian) {MatSetOption(*M,MAT_HERMITIAN,PETSC_TRUE);}
3941:   return(0);
3942: }

3946: /*@C
3947:    MatFactorGetSolverPackage - Returns name of the package providing the factorization routines

3949:    Not Collective

3951:    Input Parameter:
3952: .  mat - the matrix, must be a factored matrix

3954:    Output Parameter:
3955: .   type - the string name of the package (do not free this string)

3957:    Notes:
3958:       In Fortran you pass in a empty string and the package name will be copied into it.
3959:     (Make sure the string is long enough)

3961:    Level: intermediate

3963: .seealso: MatCopy(), MatDuplicate(), MatGetFactorAvailable(), MatGetFactor()
3964: @*/
3965: PetscErrorCode MatFactorGetSolverPackage(Mat mat, const MatSolverPackage *type)
3966: {
3967:   PetscErrorCode ierr, (*conv)(Mat,const MatSolverPackage*);

3972:   if (!mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Only for factored matrix");
3973:   PetscObjectQueryFunction((PetscObject)mat,"MatFactorGetSolverPackage_C",&conv);
3974:   if (!conv) {
3975:     *type = MATSOLVERPETSC;
3976:   } else {
3977:     (*conv)(mat,type);
3978:   }
3979:   return(0);
3980: }

3982: typedef struct _MatSolverPackageForSpecifcType* MatSolverPackageForSpecifcType;
3983: struct _MatSolverPackageForSpecifcType {
3984:   MatType                        mtype;
3985:   PetscErrorCode                 (*getfactor[4])(Mat,MatFactorType,Mat*);
3986:   MatSolverPackageForSpecifcType next;
3987: };

3989: typedef struct _MatSolverPackageHolder* MatSolverPackageHolder;
3990: struct _MatSolverPackageHolder {
3991:   char                           *name;
3992:   MatSolverPackageForSpecifcType handlers;
3993:   MatSolverPackageHolder         next;
3994: };

3996: static MatSolverPackageHolder MatSolverPackageHolders = NULL;

4000: /*@C
4001:    MatSolvePackageRegister - Registers a MatSolverPackage that works for a particular matrix type

4003:    Input Parameters:
4004: +    package - name of the package, for example petsc or superlu
4005: .    mtype - the matrix type that works with this package
4006: .    ftype - the type of factorization supported by the package
4007: -    getfactor - routine that will create the factored matrix ready to be used

4009:     Level: intermediate

4011: .seealso: MatCopy(), MatDuplicate(), MatGetFactorAvailable()
4012: @*/
4013: PetscErrorCode MatSolverPackageRegister(const MatSolverPackage package,const MatType mtype,MatFactorType ftype,PetscErrorCode (*getfactor)(Mat,MatFactorType,Mat*))
4014: {
4015:   PetscErrorCode                 ierr;
4016:   MatSolverPackageHolder         next = MatSolverPackageHolders,prev;
4017:   PetscBool                      flg;
4018:   MatSolverPackageForSpecifcType inext,iprev = NULL;

4021:   if (!MatSolverPackageHolders) {
4022:     PetscNew(&MatSolverPackageHolders);
4023:     PetscStrallocpy(package,&MatSolverPackageHolders->name);
4024:     PetscNew(&MatSolverPackageHolders->handlers);
4025:     PetscStrallocpy(mtype,(char **)&MatSolverPackageHolders->handlers->mtype);
4026:     MatSolverPackageHolders->handlers->getfactor[(int)ftype-1] = getfactor;
4027:     return(0);
4028:   }
4029:   while (next) {
4030:     PetscStrcasecmp(package,next->name,&flg);
4031:     if (flg) {
4032:       inext = next->handlers;
4033:       while (inext) {
4034:         PetscStrcasecmp(mtype,inext->mtype,&flg);
4035:         if (flg) {
4036:           inext->getfactor[(int)ftype-1] = getfactor;
4037:           return(0);
4038:         }
4039:         iprev = inext;
4040:         inext = inext->next;
4041:       }
4042:       PetscNew(&iprev->next);
4043:       PetscStrallocpy(mtype,(char **)&iprev->next->mtype);
4044:       iprev->next->getfactor[(int)ftype-1] = getfactor;
4045:       return(0);
4046:     }
4047:     prev = next;
4048:     next = next->next;
4049:   }
4050:   PetscNew(&prev->next);
4051:   PetscStrallocpy(package,&prev->next->name);
4052:   PetscNew(&prev->next->handlers);
4053:   PetscStrallocpy(mtype,(char **)&prev->next->handlers->mtype);
4054:   prev->next->handlers->getfactor[(int)ftype-1] = getfactor;
4055:   return(0);
4056: }

4060: /*@C
4061:    MatSolvePackageGet - Get's the function that creates the factor matrix if it exist

4063:    Input Parameters:
4064: +    package - name of the package, for example petsc or superlu
4065: .    ftype - the type of factorization supported by the package
4066: -    mtype - the matrix type that works with this package

4068:    Output Parameters:
4069: +   foundpackage - PETSC_TRUE if the package was registered
4070: .   foundmtype - PETSC_TRUE if the package supports the requested mtype
4071: -   getfactor - routine that will create the factored matrix ready to be used or NULL if not found

4073:     Level: intermediate

4075: .seealso: MatCopy(), MatDuplicate(), MatGetFactorAvailable()
4076: @*/
4077: PetscErrorCode MatSolverPackageGet(const MatSolverPackage package,const MatType mtype,MatFactorType ftype,PetscBool *foundpackage,PetscBool *foundmtype,PetscErrorCode (**getfactor)(Mat,MatFactorType,Mat*))
4078: {
4079:   PetscErrorCode                 ierr;
4080:   MatSolverPackageHolder         next = MatSolverPackageHolders;
4081:   PetscBool                      flg;
4082:   MatSolverPackageForSpecifcType inext;

4085:   if (foundpackage) *foundpackage = PETSC_FALSE;
4086:   if (foundmtype)   *foundmtype   = PETSC_FALSE;
4087:   if (getfactor)    *getfactor    = NULL;
4088:   while (next) {
4089:     PetscStrcasecmp(package,next->name,&flg);
4090:     if (flg) {
4091:       if (foundpackage) *foundpackage = PETSC_TRUE;
4092:       inext = next->handlers;
4093:       while (inext) {
4094:         PetscStrcasecmp(mtype,inext->mtype,&flg);
4095:         if (flg) {
4096:           if (foundmtype) *foundmtype = PETSC_TRUE;
4097:           if (getfactor)  *getfactor  = inext->getfactor[(int)ftype-1];
4098:           return(0);
4099:         }
4100:         inext = inext->next;
4101:       }
4102:     }
4103:     next = next->next;
4104:   }
4105:   return(0);
4106: }

4110: PetscErrorCode MatSolverPackageDestroy(void)
4111: {
4112:   PetscErrorCode                 ierr;
4113:   MatSolverPackageHolder         next = MatSolverPackageHolders,prev;
4114:   MatSolverPackageForSpecifcType inext,iprev;

4117:   while (next) {
4118:     PetscFree(next->name);
4119:     inext = next->handlers;
4120:     while (inext) {
4121:       PetscFree(inext->mtype);
4122:       iprev = inext;
4123:       inext = inext->next;
4124:       PetscFree(iprev);
4125:     }
4126:     prev = next;
4127:     next = next->next;
4128:     PetscFree(prev);
4129:   }
4130:   MatSolverPackageHolders = NULL;
4131:   return(0);
4132: }

4136: /*@C
4137:    MatGetFactor - Returns a matrix suitable to calls to MatXXFactorSymbolic()

4139:    Collective on Mat

4141:    Input Parameters:
4142: +  mat - the matrix
4143: .  type - name of solver type, for example, superlu, petsc (to use PETSc's default)
4144: -  ftype - factor type, MAT_FACTOR_LU, MAT_FACTOR_CHOLESKY, MAT_FACTOR_ICC, MAT_FACTOR_ILU,

4146:    Output Parameters:
4147: .  f - the factor matrix used with MatXXFactorSymbolic() calls

4149:    Notes:
4150:       Some PETSc matrix formats have alternative solvers available that are contained in alternative packages
4151:      such as pastix, superlu, mumps etc.

4153:       PETSc must have been ./configure to use the external solver, using the option --download-package

4155:    Level: intermediate

4157: .seealso: MatCopy(), MatDuplicate(), MatGetFactorAvailable()
4158: @*/
4159: PetscErrorCode MatGetFactor(Mat mat, const MatSolverPackage type,MatFactorType ftype,Mat *f)
4160: {
4161:   PetscErrorCode ierr,(*conv)(Mat,MatFactorType,Mat*);
4162:   PetscBool      foundpackage,foundmtype;


4168:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
4169:   MatCheckPreallocated(mat,1);

4171:   MatSolverPackageGet(type,((PetscObject)mat)->type_name,ftype,&foundpackage,&foundmtype,&conv);
4172:   if (!foundpackage) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_MISSING_FACTOR,"Could not locate solver package %s. Perhaps you must ./configure with --download-%s",type,type);
4173:   if (!foundmtype) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_MISSING_FACTOR,"MatSolverPackage %s does not support matrix type %s",type,((PetscObject)mat)->type_name);
4174:   if (!conv) SETERRQ3(PetscObjectComm((PetscObject)mat),PETSC_ERR_MISSING_FACTOR,"MatSolverPackage %s does not support factorization type %s for  matrix type %s",type,MatFactorTypes[ftype],((PetscObject)mat)->type_name);

4176:   (*conv)(mat,ftype,f);
4177:   return(0);
4178: }

4182: /*@C
4183:    MatGetFactorAvailable - Returns a a flag if matrix supports particular package and factor type

4185:    Not Collective

4187:    Input Parameters:
4188: +  mat - the matrix
4189: .  type - name of solver type, for example, superlu, petsc (to use PETSc's default)
4190: -  ftype - factor type, MAT_FACTOR_LU, MAT_FACTOR_CHOLESKY, MAT_FACTOR_ICC, MAT_FACTOR_ILU,

4192:    Output Parameter:
4193: .    flg - PETSC_TRUE if the factorization is available

4195:    Notes:
4196:       Some PETSc matrix formats have alternative solvers available that are contained in alternative packages
4197:      such as pastix, superlu, mumps etc.

4199:       PETSc must have been ./configure to use the external solver, using the option --download-package

4201:    Level: intermediate

4203: .seealso: MatCopy(), MatDuplicate(), MatGetFactor()
4204: @*/
4205: PetscErrorCode MatGetFactorAvailable(Mat mat, const MatSolverPackage type,MatFactorType ftype,PetscBool  *flg)
4206: {
4207:   PetscErrorCode ierr, (*gconv)(Mat,MatFactorType,Mat*);


4213:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
4214:   MatCheckPreallocated(mat,1);

4216:   *flg = PETSC_FALSE;
4217:   MatSolverPackageGet(type,((PetscObject)mat)->type_name,ftype,NULL,NULL,&gconv);
4218:   if (gconv) {
4219:     *flg = PETSC_TRUE;
4220:   }
4221:   return(0);
4222: }

4224: #include <petscdmtypes.h>

4228: /*@
4229:    MatDuplicate - Duplicates a matrix including the non-zero structure.

4231:    Collective on Mat

4233:    Input Parameters:
4234: +  mat - the matrix
4235: -  op - either MAT_DO_NOT_COPY_VALUES or MAT_COPY_VALUES, cause it to copy the numerical values in the matrix
4236:         MAT_SHARE_NONZERO_PATTERN to share the nonzero patterns with the previous matrix and not copy them.

4238:    Output Parameter:
4239: .  M - pointer to place new matrix

4241:    Level: intermediate

4243:    Concepts: matrices^duplicating

4245:     Notes: You cannot change the nonzero pattern for the parent or child matrix if you use MAT_SHARE_NONZERO_PATTERN.

4247: .seealso: MatCopy(), MatConvert()
4248: @*/
4249: PetscErrorCode MatDuplicate(Mat mat,MatDuplicateOption op,Mat *M)
4250: {
4252:   Mat            B;
4253:   PetscInt       i;
4254:   DM             dm;

4260:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
4261:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
4262:   MatCheckPreallocated(mat,1);

4264:   *M = 0;
4265:   if (!mat->ops->duplicate) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Not written for this matrix type");
4266:   PetscLogEventBegin(MAT_Convert,mat,0,0,0);
4267:   (*mat->ops->duplicate)(mat,op,M);
4268:   B    = *M;

4270:   B->stencil.dim = mat->stencil.dim;
4271:   B->stencil.noc = mat->stencil.noc;
4272:   for (i=0; i<=mat->stencil.dim; i++) {
4273:     B->stencil.dims[i]   = mat->stencil.dims[i];
4274:     B->stencil.starts[i] = mat->stencil.starts[i];
4275:   }

4277:   B->nooffproczerorows = mat->nooffproczerorows;
4278:   B->nooffprocentries  = mat->nooffprocentries;

4280:   PetscObjectQuery((PetscObject) mat, "__PETSc_dm", (PetscObject*) &dm);
4281:   if (dm) {
4282:     PetscObjectCompose((PetscObject) B, "__PETSc_dm", (PetscObject) dm);
4283:   }
4284:   PetscLogEventEnd(MAT_Convert,mat,0,0,0);
4285:   PetscObjectStateIncrease((PetscObject)B);
4286:   return(0);
4287: }

4291: /*@
4292:    MatGetDiagonal - Gets the diagonal of a matrix.

4294:    Logically Collective on Mat and Vec

4296:    Input Parameters:
4297: +  mat - the matrix
4298: -  v - the vector for storing the diagonal

4300:    Output Parameter:
4301: .  v - the diagonal of the matrix

4303:    Level: intermediate

4305:    Note:
4306:    Currently only correct in parallel for square matrices.

4308:    Concepts: matrices^accessing diagonals

4310: .seealso: MatGetRow(), MatGetSubMatrices(), MatGetSubmatrix(), MatGetRowMaxAbs()
4311: @*/
4312: PetscErrorCode MatGetDiagonal(Mat mat,Vec v)
4313: {

4320:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
4321:   if (!mat->ops->getdiagonal) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
4322:   MatCheckPreallocated(mat,1);

4324:   (*mat->ops->getdiagonal)(mat,v);
4325:   PetscObjectStateIncrease((PetscObject)v);
4326:   return(0);
4327: }

4331: /*@C
4332:    MatGetRowMin - Gets the minimum value (of the real part) of each
4333:         row of the matrix

4335:    Logically Collective on Mat and Vec

4337:    Input Parameters:
4338: .  mat - the matrix

4340:    Output Parameter:
4341: +  v - the vector for storing the maximums
4342: -  idx - the indices of the column found for each row (optional)

4344:    Level: intermediate

4346:    Notes: The result of this call are the same as if one converted the matrix to dense format
4347:       and found the minimum value in each row (i.e. the implicit zeros are counted as zeros).

4349:     This code is only implemented for a couple of matrix formats.

4351:    Concepts: matrices^getting row maximums

4353: .seealso: MatGetDiagonal(), MatGetSubMatrices(), MatGetSubmatrix(), MatGetRowMaxAbs(),
4354:           MatGetRowMax()
4355: @*/
4356: PetscErrorCode MatGetRowMin(Mat mat,Vec v,PetscInt idx[])
4357: {

4364:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
4365:   if (!mat->ops->getrowmax) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
4366:   MatCheckPreallocated(mat,1);

4368:   (*mat->ops->getrowmin)(mat,v,idx);
4369:   PetscObjectStateIncrease((PetscObject)v);
4370:   return(0);
4371: }

4375: /*@C
4376:    MatGetRowMinAbs - Gets the minimum value (in absolute value) of each
4377:         row of the matrix

4379:    Logically Collective on Mat and Vec

4381:    Input Parameters:
4382: .  mat - the matrix

4384:    Output Parameter:
4385: +  v - the vector for storing the minimums
4386: -  idx - the indices of the column found for each row (or NULL if not needed)

4388:    Level: intermediate

4390:    Notes: if a row is completely empty or has only 0.0 values then the idx[] value for that
4391:     row is 0 (the first column).

4393:     This code is only implemented for a couple of matrix formats.

4395:    Concepts: matrices^getting row maximums

4397: .seealso: MatGetDiagonal(), MatGetSubMatrices(), MatGetSubmatrix(), MatGetRowMax(), MatGetRowMaxAbs(), MatGetRowMin()
4398: @*/
4399: PetscErrorCode MatGetRowMinAbs(Mat mat,Vec v,PetscInt idx[])
4400: {

4407:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
4408:   if (!mat->ops->getrowminabs) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
4409:   MatCheckPreallocated(mat,1);
4410:   if (idx) {PetscMemzero(idx,mat->rmap->n*sizeof(PetscInt));}

4412:   (*mat->ops->getrowminabs)(mat,v,idx);
4413:   PetscObjectStateIncrease((PetscObject)v);
4414:   return(0);
4415: }

4419: /*@C
4420:    MatGetRowMax - Gets the maximum value (of the real part) of each
4421:         row of the matrix

4423:    Logically Collective on Mat and Vec

4425:    Input Parameters:
4426: .  mat - the matrix

4428:    Output Parameter:
4429: +  v - the vector for storing the maximums
4430: -  idx - the indices of the column found for each row (optional)

4432:    Level: intermediate

4434:    Notes: The result of this call are the same as if one converted the matrix to dense format
4435:       and found the minimum value in each row (i.e. the implicit zeros are counted as zeros).

4437:     This code is only implemented for a couple of matrix formats.

4439:    Concepts: matrices^getting row maximums

4441: .seealso: MatGetDiagonal(), MatGetSubMatrices(), MatGetSubmatrix(), MatGetRowMaxAbs(), MatGetRowMin()
4442: @*/
4443: PetscErrorCode MatGetRowMax(Mat mat,Vec v,PetscInt idx[])
4444: {

4451:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
4452:   if (!mat->ops->getrowmax) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
4453:   MatCheckPreallocated(mat,1);

4455:   (*mat->ops->getrowmax)(mat,v,idx);
4456:   PetscObjectStateIncrease((PetscObject)v);
4457:   return(0);
4458: }

4462: /*@C
4463:    MatGetRowMaxAbs - Gets the maximum value (in absolute value) of each
4464:         row of the matrix

4466:    Logically Collective on Mat and Vec

4468:    Input Parameters:
4469: .  mat - the matrix

4471:    Output Parameter:
4472: +  v - the vector for storing the maximums
4473: -  idx - the indices of the column found for each row (or NULL if not needed)

4475:    Level: intermediate

4477:    Notes: if a row is completely empty or has only 0.0 values then the idx[] value for that
4478:     row is 0 (the first column).

4480:     This code is only implemented for a couple of matrix formats.

4482:    Concepts: matrices^getting row maximums

4484: .seealso: MatGetDiagonal(), MatGetSubMatrices(), MatGetSubmatrix(), MatGetRowMax(), MatGetRowMin()
4485: @*/
4486: PetscErrorCode MatGetRowMaxAbs(Mat mat,Vec v,PetscInt idx[])
4487: {

4494:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
4495:   if (!mat->ops->getrowmaxabs) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
4496:   MatCheckPreallocated(mat,1);
4497:   if (idx) {PetscMemzero(idx,mat->rmap->n*sizeof(PetscInt));}

4499:   (*mat->ops->getrowmaxabs)(mat,v,idx);
4500:   PetscObjectStateIncrease((PetscObject)v);
4501:   return(0);
4502: }

4506: /*@
4507:    MatGetRowSum - Gets the sum of each row of the matrix

4509:    Logically Collective on Mat and Vec

4511:    Input Parameters:
4512: .  mat - the matrix

4514:    Output Parameter:
4515: .  v - the vector for storing the sum of rows

4517:    Level: intermediate

4519:    Notes: This code is slow since it is not currently specialized for different formats

4521:    Concepts: matrices^getting row sums

4523: .seealso: MatGetDiagonal(), MatGetSubMatrices(), MatGetSubmatrix(), MatGetRowMax(), MatGetRowMin()
4524: @*/
4525: PetscErrorCode MatGetRowSum(Mat mat, Vec v)
4526: {
4527:   PetscInt       start = 0, end = 0, row;
4528:   PetscScalar    *array;

4535:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
4536:   MatCheckPreallocated(mat,1);
4537:   MatGetOwnershipRange(mat, &start, &end);
4538:   VecGetArray(v, &array);
4539:   for (row = start; row < end; ++row) {
4540:     PetscInt          ncols, col;
4541:     const PetscInt    *cols;
4542:     const PetscScalar *vals;

4544:     array[row - start] = 0.0;

4546:     MatGetRow(mat, row, &ncols, &cols, &vals);
4547:     for (col = 0; col < ncols; col++) {
4548:       array[row - start] += vals[col];
4549:     }
4550:     MatRestoreRow(mat, row, &ncols, &cols, &vals);
4551:   }
4552:   VecRestoreArray(v, &array);
4553:   PetscObjectStateIncrease((PetscObject) v);
4554:   return(0);
4555: }

4559: /*@
4560:    MatTranspose - Computes an in-place or out-of-place transpose of a matrix.

4562:    Collective on Mat

4564:    Input Parameter:
4565: +  mat - the matrix to transpose
4566: -  reuse - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX

4568:    Output Parameters:
4569: .  B - the transpose

4571:    Notes:
4572:      If you  pass in &mat for B the transpose will be done in place, for example MatTranspose(mat,MAT_REUSE_MATRIX,&mat);

4574:      Consider using MatCreateTranspose() instead if you only need a matrix that behaves like the transpose, but don't need the storage to be changed.

4576:    Level: intermediate

4578:    Concepts: matrices^transposing

4580: .seealso: MatMultTranspose(), MatMultTransposeAdd(), MatIsTranspose(), MatReuse
4581: @*/
4582: PetscErrorCode MatTranspose(Mat mat,MatReuse reuse,Mat *B)
4583: {

4589:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
4590:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
4591:   if (!mat->ops->transpose) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
4592:   MatCheckPreallocated(mat,1);

4594:   PetscLogEventBegin(MAT_Transpose,mat,0,0,0);
4595:   (*mat->ops->transpose)(mat,reuse,B);
4596:   PetscLogEventEnd(MAT_Transpose,mat,0,0,0);
4597:   if (B) {PetscObjectStateIncrease((PetscObject)*B);}
4598:   return(0);
4599: }

4603: /*@
4604:    MatIsTranspose - Test whether a matrix is another one's transpose,
4605:         or its own, in which case it tests symmetry.

4607:    Collective on Mat

4609:    Input Parameter:
4610: +  A - the matrix to test
4611: -  B - the matrix to test against, this can equal the first parameter

4613:    Output Parameters:
4614: .  flg - the result

4616:    Notes:
4617:    Only available for SeqAIJ/MPIAIJ matrices. The sequential algorithm
4618:    has a running time of the order of the number of nonzeros; the parallel
4619:    test involves parallel copies of the block-offdiagonal parts of the matrix.

4621:    Level: intermediate

4623:    Concepts: matrices^transposing, matrix^symmetry

4625: .seealso: MatTranspose(), MatIsSymmetric(), MatIsHermitian()
4626: @*/
4627: PetscErrorCode MatIsTranspose(Mat A,Mat B,PetscReal tol,PetscBool  *flg)
4628: {
4629:   PetscErrorCode ierr,(*f)(Mat,Mat,PetscReal,PetscBool*),(*g)(Mat,Mat,PetscReal,PetscBool*);

4635:   PetscObjectQueryFunction((PetscObject)A,"MatIsTranspose_C",&f);
4636:   PetscObjectQueryFunction((PetscObject)B,"MatIsTranspose_C",&g);
4637:   *flg = PETSC_FALSE;
4638:   if (f && g) {
4639:     if (f == g) {
4640:       (*f)(A,B,tol,flg);
4641:     } else SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_NOTSAMETYPE,"Matrices do not have the same comparator for symmetry test");
4642:   } else {
4643:     MatType mattype;
4644:     if (!f) {
4645:       MatGetType(A,&mattype);
4646:     } else {
4647:       MatGetType(B,&mattype);
4648:     }
4649:     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type <%s> does not support checking for transpose",mattype);
4650:   }
4651:   return(0);
4652: }

4656: /*@
4657:    MatHermitianTranspose - Computes an in-place or out-of-place transpose of a matrix in complex conjugate.

4659:    Collective on Mat

4661:    Input Parameter:
4662: +  mat - the matrix to transpose and complex conjugate
4663: -  reuse - store the transpose matrix in the provided B

4665:    Output Parameters:
4666: .  B - the Hermitian

4668:    Notes:
4669:      If you  pass in &mat for B the Hermitian will be done in place

4671:    Level: intermediate

4673:    Concepts: matrices^transposing, complex conjugatex

4675: .seealso: MatTranspose(), MatMultTranspose(), MatMultTransposeAdd(), MatIsTranspose(), MatReuse
4676: @*/
4677: PetscErrorCode MatHermitianTranspose(Mat mat,MatReuse reuse,Mat *B)
4678: {

4682:   MatTranspose(mat,reuse,B);
4683: #if defined(PETSC_USE_COMPLEX)
4684:   MatConjugate(*B);
4685: #endif
4686:   return(0);
4687: }

4691: /*@
4692:    MatIsHermitianTranspose - Test whether a matrix is another one's Hermitian transpose,

4694:    Collective on Mat

4696:    Input Parameter:
4697: +  A - the matrix to test
4698: -  B - the matrix to test against, this can equal the first parameter

4700:    Output Parameters:
4701: .  flg - the result

4703:    Notes:
4704:    Only available for SeqAIJ/MPIAIJ matrices. The sequential algorithm
4705:    has a running time of the order of the number of nonzeros; the parallel
4706:    test involves parallel copies of the block-offdiagonal parts of the matrix.

4708:    Level: intermediate

4710:    Concepts: matrices^transposing, matrix^symmetry

4712: .seealso: MatTranspose(), MatIsSymmetric(), MatIsHermitian(), MatIsTranspose()
4713: @*/
4714: PetscErrorCode MatIsHermitianTranspose(Mat A,Mat B,PetscReal tol,PetscBool  *flg)
4715: {
4716:   PetscErrorCode ierr,(*f)(Mat,Mat,PetscReal,PetscBool*),(*g)(Mat,Mat,PetscReal,PetscBool*);

4722:   PetscObjectQueryFunction((PetscObject)A,"MatIsHermitianTranspose_C",&f);
4723:   PetscObjectQueryFunction((PetscObject)B,"MatIsHermitianTranspose_C",&g);
4724:   if (f && g) {
4725:     if (f==g) {
4726:       (*f)(A,B,tol,flg);
4727:     } else SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_NOTSAMETYPE,"Matrices do not have the same comparator for Hermitian test");
4728:   }
4729:   return(0);
4730: }

4734: /*@
4735:    MatPermute - Creates a new matrix with rows and columns permuted from the
4736:    original.

4738:    Collective on Mat

4740:    Input Parameters:
4741: +  mat - the matrix to permute
4742: .  row - row permutation, each processor supplies only the permutation for its rows
4743: -  col - column permutation, each processor supplies only the permutation for its columns

4745:    Output Parameters:
4746: .  B - the permuted matrix

4748:    Level: advanced

4750:    Note:
4751:    The index sets map from row/col of permuted matrix to row/col of original matrix.
4752:    The index sets should be on the same communicator as Mat and have the same local sizes.

4754:    Concepts: matrices^permuting

4756: .seealso: MatGetOrdering(), ISAllGather()

4758: @*/
4759: PetscErrorCode MatPermute(Mat mat,IS row,IS col,Mat *B)
4760: {

4769:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
4770:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
4771:   if (!mat->ops->permute) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"MatPermute not available for Mat type %s",((PetscObject)mat)->type_name);
4772:   MatCheckPreallocated(mat,1);

4774:   (*mat->ops->permute)(mat,row,col,B);
4775:   PetscObjectStateIncrease((PetscObject)*B);
4776:   return(0);
4777: }

4781: /*@
4782:    MatEqual - Compares two matrices.

4784:    Collective on Mat

4786:    Input Parameters:
4787: +  A - the first matrix
4788: -  B - the second matrix

4790:    Output Parameter:
4791: .  flg - PETSC_TRUE if the matrices are equal; PETSC_FALSE otherwise.

4793:    Level: intermediate

4795:    Concepts: matrices^equality between
4796: @*/
4797: PetscErrorCode MatEqual(Mat A,Mat B,PetscBool  *flg)
4798: {

4808:   MatCheckPreallocated(B,2);
4809:   if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
4810:   if (!B->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
4811:   if (A->rmap->N != B->rmap->N || A->cmap->N != B->cmap->N) SETERRQ4(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Mat A,Mat B: global dim %D %D %D %D",A->rmap->N,B->rmap->N,A->cmap->N,B->cmap->N);
4812:   if (!A->ops->equal) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Mat type %s",((PetscObject)A)->type_name);
4813:   if (!B->ops->equal) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Mat type %s",((PetscObject)B)->type_name);
4814:   if (A->ops->equal != B->ops->equal) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_INCOMP,"A is type: %s\nB is type: %s",((PetscObject)A)->type_name,((PetscObject)B)->type_name);
4815:   MatCheckPreallocated(A,1);

4817:   (*A->ops->equal)(A,B,flg);
4818:   return(0);
4819: }

4823: /*@
4824:    MatDiagonalScale - Scales a matrix on the left and right by diagonal
4825:    matrices that are stored as vectors.  Either of the two scaling
4826:    matrices can be NULL.

4828:    Collective on Mat

4830:    Input Parameters:
4831: +  mat - the matrix to be scaled
4832: .  l - the left scaling vector (or NULL)
4833: -  r - the right scaling vector (or NULL)

4835:    Notes:
4836:    MatDiagonalScale() computes A = LAR, where
4837:    L = a diagonal matrix (stored as a vector), R = a diagonal matrix (stored as a vector)
4838:    The L scales the rows of the matrix, the R scales the columns of the matrix.

4840:    Level: intermediate

4842:    Concepts: matrices^diagonal scaling
4843:    Concepts: diagonal scaling of matrices

4845: .seealso: MatScale()
4846: @*/
4847: PetscErrorCode MatDiagonalScale(Mat mat,Vec l,Vec r)
4848: {

4854:   if (!mat->ops->diagonalscale) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
4857:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
4858:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
4859:   MatCheckPreallocated(mat,1);

4861:   PetscLogEventBegin(MAT_Scale,mat,0,0,0);
4862:   (*mat->ops->diagonalscale)(mat,l,r);
4863:   PetscLogEventEnd(MAT_Scale,mat,0,0,0);
4864:   PetscObjectStateIncrease((PetscObject)mat);
4865: #if defined(PETSC_HAVE_CUSP)
4866:   if (mat->valid_GPU_matrix != PETSC_CUSP_UNALLOCATED) {
4867:     mat->valid_GPU_matrix = PETSC_CUSP_CPU;
4868:   }
4869: #endif
4870: #if defined(PETSC_HAVE_VIENNACL)
4871:   if (mat->valid_GPU_matrix != PETSC_VIENNACL_UNALLOCATED) {
4872:     mat->valid_GPU_matrix = PETSC_VIENNACL_CPU;
4873:   }
4874: #endif
4875:   return(0);
4876: }

4880: /*@
4881:     MatScale - Scales all elements of a matrix by a given number.

4883:     Logically Collective on Mat

4885:     Input Parameters:
4886: +   mat - the matrix to be scaled
4887: -   a  - the scaling value

4889:     Output Parameter:
4890: .   mat - the scaled matrix

4892:     Level: intermediate

4894:     Concepts: matrices^scaling all entries

4896: .seealso: MatDiagonalScale()
4897: @*/
4898: PetscErrorCode MatScale(Mat mat,PetscScalar a)
4899: {

4905:   if (a != (PetscScalar)1.0 && !mat->ops->scale) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
4906:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
4907:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
4909:   MatCheckPreallocated(mat,1);

4911:   PetscLogEventBegin(MAT_Scale,mat,0,0,0);
4912:   if (a != (PetscScalar)1.0) {
4913:     (*mat->ops->scale)(mat,a);
4914:     PetscObjectStateIncrease((PetscObject)mat);
4915:   }
4916:   PetscLogEventEnd(MAT_Scale,mat,0,0,0);
4917: #if defined(PETSC_HAVE_CUSP)
4918:   if (mat->valid_GPU_matrix != PETSC_CUSP_UNALLOCATED) {
4919:     mat->valid_GPU_matrix = PETSC_CUSP_CPU;
4920:   }
4921: #endif
4922: #if defined(PETSC_HAVE_VIENNACL)
4923:   if (mat->valid_GPU_matrix != PETSC_VIENNACL_UNALLOCATED) {
4924:     mat->valid_GPU_matrix = PETSC_VIENNACL_CPU;
4925:   }
4926: #endif
4927:   return(0);
4928: }

4932: /*@
4933:    MatNorm - Calculates various norms of a matrix.

4935:    Collective on Mat

4937:    Input Parameters:
4938: +  mat - the matrix
4939: -  type - the type of norm, NORM_1, NORM_FROBENIUS, NORM_INFINITY

4941:    Output Parameters:
4942: .  nrm - the resulting norm

4944:    Level: intermediate

4946:    Concepts: matrices^norm
4947:    Concepts: norm^of matrix
4948: @*/
4949: PetscErrorCode MatNorm(Mat mat,NormType type,PetscReal *nrm)
4950: {


4958:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
4959:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
4960:   if (!mat->ops->norm) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
4961:   MatCheckPreallocated(mat,1);

4963:   (*mat->ops->norm)(mat,type,nrm);
4964:   return(0);
4965: }

4967: /*
4968:      This variable is used to prevent counting of MatAssemblyBegin() that
4969:    are called from within a MatAssemblyEnd().
4970: */
4971: static PetscInt MatAssemblyEnd_InUse = 0;
4974: /*@
4975:    MatAssemblyBegin - Begins assembling the matrix.  This routine should
4976:    be called after completing all calls to MatSetValues().

4978:    Collective on Mat

4980:    Input Parameters:
4981: +  mat - the matrix
4982: -  type - type of assembly, either MAT_FLUSH_ASSEMBLY or MAT_FINAL_ASSEMBLY

4984:    Notes:
4985:    MatSetValues() generally caches the values.  The matrix is ready to
4986:    use only after MatAssemblyBegin() and MatAssemblyEnd() have been called.
4987:    Use MAT_FLUSH_ASSEMBLY when switching between ADD_VALUES and INSERT_VALUES
4988:    in MatSetValues(); use MAT_FINAL_ASSEMBLY for the final assembly before
4989:    using the matrix.

4991:    ALL processes that share a matrix MUST call MatAssemblyBegin() and MatAssemblyEnd() the SAME NUMBER of times, and each time with the
4992:    same flag of MAT_FLUSH_ASSEMBLY or MAT_FINAL_ASSEMBLY for all processes. Thus you CANNOT locally change from ADD_VALUES to INSERT_VALUES, that is
4993:    a global collective operation requring all processes that share the matrix.

4995:    Space for preallocated nonzeros that is not filled by a call to MatSetValues() or a related routine are compressed
4996:    out by assembly. If you intend to use that extra space on a subsequent assembly, be sure to insert explicit zeros
4997:    before MAT_FINAL_ASSEMBLY so the space is not compressed out.

4999:    Level: beginner

5001:    Concepts: matrices^assembling

5003: .seealso: MatAssemblyEnd(), MatSetValues(), MatAssembled()
5004: @*/
5005: PetscErrorCode MatAssemblyBegin(Mat mat,MatAssemblyType type)
5006: {

5012:   MatCheckPreallocated(mat,1);
5013:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix.\nDid you forget to call MatSetUnfactored()?");
5014:   if (mat->assembled) {
5015:     mat->was_assembled = PETSC_TRUE;
5016:     mat->assembled     = PETSC_FALSE;
5017:   }
5018:   if (!MatAssemblyEnd_InUse) {
5019:     PetscLogEventBegin(MAT_AssemblyBegin,mat,0,0,0);
5020:     if (mat->ops->assemblybegin) {(*mat->ops->assemblybegin)(mat,type);}
5021:     PetscLogEventEnd(MAT_AssemblyBegin,mat,0,0,0);
5022:   } else if (mat->ops->assemblybegin) {
5023:     (*mat->ops->assemblybegin)(mat,type);
5024:   }
5025:   return(0);
5026: }

5030: /*@
5031:    MatAssembled - Indicates if a matrix has been assembled and is ready for
5032:      use; for example, in matrix-vector product.

5034:    Not Collective

5036:    Input Parameter:
5037: .  mat - the matrix

5039:    Output Parameter:
5040: .  assembled - PETSC_TRUE or PETSC_FALSE

5042:    Level: advanced

5044:    Concepts: matrices^assembled?

5046: .seealso: MatAssemblyEnd(), MatSetValues(), MatAssemblyBegin()
5047: @*/
5048: PetscErrorCode MatAssembled(Mat mat,PetscBool  *assembled)
5049: {
5054:   *assembled = mat->assembled;
5055:   return(0);
5056: }

5060: /*@
5061:    MatAssemblyEnd - Completes assembling the matrix.  This routine should
5062:    be called after MatAssemblyBegin().

5064:    Collective on Mat

5066:    Input Parameters:
5067: +  mat - the matrix
5068: -  type - type of assembly, either MAT_FLUSH_ASSEMBLY or MAT_FINAL_ASSEMBLY

5070:    Options Database Keys:
5071: +  -mat_view ::ascii_info - Prints info on matrix at conclusion of MatEndAssembly()
5072: .  -mat_view ::ascii_info_detail - Prints more detailed info
5073: .  -mat_view - Prints matrix in ASCII format
5074: .  -mat_view ::ascii_matlab - Prints matrix in Matlab format
5075: .  -mat_view draw - PetscDraws nonzero structure of matrix, using MatView() and PetscDrawOpenX().
5076: .  -display <name> - Sets display name (default is host)
5077: .  -draw_pause <sec> - Sets number of seconds to pause after display
5078: .  -mat_view socket - Sends matrix to socket, can be accessed from Matlab (See Users-Manual: Chapter 11 Using MATLAB with PETSc )
5079: .  -viewer_socket_machine <machine> - Machine to use for socket
5080: .  -viewer_socket_port <port> - Port number to use for socket
5081: -  -mat_view binary:filename[:append] - Save matrix to file in binary format

5083:    Notes:
5084:    MatSetValues() generally caches the values.  The matrix is ready to
5085:    use only after MatAssemblyBegin() and MatAssemblyEnd() have been called.
5086:    Use MAT_FLUSH_ASSEMBLY when switching between ADD_VALUES and INSERT_VALUES
5087:    in MatSetValues(); use MAT_FINAL_ASSEMBLY for the final assembly before
5088:    using the matrix.

5090:    Space for preallocated nonzeros that is not filled by a call to MatSetValues() or a related routine are compressed
5091:    out by assembly. If you intend to use that extra space on a subsequent assembly, be sure to insert explicit zeros
5092:    before MAT_FINAL_ASSEMBLY so the space is not compressed out.

5094:    Level: beginner

5096: .seealso: MatAssemblyBegin(), MatSetValues(), PetscDrawOpenX(), PetscDrawCreate(), MatView(), MatAssembled(), PetscViewerSocketOpen()
5097: @*/
5098: PetscErrorCode MatAssemblyEnd(Mat mat,MatAssemblyType type)
5099: {
5100:   PetscErrorCode  ierr;
5101:   static PetscInt inassm = 0;
5102:   PetscBool       flg    = PETSC_FALSE;


5108:   inassm++;
5109:   MatAssemblyEnd_InUse++;
5110:   if (MatAssemblyEnd_InUse == 1) { /* Do the logging only the first time through */
5111:     PetscLogEventBegin(MAT_AssemblyEnd,mat,0,0,0);
5112:     if (mat->ops->assemblyend) {
5113:       (*mat->ops->assemblyend)(mat,type);
5114:     }
5115:     PetscLogEventEnd(MAT_AssemblyEnd,mat,0,0,0);
5116:   } else if (mat->ops->assemblyend) {
5117:     (*mat->ops->assemblyend)(mat,type);
5118:   }

5120:   /* Flush assembly is not a true assembly */
5121:   if (type != MAT_FLUSH_ASSEMBLY) {
5122:     mat->assembled = PETSC_TRUE; mat->num_ass++;
5123:   }
5124:   mat->insertmode = NOT_SET_VALUES;
5125:   MatAssemblyEnd_InUse--;
5126:   PetscObjectStateIncrease((PetscObject)mat);
5127:   if (!mat->symmetric_eternal) {
5128:     mat->symmetric_set              = PETSC_FALSE;
5129:     mat->hermitian_set              = PETSC_FALSE;
5130:     mat->structurally_symmetric_set = PETSC_FALSE;
5131:   }
5132: #if defined(PETSC_HAVE_CUSP)
5133:   if (mat->valid_GPU_matrix != PETSC_CUSP_UNALLOCATED) {
5134:     mat->valid_GPU_matrix = PETSC_CUSP_CPU;
5135:   }
5136: #endif
5137: #if defined(PETSC_HAVE_VIENNACL)
5138:   if (mat->valid_GPU_matrix != PETSC_VIENNACL_UNALLOCATED) {
5139:     mat->valid_GPU_matrix = PETSC_VIENNACL_CPU;
5140:   }
5141: #endif
5142:   if (inassm == 1 && type != MAT_FLUSH_ASSEMBLY) {
5143:     MatViewFromOptions(mat,NULL,"-mat_view");

5145:     if (mat->checksymmetryonassembly) {
5146:       MatIsSymmetric(mat,mat->checksymmetrytol,&flg);
5147:       if (flg) {
5148:         PetscPrintf(PetscObjectComm((PetscObject)mat),"Matrix is symmetric (tolerance %g)\n",(double)mat->checksymmetrytol);
5149:       } else {
5150:         PetscPrintf(PetscObjectComm((PetscObject)mat),"Matrix is not symmetric (tolerance %g)\n",(double)mat->checksymmetrytol);
5151:       }
5152:     }
5153:     if (mat->nullsp && mat->checknullspaceonassembly) {
5154:       MatNullSpaceTest(mat->nullsp,mat,NULL);
5155:     }
5156:   }
5157:   inassm--;
5158:   return(0);
5159: }

5163: /*@
5164:    MatSetOption - Sets a parameter option for a matrix. Some options
5165:    may be specific to certain storage formats.  Some options
5166:    determine how values will be inserted (or added). Sorted,
5167:    row-oriented input will generally assemble the fastest. The default
5168:    is row-oriented.

5170:    Logically Collective on Mat for certain operations, such as MAT_SPD, not collective for MAT_ROW_ORIENTED, see MatOption

5172:    Input Parameters:
5173: +  mat - the matrix
5174: .  option - the option, one of those listed below (and possibly others),
5175: -  flg - turn the option on (PETSC_TRUE) or off (PETSC_FALSE)

5177:   Options Describing Matrix Structure:
5178: +    MAT_SPD - symmetric positive definite
5179: .    MAT_SYMMETRIC - symmetric in terms of both structure and value
5180: .    MAT_HERMITIAN - transpose is the complex conjugation
5181: .    MAT_STRUCTURALLY_SYMMETRIC - symmetric nonzero structure
5182: -    MAT_SYMMETRY_ETERNAL - if you would like the symmetry/Hermitian flag
5183:                             you set to be kept with all future use of the matrix
5184:                             including after MatAssemblyBegin/End() which could
5185:                             potentially change the symmetry structure, i.e. you
5186:                             KNOW the matrix will ALWAYS have the property you set.


5189:    Options For Use with MatSetValues():
5190:    Insert a logically dense subblock, which can be
5191: .    MAT_ROW_ORIENTED - row-oriented (default)

5193:    Note these options reflect the data you pass in with MatSetValues(); it has
5194:    nothing to do with how the data is stored internally in the matrix
5195:    data structure.

5197:    When (re)assembling a matrix, we can restrict the input for
5198:    efficiency/debugging purposes.  These options include:
5199: +    MAT_NEW_NONZERO_LOCATIONS - additional insertions will be allowed if they generate a new nonzero (slow)
5200: .    MAT_NEW_DIAGONALS - new diagonals will be allowed (for block diagonal format only)
5201: .    MAT_IGNORE_OFF_PROC_ENTRIES - drops off-processor entries
5202: .    MAT_NEW_NONZERO_LOCATION_ERR - generates an error for new matrix entry
5203: .    MAT_USE_HASH_TABLE - uses a hash table to speed up matrix assembly
5204: .    MAT_NO_OFF_PROC_ENTRIES - you know each process will only set values for its own rows, will generate an error if
5205:         any process sets values for another process. This avoids all reductions in the MatAssembly routines and thus improves
5206:         performance for very large process counts.
5207: -    MAT_SUBSET_OFF_PROC_ENTRIES - you know that the first assembly after setting this flag will set a superset
5208:         of the off-process entries required for all subsequent assemblies. This avoids a rendezvous step in the MatAssembly
5209:         functions, instead sending only neighbor messages.

5211:    Notes:
5212:    Except for MAT_UNUSED_NONZERO_LOCATION_ERR and  MAT_ROW_ORIENTED all processes that share the matrix must pass the same value in flg!

5214:    Some options are relevant only for particular matrix types and
5215:    are thus ignored by others.  Other options are not supported by
5216:    certain matrix types and will generate an error message if set.

5218:    If using a Fortran 77 module to compute a matrix, one may need to
5219:    use the column-oriented option (or convert to the row-oriented
5220:    format).

5222:    MAT_NEW_NONZERO_LOCATIONS set to PETSC_FALSE indicates that any add or insertion
5223:    that would generate a new entry in the nonzero structure is instead
5224:    ignored.  Thus, if memory has not alredy been allocated for this particular
5225:    data, then the insertion is ignored. For dense matrices, in which
5226:    the entire array is allocated, no entries are ever ignored.
5227:    Set after the first MatAssemblyEnd(). If this option is set then the MatAssemblyBegin/End() processes has one less global reduction

5229:    MAT_NEW_NONZERO_LOCATION_ERR set to PETSC_TRUE indicates that any add or insertion
5230:    that would generate a new entry in the nonzero structure instead produces
5231:    an error. (Currently supported for AIJ and BAIJ formats only.) If this option is set then the MatAssemblyBegin/End() processes has one less global reduction

5233:    MAT_NEW_NONZERO_ALLOCATION_ERR set to PETSC_TRUE indicates that any add or insertion
5234:    that would generate a new entry that has not been preallocated will
5235:    instead produce an error. (Currently supported for AIJ and BAIJ formats
5236:    only.) This is a useful flag when debugging matrix memory preallocation.
5237:    If this option is set then the MatAssemblyBegin/End() processes has one less global reduction

5239:    MAT_IGNORE_OFF_PROC_ENTRIES set to PETSC_TRUE indicates entries destined for
5240:    other processors should be dropped, rather than stashed.
5241:    This is useful if you know that the "owning" processor is also
5242:    always generating the correct matrix entries, so that PETSc need
5243:    not transfer duplicate entries generated on another processor.

5245:    MAT_USE_HASH_TABLE indicates that a hash table be used to improve the
5246:    searches during matrix assembly. When this flag is set, the hash table
5247:    is created during the first Matrix Assembly. This hash table is
5248:    used the next time through, during MatSetVaules()/MatSetVaulesBlocked()
5249:    to improve the searching of indices. MAT_NEW_NONZERO_LOCATIONS flag
5250:    should be used with MAT_USE_HASH_TABLE flag. This option is currently
5251:    supported by MATMPIBAIJ format only.

5253:    MAT_KEEP_NONZERO_PATTERN indicates when MatZeroRows() is called the zeroed entries
5254:    are kept in the nonzero structure

5256:    MAT_IGNORE_ZERO_ENTRIES - for AIJ/IS matrices this will stop zero values from creating
5257:    a zero location in the matrix

5259:    MAT_USE_INODES - indicates using inode version of the code - works with AIJ matrix types

5261:    MAT_NO_OFF_PROC_ZERO_ROWS - you know each process will only zero its own rows. This avoids all reductions in the
5262:         zero row routines and thus improves performance for very large process counts.

5264:    MAT_IGNORE_LOWER_TRIANGULAR - For SBAIJ matrices will ignore any insertions you make in the lower triangular
5265:         part of the matrix (since they should match the upper triangular part).

5267:    Notes: Can only be called after MatSetSizes() and MatSetType() have been set.

5269:    Level: intermediate

5271:    Concepts: matrices^setting options

5273: .seealso:  MatOption, Mat

5275: @*/
5276: PetscErrorCode MatSetOption(Mat mat,MatOption op,PetscBool flg)
5277: {

5283:   if (op > 0) {
5286:   }

5288:   if (((int) op) <= MAT_OPTION_MIN || ((int) op) >= MAT_OPTION_MAX) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"Options %d is out of range",(int)op);
5289:   if (!((PetscObject)mat)->type_name) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_TYPENOTSET,"Cannot set options until type and size have been set, see MatSetType() and MatSetSizes()");

5291:   switch (op) {
5292:   case MAT_NO_OFF_PROC_ENTRIES:
5293:     mat->nooffprocentries = flg;
5294:     return(0);
5295:     break;
5296:   case MAT_SUBSET_OFF_PROC_ENTRIES:
5297:     mat->subsetoffprocentries = flg;
5298:     return(0);
5299:   case MAT_NO_OFF_PROC_ZERO_ROWS:
5300:     mat->nooffproczerorows = flg;
5301:     return(0);
5302:     break;
5303:   case MAT_SPD:
5304:     mat->spd_set = PETSC_TRUE;
5305:     mat->spd     = flg;
5306:     if (flg) {
5307:       mat->symmetric                  = PETSC_TRUE;
5308:       mat->structurally_symmetric     = PETSC_TRUE;
5309:       mat->symmetric_set              = PETSC_TRUE;
5310:       mat->structurally_symmetric_set = PETSC_TRUE;
5311:     }
5312:     break;
5313:   case MAT_SYMMETRIC:
5314:     mat->symmetric = flg;
5315:     if (flg) mat->structurally_symmetric = PETSC_TRUE;
5316:     mat->symmetric_set              = PETSC_TRUE;
5317:     mat->structurally_symmetric_set = flg;
5318:     break;
5319:   case MAT_HERMITIAN:
5320:     mat->hermitian = flg;
5321:     if (flg) mat->structurally_symmetric = PETSC_TRUE;
5322:     mat->hermitian_set              = PETSC_TRUE;
5323:     mat->structurally_symmetric_set = flg;
5324:     break;
5325:   case MAT_STRUCTURALLY_SYMMETRIC:
5326:     mat->structurally_symmetric     = flg;
5327:     mat->structurally_symmetric_set = PETSC_TRUE;
5328:     break;
5329:   case MAT_SYMMETRY_ETERNAL:
5330:     mat->symmetric_eternal = flg;
5331:     break;
5332:   default:
5333:     break;
5334:   }
5335:   if (mat->ops->setoption) {
5336:     (*mat->ops->setoption)(mat,op,flg);
5337:   }
5338:   return(0);
5339: }

5343: /*@
5344:    MatGetOption - Gets a parameter option that has been set for a matrix.

5346:    Logically Collective on Mat for certain operations, such as MAT_SPD, not collective for MAT_ROW_ORIENTED, see MatOption

5348:    Input Parameters:
5349: +  mat - the matrix
5350: -  option - the option, this only responds to certain options, check the code for which ones

5352:    Output Parameter:
5353: .  flg - turn the option on (PETSC_TRUE) or off (PETSC_FALSE)

5355:     Notes: Can only be called after MatSetSizes() and MatSetType() have been set.

5357:    Level: intermediate

5359:    Concepts: matrices^setting options

5361: .seealso:  MatOption, MatSetOption()

5363: @*/
5364: PetscErrorCode MatGetOption(Mat mat,MatOption op,PetscBool *flg)
5365: {

5370:   if (((int) op) <= MAT_OPTION_MIN || ((int) op) >= MAT_OPTION_MAX) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"Options %d is out of range",(int)op);
5371:   if (!((PetscObject)mat)->type_name) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_TYPENOTSET,"Cannot get options until type and size have been set, see MatSetType() and MatSetSizes()");

5373:   switch (op) {
5374:   case MAT_NO_OFF_PROC_ENTRIES:
5375:     *flg = mat->nooffprocentries;
5376:     break;
5377:   case MAT_NO_OFF_PROC_ZERO_ROWS:
5378:     *flg = mat->nooffproczerorows;
5379:     break;
5380:   case MAT_SYMMETRIC:
5381:     *flg = mat->symmetric;
5382:     break;
5383:   case MAT_HERMITIAN:
5384:     *flg = mat->hermitian;
5385:     break;
5386:   case MAT_STRUCTURALLY_SYMMETRIC:
5387:     *flg = mat->structurally_symmetric;
5388:     break;
5389:   case MAT_SYMMETRY_ETERNAL:
5390:     *flg = mat->symmetric_eternal;
5391:     break;
5392:   default:
5393:     break;
5394:   }
5395:   return(0);
5396: }

5400: /*@
5401:    MatZeroEntries - Zeros all entries of a matrix.  For sparse matrices
5402:    this routine retains the old nonzero structure.

5404:    Logically Collective on Mat

5406:    Input Parameters:
5407: .  mat - the matrix

5409:    Level: intermediate

5411:    Notes: If the matrix was not preallocated then a default, likely poor preallocation will be set in the matrix, so this should be called after the preallocation phase.
5412:    See the Performance chapter of the users manual for information on preallocating matrices.

5414:    Concepts: matrices^zeroing

5416: .seealso: MatZeroRows()
5417: @*/
5418: PetscErrorCode MatZeroEntries(Mat mat)
5419: {

5425:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
5426:   if (mat->insertmode != NOT_SET_VALUES) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for matrices where you have set values but not yet assembled");
5427:   if (!mat->ops->zeroentries) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
5428:   MatCheckPreallocated(mat,1);

5430:   PetscLogEventBegin(MAT_ZeroEntries,mat,0,0,0);
5431:   (*mat->ops->zeroentries)(mat);
5432:   PetscLogEventEnd(MAT_ZeroEntries,mat,0,0,0);
5433:   PetscObjectStateIncrease((PetscObject)mat);
5434: #if defined(PETSC_HAVE_CUSP)
5435:   if (mat->valid_GPU_matrix != PETSC_CUSP_UNALLOCATED) {
5436:     mat->valid_GPU_matrix = PETSC_CUSP_CPU;
5437:   }
5438: #endif
5439: #if defined(PETSC_HAVE_VIENNACL)
5440:   if (mat->valid_GPU_matrix != PETSC_VIENNACL_UNALLOCATED) {
5441:     mat->valid_GPU_matrix = PETSC_VIENNACL_CPU;
5442:   }
5443: #endif
5444:   return(0);
5445: }

5449: /*@C
5450:    MatZeroRowsColumns - Zeros all entries (except possibly the main diagonal)
5451:    of a set of rows and columns of a matrix.

5453:    Collective on Mat

5455:    Input Parameters:
5456: +  mat - the matrix
5457: .  numRows - the number of rows to remove
5458: .  rows - the global row indices
5459: .  diag - value put in all diagonals of eliminated rows (0.0 will even eliminate diagonal entry)
5460: .  x - optional vector of solutions for zeroed rows (other entries in vector are not used)
5461: -  b - optional vector of right hand side, that will be adjusted by provided solution

5463:    Notes:
5464:    This does not change the nonzero structure of the matrix, it merely zeros those entries in the matrix.

5466:    The user can set a value in the diagonal entry (or for the AIJ and
5467:    row formats can optionally remove the main diagonal entry from the
5468:    nonzero structure as well, by passing 0.0 as the final argument).

5470:    For the parallel case, all processes that share the matrix (i.e.,
5471:    those in the communicator used for matrix creation) MUST call this
5472:    routine, regardless of whether any rows being zeroed are owned by
5473:    them.

5475:    Each processor can indicate any rows in the entire matrix to be zeroed (i.e. each process does NOT have to
5476:    list only rows local to itself).

5478:    The option MAT_NO_OFF_PROC_ZERO_ROWS does not apply to this routine.

5480:    Level: intermediate

5482:    Concepts: matrices^zeroing rows

5484: .seealso: MatZeroRowsIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(), MatZeroRowsColumnsIS()
5485: @*/
5486: PetscErrorCode MatZeroRowsColumns(Mat mat,PetscInt numRows,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
5487: {

5494:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
5495:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
5496:   if (!mat->ops->zerorowscolumns) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
5497:   MatCheckPreallocated(mat,1);

5499:   (*mat->ops->zerorowscolumns)(mat,numRows,rows,diag,x,b);
5500:   MatViewFromOptions(mat,NULL,"-mat_view");
5501:   PetscObjectStateIncrease((PetscObject)mat);
5502: #if defined(PETSC_HAVE_CUSP)
5503:   if (mat->valid_GPU_matrix != PETSC_CUSP_UNALLOCATED) {
5504:     mat->valid_GPU_matrix = PETSC_CUSP_CPU;
5505:   }
5506: #endif
5507: #if defined(PETSC_HAVE_VIENNACL)
5508:   if (mat->valid_GPU_matrix != PETSC_VIENNACL_UNALLOCATED) {
5509:     mat->valid_GPU_matrix = PETSC_VIENNACL_CPU;
5510:   }
5511: #endif
5512:   return(0);
5513: }

5517: /*@C
5518:    MatZeroRowsColumnsIS - Zeros all entries (except possibly the main diagonal)
5519:    of a set of rows and columns of a matrix.

5521:    Collective on Mat

5523:    Input Parameters:
5524: +  mat - the matrix
5525: .  is - the rows to zero
5526: .  diag - value put in all diagonals of eliminated rows (0.0 will even eliminate diagonal entry)
5527: .  x - optional vector of solutions for zeroed rows (other entries in vector are not used)
5528: -  b - optional vector of right hand side, that will be adjusted by provided solution

5530:    Notes:
5531:    This does not change the nonzero structure of the matrix, it merely zeros those entries in the matrix.

5533:    The user can set a value in the diagonal entry (or for the AIJ and
5534:    row formats can optionally remove the main diagonal entry from the
5535:    nonzero structure as well, by passing 0.0 as the final argument).

5537:    For the parallel case, all processes that share the matrix (i.e.,
5538:    those in the communicator used for matrix creation) MUST call this
5539:    routine, regardless of whether any rows being zeroed are owned by
5540:    them.

5542:    Each processor can indicate any rows in the entire matrix to be zeroed (i.e. each process does NOT have to
5543:    list only rows local to itself).

5545:    The option MAT_NO_OFF_PROC_ZERO_ROWS does not apply to this routine.

5547:    Level: intermediate

5549:    Concepts: matrices^zeroing rows

5551: .seealso: MatZeroRowsIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(), MatZeroRowsColumns()
5552: @*/
5553: PetscErrorCode MatZeroRowsColumnsIS(Mat mat,IS is,PetscScalar diag,Vec x,Vec b)
5554: {
5556:   PetscInt       numRows;
5557:   const PetscInt *rows;

5564:   ISGetLocalSize(is,&numRows);
5565:   ISGetIndices(is,&rows);
5566:   MatZeroRowsColumns(mat,numRows,rows,diag,x,b);
5567:   ISRestoreIndices(is,&rows);
5568:   return(0);
5569: }

5573: /*@C
5574:    MatZeroRows - Zeros all entries (except possibly the main diagonal)
5575:    of a set of rows of a matrix.

5577:    Collective on Mat

5579:    Input Parameters:
5580: +  mat - the matrix
5581: .  numRows - the number of rows to remove
5582: .  rows - the global row indices
5583: .  diag - value put in all diagonals of eliminated rows (0.0 will even eliminate diagonal entry)
5584: .  x - optional vector of solutions for zeroed rows (other entries in vector are not used)
5585: -  b - optional vector of right hand side, that will be adjusted by provided solution

5587:    Notes:
5588:    For the AIJ and BAIJ matrix formats this removes the old nonzero structure,
5589:    but does not release memory.  For the dense and block diagonal
5590:    formats this does not alter the nonzero structure.

5592:    If the option MatSetOption(mat,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE) the nonzero structure
5593:    of the matrix is not changed (even for AIJ and BAIJ matrices) the values are
5594:    merely zeroed.

5596:    The user can set a value in the diagonal entry (or for the AIJ and
5597:    row formats can optionally remove the main diagonal entry from the
5598:    nonzero structure as well, by passing 0.0 as the final argument).

5600:    For the parallel case, all processes that share the matrix (i.e.,
5601:    those in the communicator used for matrix creation) MUST call this
5602:    routine, regardless of whether any rows being zeroed are owned by
5603:    them.

5605:    Each processor can indicate any rows in the entire matrix to be zeroed (i.e. each process does NOT have to
5606:    list only rows local to itself).

5608:    You can call MatSetOption(mat,MAT_NO_OFF_PROC_ZERO_ROWS,PETSC_TRUE) if each process indicates only rows it
5609:    owns that are to be zeroed. This saves a global synchronization in the implementation.

5611:    Level: intermediate

5613:    Concepts: matrices^zeroing rows

5615: .seealso: MatZeroRowsIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption()
5616: @*/
5617: PetscErrorCode MatZeroRows(Mat mat,PetscInt numRows,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
5618: {

5625:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
5626:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
5627:   if (!mat->ops->zerorows) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
5628:   MatCheckPreallocated(mat,1);

5630:   (*mat->ops->zerorows)(mat,numRows,rows,diag,x,b);
5631:   MatViewFromOptions(mat,NULL,"-mat_view");
5632:   PetscObjectStateIncrease((PetscObject)mat);
5633: #if defined(PETSC_HAVE_CUSP)
5634:   if (mat->valid_GPU_matrix != PETSC_CUSP_UNALLOCATED) {
5635:     mat->valid_GPU_matrix = PETSC_CUSP_CPU;
5636:   }
5637: #endif
5638: #if defined(PETSC_HAVE_VIENNACL)
5639:   if (mat->valid_GPU_matrix != PETSC_VIENNACL_UNALLOCATED) {
5640:     mat->valid_GPU_matrix = PETSC_VIENNACL_CPU;
5641:   }
5642: #endif
5643:   return(0);
5644: }

5648: /*@C
5649:    MatZeroRowsIS - Zeros all entries (except possibly the main diagonal)
5650:    of a set of rows of a matrix.

5652:    Collective on Mat

5654:    Input Parameters:
5655: +  mat - the matrix
5656: .  is - index set of rows to remove
5657: .  diag - value put in all diagonals of eliminated rows
5658: .  x - optional vector of solutions for zeroed rows (other entries in vector are not used)
5659: -  b - optional vector of right hand side, that will be adjusted by provided solution

5661:    Notes:
5662:    For the AIJ and BAIJ matrix formats this removes the old nonzero structure,
5663:    but does not release memory.  For the dense and block diagonal
5664:    formats this does not alter the nonzero structure.

5666:    If the option MatSetOption(mat,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE) the nonzero structure
5667:    of the matrix is not changed (even for AIJ and BAIJ matrices) the values are
5668:    merely zeroed.

5670:    The user can set a value in the diagonal entry (or for the AIJ and
5671:    row formats can optionally remove the main diagonal entry from the
5672:    nonzero structure as well, by passing 0.0 as the final argument).

5674:    For the parallel case, all processes that share the matrix (i.e.,
5675:    those in the communicator used for matrix creation) MUST call this
5676:    routine, regardless of whether any rows being zeroed are owned by
5677:    them.

5679:    Each processor can indicate any rows in the entire matrix to be zeroed (i.e. each process does NOT have to
5680:    list only rows local to itself).

5682:    You can call MatSetOption(mat,MAT_NO_OFF_PROC_ZERO_ROWS,PETSC_TRUE) if each process indicates only rows it
5683:    owns that are to be zeroed. This saves a global synchronization in the implementation.

5685:    Level: intermediate

5687:    Concepts: matrices^zeroing rows

5689: .seealso: MatZeroRows(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption()
5690: @*/
5691: PetscErrorCode MatZeroRowsIS(Mat mat,IS is,PetscScalar diag,Vec x,Vec b)
5692: {
5693:   PetscInt       numRows;
5694:   const PetscInt *rows;

5701:   ISGetLocalSize(is,&numRows);
5702:   ISGetIndices(is,&rows);
5703:   MatZeroRows(mat,numRows,rows,diag,x,b);
5704:   ISRestoreIndices(is,&rows);
5705:   return(0);
5706: }

5710: /*@C
5711:    MatZeroRowsStencil - Zeros all entries (except possibly the main diagonal)
5712:    of a set of rows of a matrix. These rows must be local to the process.

5714:    Collective on Mat

5716:    Input Parameters:
5717: +  mat - the matrix
5718: .  numRows - the number of rows to remove
5719: .  rows - the grid coordinates (and component number when dof > 1) for matrix rows
5720: .  diag - value put in all diagonals of eliminated rows (0.0 will even eliminate diagonal entry)
5721: .  x - optional vector of solutions for zeroed rows (other entries in vector are not used)
5722: -  b - optional vector of right hand side, that will be adjusted by provided solution

5724:    Notes:
5725:    For the AIJ and BAIJ matrix formats this removes the old nonzero structure,
5726:    but does not release memory.  For the dense and block diagonal
5727:    formats this does not alter the nonzero structure.

5729:    If the option MatSetOption(mat,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE) the nonzero structure
5730:    of the matrix is not changed (even for AIJ and BAIJ matrices) the values are
5731:    merely zeroed.

5733:    The user can set a value in the diagonal entry (or for the AIJ and
5734:    row formats can optionally remove the main diagonal entry from the
5735:    nonzero structure as well, by passing 0.0 as the final argument).

5737:    For the parallel case, all processes that share the matrix (i.e.,
5738:    those in the communicator used for matrix creation) MUST call this
5739:    routine, regardless of whether any rows being zeroed are owned by
5740:    them.

5742:    Each processor can indicate any rows in the entire matrix to be zeroed (i.e. each process does NOT have to
5743:    list only rows local to itself).

5745:    The grid coordinates are across the entire grid, not just the local portion

5747:    In Fortran idxm and idxn should be declared as
5748: $     MatStencil idxm(4,m)
5749:    and the values inserted using
5750: $    idxm(MatStencil_i,1) = i
5751: $    idxm(MatStencil_j,1) = j
5752: $    idxm(MatStencil_k,1) = k
5753: $    idxm(MatStencil_c,1) = c
5754:    etc

5756:    For periodic boundary conditions use negative indices for values to the left (below 0; that are to be
5757:    obtained by wrapping values from right edge). For values to the right of the last entry using that index plus one
5758:    etc to obtain values that obtained by wrapping the values from the left edge. This does not work for anything but the
5759:    DM_BOUNDARY_PERIODIC boundary type.

5761:    For indices that don't mean anything for your case (like the k index when working in 2d) or the c index when you have
5762:    a single value per point) you can skip filling those indices.

5764:    Level: intermediate

5766:    Concepts: matrices^zeroing rows

5768: .seealso: MatZeroRows(), MatZeroRowsIS(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption()
5769: @*/
5770: PetscErrorCode MatZeroRowsStencil(Mat mat,PetscInt numRows,const MatStencil rows[],PetscScalar diag,Vec x,Vec b)
5771: {
5772:   PetscInt       dim     = mat->stencil.dim;
5773:   PetscInt       sdim    = dim - (1 - (PetscInt) mat->stencil.noc);
5774:   PetscInt       *dims   = mat->stencil.dims+1;
5775:   PetscInt       *starts = mat->stencil.starts;
5776:   PetscInt       *dxm    = (PetscInt*) rows;
5777:   PetscInt       *jdxm, i, j, tmp, numNewRows = 0;


5785:   PetscMalloc1(numRows, &jdxm);
5786:   for (i = 0; i < numRows; ++i) {
5787:     /* Skip unused dimensions (they are ordered k, j, i, c) */
5788:     for (j = 0; j < 3-sdim; ++j) dxm++;
5789:     /* Local index in X dir */
5790:     tmp = *dxm++ - starts[0];
5791:     /* Loop over remaining dimensions */
5792:     for (j = 0; j < dim-1; ++j) {
5793:       /* If nonlocal, set index to be negative */
5794:       if ((*dxm++ - starts[j+1]) < 0 || tmp < 0) tmp = PETSC_MIN_INT;
5795:       /* Update local index */
5796:       else tmp = tmp*dims[j] + *(dxm-1) - starts[j+1];
5797:     }
5798:     /* Skip component slot if necessary */
5799:     if (mat->stencil.noc) dxm++;
5800:     /* Local row number */
5801:     if (tmp >= 0) {
5802:       jdxm[numNewRows++] = tmp;
5803:     }
5804:   }
5805:   MatZeroRowsLocal(mat,numNewRows,jdxm,diag,x,b);
5806:   PetscFree(jdxm);
5807:   return(0);
5808: }

5812: /*@C
5813:    MatZeroRowsColumnsStencil - Zeros all row and column entries (except possibly the main diagonal)
5814:    of a set of rows and columns of a matrix.

5816:    Collective on Mat

5818:    Input Parameters:
5819: +  mat - the matrix
5820: .  numRows - the number of rows/columns to remove
5821: .  rows - the grid coordinates (and component number when dof > 1) for matrix rows
5822: .  diag - value put in all diagonals of eliminated rows (0.0 will even eliminate diagonal entry)
5823: .  x - optional vector of solutions for zeroed rows (other entries in vector are not used)
5824: -  b - optional vector of right hand side, that will be adjusted by provided solution

5826:    Notes:
5827:    For the AIJ and BAIJ matrix formats this removes the old nonzero structure,
5828:    but does not release memory.  For the dense and block diagonal
5829:    formats this does not alter the nonzero structure.

5831:    If the option MatSetOption(mat,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE) the nonzero structure
5832:    of the matrix is not changed (even for AIJ and BAIJ matrices) the values are
5833:    merely zeroed.

5835:    The user can set a value in the diagonal entry (or for the AIJ and
5836:    row formats can optionally remove the main diagonal entry from the
5837:    nonzero structure as well, by passing 0.0 as the final argument).

5839:    For the parallel case, all processes that share the matrix (i.e.,
5840:    those in the communicator used for matrix creation) MUST call this
5841:    routine, regardless of whether any rows being zeroed are owned by
5842:    them.

5844:    Each processor can indicate any rows in the entire matrix to be zeroed (i.e. each process does NOT have to
5845:    list only rows local to itself, but the row/column numbers are given in local numbering).

5847:    The grid coordinates are across the entire grid, not just the local portion

5849:    In Fortran idxm and idxn should be declared as
5850: $     MatStencil idxm(4,m)
5851:    and the values inserted using
5852: $    idxm(MatStencil_i,1) = i
5853: $    idxm(MatStencil_j,1) = j
5854: $    idxm(MatStencil_k,1) = k
5855: $    idxm(MatStencil_c,1) = c
5856:    etc

5858:    For periodic boundary conditions use negative indices for values to the left (below 0; that are to be
5859:    obtained by wrapping values from right edge). For values to the right of the last entry using that index plus one
5860:    etc to obtain values that obtained by wrapping the values from the left edge. This does not work for anything but the
5861:    DM_BOUNDARY_PERIODIC boundary type.

5863:    For indices that don't mean anything for your case (like the k index when working in 2d) or the c index when you have
5864:    a single value per point) you can skip filling those indices.

5866:    Level: intermediate

5868:    Concepts: matrices^zeroing rows

5870: .seealso: MatZeroRows(), MatZeroRowsIS(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption()
5871: @*/
5872: PetscErrorCode MatZeroRowsColumnsStencil(Mat mat,PetscInt numRows,const MatStencil rows[],PetscScalar diag,Vec x,Vec b)
5873: {
5874:   PetscInt       dim     = mat->stencil.dim;
5875:   PetscInt       sdim    = dim - (1 - (PetscInt) mat->stencil.noc);
5876:   PetscInt       *dims   = mat->stencil.dims+1;
5877:   PetscInt       *starts = mat->stencil.starts;
5878:   PetscInt       *dxm    = (PetscInt*) rows;
5879:   PetscInt       *jdxm, i, j, tmp, numNewRows = 0;


5887:   PetscMalloc1(numRows, &jdxm);
5888:   for (i = 0; i < numRows; ++i) {
5889:     /* Skip unused dimensions (they are ordered k, j, i, c) */
5890:     for (j = 0; j < 3-sdim; ++j) dxm++;
5891:     /* Local index in X dir */
5892:     tmp = *dxm++ - starts[0];
5893:     /* Loop over remaining dimensions */
5894:     for (j = 0; j < dim-1; ++j) {
5895:       /* If nonlocal, set index to be negative */
5896:       if ((*dxm++ - starts[j+1]) < 0 || tmp < 0) tmp = PETSC_MIN_INT;
5897:       /* Update local index */
5898:       else tmp = tmp*dims[j] + *(dxm-1) - starts[j+1];
5899:     }
5900:     /* Skip component slot if necessary */
5901:     if (mat->stencil.noc) dxm++;
5902:     /* Local row number */
5903:     if (tmp >= 0) {
5904:       jdxm[numNewRows++] = tmp;
5905:     }
5906:   }
5907:   MatZeroRowsColumnsLocal(mat,numNewRows,jdxm,diag,x,b);
5908:   PetscFree(jdxm);
5909:   return(0);
5910: }

5914: /*@C
5915:    MatZeroRowsLocal - Zeros all entries (except possibly the main diagonal)
5916:    of a set of rows of a matrix; using local numbering of rows.

5918:    Collective on Mat

5920:    Input Parameters:
5921: +  mat - the matrix
5922: .  numRows - the number of rows to remove
5923: .  rows - the global row indices
5924: .  diag - value put in all diagonals of eliminated rows
5925: .  x - optional vector of solutions for zeroed rows (other entries in vector are not used)
5926: -  b - optional vector of right hand side, that will be adjusted by provided solution

5928:    Notes:
5929:    Before calling MatZeroRowsLocal(), the user must first set the
5930:    local-to-global mapping by calling MatSetLocalToGlobalMapping().

5932:    For the AIJ matrix formats this removes the old nonzero structure,
5933:    but does not release memory.  For the dense and block diagonal
5934:    formats this does not alter the nonzero structure.

5936:    If the option MatSetOption(mat,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE) the nonzero structure
5937:    of the matrix is not changed (even for AIJ and BAIJ matrices) the values are
5938:    merely zeroed.

5940:    The user can set a value in the diagonal entry (or for the AIJ and
5941:    row formats can optionally remove the main diagonal entry from the
5942:    nonzero structure as well, by passing 0.0 as the final argument).

5944:    You can call MatSetOption(mat,MAT_NO_OFF_PROC_ZERO_ROWS,PETSC_TRUE) if each process indicates only rows it
5945:    owns that are to be zeroed. This saves a global synchronization in the implementation.

5947:    Level: intermediate

5949:    Concepts: matrices^zeroing

5951: .seealso: MatZeroRows(), MatZeroRowsLocalIS(), MatZeroEntries(), MatZeroRows(), MatSetLocalToGlobalMapping
5952: @*/
5953: PetscErrorCode MatZeroRowsLocal(Mat mat,PetscInt numRows,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
5954: {

5961:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
5962:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
5963:   MatCheckPreallocated(mat,1);

5965:   if (mat->ops->zerorowslocal) {
5966:     (*mat->ops->zerorowslocal)(mat,numRows,rows,diag,x,b);
5967:   } else {
5968:     IS             is, newis;
5969:     const PetscInt *newRows;

5971:     if (!mat->rmap->mapping) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Need to provide local to global mapping to matrix first");
5972:     ISCreateGeneral(PETSC_COMM_SELF,numRows,rows,PETSC_COPY_VALUES,&is);
5973:     ISLocalToGlobalMappingApplyIS(mat->rmap->mapping,is,&newis);
5974:     ISGetIndices(newis,&newRows);
5975:     (*mat->ops->zerorows)(mat,numRows,newRows,diag,x,b);
5976:     ISRestoreIndices(newis,&newRows);
5977:     ISDestroy(&newis);
5978:     ISDestroy(&is);
5979:   }
5980:   PetscObjectStateIncrease((PetscObject)mat);
5981: #if defined(PETSC_HAVE_CUSP)
5982:   if (mat->valid_GPU_matrix != PETSC_CUSP_UNALLOCATED) {
5983:     mat->valid_GPU_matrix = PETSC_CUSP_CPU;
5984:   }
5985: #endif
5986: #if defined(PETSC_HAVE_VIENNACL)
5987:   if (mat->valid_GPU_matrix != PETSC_VIENNACL_UNALLOCATED) {
5988:     mat->valid_GPU_matrix = PETSC_VIENNACL_CPU;
5989:   }
5990: #endif
5991:   return(0);
5992: }

5996: /*@C
5997:    MatZeroRowsLocalIS - Zeros all entries (except possibly the main diagonal)
5998:    of a set of rows of a matrix; using local numbering of rows.

6000:    Collective on Mat

6002:    Input Parameters:
6003: +  mat - the matrix
6004: .  is - index set of rows to remove
6005: .  diag - value put in all diagonals of eliminated rows
6006: .  x - optional vector of solutions for zeroed rows (other entries in vector are not used)
6007: -  b - optional vector of right hand side, that will be adjusted by provided solution

6009:    Notes:
6010:    Before calling MatZeroRowsLocalIS(), the user must first set the
6011:    local-to-global mapping by calling MatSetLocalToGlobalMapping().

6013:    For the AIJ matrix formats this removes the old nonzero structure,
6014:    but does not release memory.  For the dense and block diagonal
6015:    formats this does not alter the nonzero structure.

6017:    If the option MatSetOption(mat,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE) the nonzero structure
6018:    of the matrix is not changed (even for AIJ and BAIJ matrices) the values are
6019:    merely zeroed.

6021:    The user can set a value in the diagonal entry (or for the AIJ and
6022:    row formats can optionally remove the main diagonal entry from the
6023:    nonzero structure as well, by passing 0.0 as the final argument).

6025:    You can call MatSetOption(mat,MAT_NO_OFF_PROC_ZERO_ROWS,PETSC_TRUE) if each process indicates only rows it
6026:    owns that are to be zeroed. This saves a global synchronization in the implementation.

6028:    Level: intermediate

6030:    Concepts: matrices^zeroing

6032: .seealso: MatZeroRows(), MatZeroRowsLocal(), MatZeroEntries(), MatZeroRows(), MatSetLocalToGlobalMapping
6033: @*/
6034: PetscErrorCode MatZeroRowsLocalIS(Mat mat,IS is,PetscScalar diag,Vec x,Vec b)
6035: {
6037:   PetscInt       numRows;
6038:   const PetscInt *rows;

6044:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
6045:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
6046:   MatCheckPreallocated(mat,1);

6048:   ISGetLocalSize(is,&numRows);
6049:   ISGetIndices(is,&rows);
6050:   MatZeroRowsLocal(mat,numRows,rows,diag,x,b);
6051:   ISRestoreIndices(is,&rows);
6052:   return(0);
6053: }

6057: /*@C
6058:    MatZeroRowsColumnsLocal - Zeros all entries (except possibly the main diagonal)
6059:    of a set of rows and columns of a matrix; using local numbering of rows.

6061:    Collective on Mat

6063:    Input Parameters:
6064: +  mat - the matrix
6065: .  numRows - the number of rows to remove
6066: .  rows - the global row indices
6067: .  diag - value put in all diagonals of eliminated rows
6068: .  x - optional vector of solutions for zeroed rows (other entries in vector are not used)
6069: -  b - optional vector of right hand side, that will be adjusted by provided solution

6071:    Notes:
6072:    Before calling MatZeroRowsColumnsLocal(), the user must first set the
6073:    local-to-global mapping by calling MatSetLocalToGlobalMapping().

6075:    The user can set a value in the diagonal entry (or for the AIJ and
6076:    row formats can optionally remove the main diagonal entry from the
6077:    nonzero structure as well, by passing 0.0 as the final argument).

6079:    Level: intermediate

6081:    Concepts: matrices^zeroing

6083: .seealso: MatZeroRows(), MatZeroRowsLocalIS(), MatZeroEntries(), MatZeroRows(), MatSetLocalToGlobalMapping
6084: @*/
6085: PetscErrorCode MatZeroRowsColumnsLocal(Mat mat,PetscInt numRows,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
6086: {
6088:   IS             is, newis;
6089:   const PetscInt *newRows;

6095:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
6096:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
6097:   MatCheckPreallocated(mat,1);

6099:   if (!mat->cmap->mapping) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Need to provide local to global mapping to matrix first");
6100:   ISCreateGeneral(PETSC_COMM_SELF,numRows,rows,PETSC_COPY_VALUES,&is);
6101:   ISLocalToGlobalMappingApplyIS(mat->cmap->mapping,is,&newis);
6102:   ISGetIndices(newis,&newRows);
6103:   (*mat->ops->zerorowscolumns)(mat,numRows,newRows,diag,x,b);
6104:   ISRestoreIndices(newis,&newRows);
6105:   ISDestroy(&newis);
6106:   ISDestroy(&is);
6107:   PetscObjectStateIncrease((PetscObject)mat);
6108: #if defined(PETSC_HAVE_CUSP)
6109:   if (mat->valid_GPU_matrix != PETSC_CUSP_UNALLOCATED) {
6110:     mat->valid_GPU_matrix = PETSC_CUSP_CPU;
6111:   }
6112: #endif
6113: #if defined(PETSC_HAVE_VIENNACL)
6114:   if (mat->valid_GPU_matrix != PETSC_VIENNACL_UNALLOCATED) {
6115:     mat->valid_GPU_matrix = PETSC_VIENNACL_CPU;
6116:   }
6117: #endif
6118:   return(0);
6119: }

6123: /*@C
6124:    MatZeroRowsColumnsLocalIS - Zeros all entries (except possibly the main diagonal)
6125:    of a set of rows and columns of a matrix; using local numbering of rows.

6127:    Collective on Mat

6129:    Input Parameters:
6130: +  mat - the matrix
6131: .  is - index set of rows to remove
6132: .  diag - value put in all diagonals of eliminated rows
6133: .  x - optional vector of solutions for zeroed rows (other entries in vector are not used)
6134: -  b - optional vector of right hand side, that will be adjusted by provided solution

6136:    Notes:
6137:    Before calling MatZeroRowsColumnsLocalIS(), the user must first set the
6138:    local-to-global mapping by calling MatSetLocalToGlobalMapping().

6140:    The user can set a value in the diagonal entry (or for the AIJ and
6141:    row formats can optionally remove the main diagonal entry from the
6142:    nonzero structure as well, by passing 0.0 as the final argument).

6144:    Level: intermediate

6146:    Concepts: matrices^zeroing

6148: .seealso: MatZeroRows(), MatZeroRowsLocal(), MatZeroEntries(), MatZeroRows(), MatSetLocalToGlobalMapping
6149: @*/
6150: PetscErrorCode MatZeroRowsColumnsLocalIS(Mat mat,IS is,PetscScalar diag,Vec x,Vec b)
6151: {
6153:   PetscInt       numRows;
6154:   const PetscInt *rows;

6160:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
6161:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
6162:   MatCheckPreallocated(mat,1);

6164:   ISGetLocalSize(is,&numRows);
6165:   ISGetIndices(is,&rows);
6166:   MatZeroRowsColumnsLocal(mat,numRows,rows,diag,x,b);
6167:   ISRestoreIndices(is,&rows);
6168:   return(0);
6169: }

6173: /*@
6174:    MatGetSize - Returns the numbers of rows and columns in a matrix.

6176:    Not Collective

6178:    Input Parameter:
6179: .  mat - the matrix

6181:    Output Parameters:
6182: +  m - the number of global rows
6183: -  n - the number of global columns

6185:    Note: both output parameters can be NULL on input.

6187:    Level: beginner

6189:    Concepts: matrices^size

6191: .seealso: MatGetLocalSize()
6192: @*/
6193: PetscErrorCode MatGetSize(Mat mat,PetscInt *m,PetscInt *n)
6194: {
6197:   if (m) *m = mat->rmap->N;
6198:   if (n) *n = mat->cmap->N;
6199:   return(0);
6200: }

6204: /*@
6205:    MatGetLocalSize - Returns the number of rows and columns in a matrix
6206:    stored locally.  This information may be implementation dependent, so
6207:    use with care.

6209:    Not Collective

6211:    Input Parameters:
6212: .  mat - the matrix

6214:    Output Parameters:
6215: +  m - the number of local rows
6216: -  n - the number of local columns

6218:    Note: both output parameters can be NULL on input.

6220:    Level: beginner

6222:    Concepts: matrices^local size

6224: .seealso: MatGetSize()
6225: @*/
6226: PetscErrorCode MatGetLocalSize(Mat mat,PetscInt *m,PetscInt *n)
6227: {
6232:   if (m) *m = mat->rmap->n;
6233:   if (n) *n = mat->cmap->n;
6234:   return(0);
6235: }

6239: /*@
6240:    MatGetOwnershipRangeColumn - Returns the range of matrix columns associated with rows of a vector one multiplies by that owned by
6241:    this processor. (The columns of the "diagonal block")

6243:    Not Collective, unless matrix has not been allocated, then collective on Mat

6245:    Input Parameters:
6246: .  mat - the matrix

6248:    Output Parameters:
6249: +  m - the global index of the first local column
6250: -  n - one more than the global index of the last local column

6252:    Notes: both output parameters can be NULL on input.

6254:    Level: developer

6256:    Concepts: matrices^column ownership

6258: .seealso:  MatGetOwnershipRange(), MatGetOwnershipRanges(), MatGetOwnershipRangesColumn()

6260: @*/
6261: PetscErrorCode MatGetOwnershipRangeColumn(Mat mat,PetscInt *m,PetscInt *n)
6262: {
6268:   MatCheckPreallocated(mat,1);
6269:   if (m) *m = mat->cmap->rstart;
6270:   if (n) *n = mat->cmap->rend;
6271:   return(0);
6272: }

6276: /*@
6277:    MatGetOwnershipRange - Returns the range of matrix rows owned by
6278:    this processor, assuming that the matrix is laid out with the first
6279:    n1 rows on the first processor, the next n2 rows on the second, etc.
6280:    For certain parallel layouts this range may not be well defined.

6282:    Not Collective

6284:    Input Parameters:
6285: .  mat - the matrix

6287:    Output Parameters:
6288: +  m - the global index of the first local row
6289: -  n - one more than the global index of the last local row

6291:    Note: Both output parameters can be NULL on input.
6292: $  This function requires that the matrix be preallocated. If you have not preallocated, consider using
6293: $    PetscSplitOwnership(MPI_Comm comm, PetscInt *n, PetscInt *N)
6294: $  and then MPI_Scan() to calculate prefix sums of the local sizes.

6296:    Level: beginner

6298:    Concepts: matrices^row ownership

6300: .seealso:   MatGetOwnershipRanges(), MatGetOwnershipRangeColumn(), MatGetOwnershipRangesColumn(), PetscSplitOwnership(), PetscSplitOwnershipBlock()

6302: @*/
6303: PetscErrorCode MatGetOwnershipRange(Mat mat,PetscInt *m,PetscInt *n)
6304: {
6310:   MatCheckPreallocated(mat,1);
6311:   if (m) *m = mat->rmap->rstart;
6312:   if (n) *n = mat->rmap->rend;
6313:   return(0);
6314: }

6318: /*@C
6319:    MatGetOwnershipRanges - Returns the range of matrix rows owned by
6320:    each process

6322:    Not Collective, unless matrix has not been allocated, then collective on Mat

6324:    Input Parameters:
6325: .  mat - the matrix

6327:    Output Parameters:
6328: .  ranges - start of each processors portion plus one more than the total length at the end

6330:    Level: beginner

6332:    Concepts: matrices^row ownership

6334: .seealso:   MatGetOwnershipRange(), MatGetOwnershipRangeColumn(), MatGetOwnershipRangesColumn()

6336: @*/
6337: PetscErrorCode MatGetOwnershipRanges(Mat mat,const PetscInt **ranges)
6338: {

6344:   MatCheckPreallocated(mat,1);
6345:   PetscLayoutGetRanges(mat->rmap,ranges);
6346:   return(0);
6347: }

6351: /*@C
6352:    MatGetOwnershipRangesColumn - Returns the range of matrix columns associated with rows of a vector one multiplies by that owned by
6353:    this processor. (The columns of the "diagonal blocks" for each process)

6355:    Not Collective, unless matrix has not been allocated, then collective on Mat

6357:    Input Parameters:
6358: .  mat - the matrix

6360:    Output Parameters:
6361: .  ranges - start of each processors portion plus one more then the total length at the end

6363:    Level: beginner

6365:    Concepts: matrices^column ownership

6367: .seealso:   MatGetOwnershipRange(), MatGetOwnershipRangeColumn(), MatGetOwnershipRanges()

6369: @*/
6370: PetscErrorCode MatGetOwnershipRangesColumn(Mat mat,const PetscInt **ranges)
6371: {

6377:   MatCheckPreallocated(mat,1);
6378:   PetscLayoutGetRanges(mat->cmap,ranges);
6379:   return(0);
6380: }

6384: /*@C
6385:    MatGetOwnershipIS - Get row and column ownership as index sets

6387:    Not Collective

6389:    Input Arguments:
6390: .  A - matrix of type Elemental

6392:    Output Arguments:
6393: +  rows - rows in which this process owns elements
6394: .  cols - columns in which this process owns elements

6396:    Level: intermediate

6398: .seealso: MatGetOwnershipRange(), MatGetOwnershipRangeColumn(), MatSetValues(), MATELEMENTAL, MatSetValues()
6399: @*/
6400: PetscErrorCode MatGetOwnershipIS(Mat A,IS *rows,IS *cols)
6401: {
6402:   PetscErrorCode ierr,(*f)(Mat,IS*,IS*);

6405:   MatCheckPreallocated(A,1);
6406:   PetscObjectQueryFunction((PetscObject)A,"MatGetOwnershipIS_C",&f);
6407:   if (f) {
6408:     (*f)(A,rows,cols);
6409:   } else {   /* Create a standard row-based partition, each process is responsible for ALL columns in their row block */
6410:     if (rows) {ISCreateStride(PETSC_COMM_SELF,A->rmap->n,A->rmap->rstart,1,rows);}
6411:     if (cols) {ISCreateStride(PETSC_COMM_SELF,A->cmap->N,0,1,cols);}
6412:   }
6413:   return(0);
6414: }

6418: /*@C
6419:    MatILUFactorSymbolic - Performs symbolic ILU factorization of a matrix.
6420:    Uses levels of fill only, not drop tolerance. Use MatLUFactorNumeric()
6421:    to complete the factorization.

6423:    Collective on Mat

6425:    Input Parameters:
6426: +  mat - the matrix
6427: .  row - row permutation
6428: .  column - column permutation
6429: -  info - structure containing
6430: $      levels - number of levels of fill.
6431: $      expected fill - as ratio of original fill.
6432: $      1 or 0 - indicating force fill on diagonal (improves robustness for matrices
6433:                 missing diagonal entries)

6435:    Output Parameters:
6436: .  fact - new matrix that has been symbolically factored

6438:    Notes: See Users-Manual: ch_mat for additional information about choosing the fill factor for better efficiency.

6440:    Most users should employ the simplified KSP interface for linear solvers
6441:    instead of working directly with matrix algebra routines such as this.
6442:    See, e.g., KSPCreate().

6444:    Level: developer

6446:   Concepts: matrices^symbolic LU factorization
6447:   Concepts: matrices^factorization
6448:   Concepts: LU^symbolic factorization

6450: .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor()
6451:           MatGetOrdering(), MatFactorInfo

6453:     Developer Note: fortran interface is not autogenerated as the f90
6454:     interface defintion cannot be generated correctly [due to MatFactorInfo]

6456: @*/
6457: PetscErrorCode MatILUFactorSymbolic(Mat fact,Mat mat,IS row,IS col,const MatFactorInfo *info)
6458: {

6468:   if (info->levels < 0) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"Levels of fill negative %D",(PetscInt)info->levels);
6469:   if (info->fill < 1.0) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"Expected fill less than 1.0 %g",(double)info->fill);
6470:   if (!(fact)->ops->ilufactorsymbolic) {
6471:     const MatSolverPackage spackage;
6472:     MatFactorGetSolverPackage(fact,&spackage);
6473:     SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Matrix type %s symbolic ILU using solver package %s",((PetscObject)mat)->type_name,spackage);
6474:   }
6475:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
6476:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
6477:   MatCheckPreallocated(mat,2);

6479:   PetscLogEventBegin(MAT_ILUFactorSymbolic,mat,row,col,0);
6480:   (fact->ops->ilufactorsymbolic)(fact,mat,row,col,info);
6481:   PetscLogEventEnd(MAT_ILUFactorSymbolic,mat,row,col,0);
6482:   return(0);
6483: }

6487: /*@C
6488:    MatICCFactorSymbolic - Performs symbolic incomplete
6489:    Cholesky factorization for a symmetric matrix.  Use
6490:    MatCholeskyFactorNumeric() to complete the factorization.

6492:    Collective on Mat

6494:    Input Parameters:
6495: +  mat - the matrix
6496: .  perm - row and column permutation
6497: -  info - structure containing
6498: $      levels - number of levels of fill.
6499: $      expected fill - as ratio of original fill.

6501:    Output Parameter:
6502: .  fact - the factored matrix

6504:    Notes:
6505:    Most users should employ the KSP interface for linear solvers
6506:    instead of working directly with matrix algebra routines such as this.
6507:    See, e.g., KSPCreate().

6509:    Level: developer

6511:   Concepts: matrices^symbolic incomplete Cholesky factorization
6512:   Concepts: matrices^factorization
6513:   Concepts: Cholsky^symbolic factorization

6515: .seealso: MatCholeskyFactorNumeric(), MatCholeskyFactor(), MatFactorInfo

6517:     Developer Note: fortran interface is not autogenerated as the f90
6518:     interface defintion cannot be generated correctly [due to MatFactorInfo]

6520: @*/
6521: PetscErrorCode MatICCFactorSymbolic(Mat fact,Mat mat,IS perm,const MatFactorInfo *info)
6522: {

6531:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
6532:   if (info->levels < 0) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"Levels negative %D",(PetscInt) info->levels);
6533:   if (info->fill < 1.0) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"Expected fill less than 1.0 %g",(double)info->fill);
6534:   if (!(fact)->ops->iccfactorsymbolic) {
6535:     const MatSolverPackage spackage;
6536:     MatFactorGetSolverPackage(fact,&spackage);
6537:     SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Matrix type %s symbolic ICC using solver package %s",((PetscObject)mat)->type_name,spackage);
6538:   }
6539:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
6540:   MatCheckPreallocated(mat,2);

6542:   PetscLogEventBegin(MAT_ICCFactorSymbolic,mat,perm,0,0);
6543:   (fact->ops->iccfactorsymbolic)(fact,mat,perm,info);
6544:   PetscLogEventEnd(MAT_ICCFactorSymbolic,mat,perm,0,0);
6545:   return(0);
6546: }

6550: /*@C
6551:    MatGetSubMatrices - Extracts several submatrices from a matrix. If submat
6552:    points to an array of valid matrices, they may be reused to store the new
6553:    submatrices.

6555:    Collective on Mat

6557:    Input Parameters:
6558: +  mat - the matrix
6559: .  n   - the number of submatrixes to be extracted (on this processor, may be zero)
6560: .  irow, icol - index sets of rows and columns to extract
6561: -  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX

6563:    Output Parameter:
6564: .  submat - the array of submatrices

6566:    Notes:
6567:    MatGetSubMatrices() can extract ONLY sequential submatrices
6568:    (from both sequential and parallel matrices). Use MatGetSubMatrix()
6569:    to extract a parallel submatrix.

6571:    Some matrix types place restrictions on the row and column
6572:    indices, such as that they be sorted or that they be equal to each other.

6574:    The index sets may not have duplicate entries.

6576:    When extracting submatrices from a parallel matrix, each processor can
6577:    form a different submatrix by setting the rows and columns of its
6578:    individual index sets according to the local submatrix desired.

6580:    When finished using the submatrices, the user should destroy
6581:    them with MatDestroyMatrices().

6583:    MAT_REUSE_MATRIX can only be used when the nonzero structure of the
6584:    original matrix has not changed from that last call to MatGetSubMatrices().

6586:    This routine creates the matrices in submat; you should NOT create them before
6587:    calling it. It also allocates the array of matrix pointers submat.

6589:    For BAIJ matrices the index sets must respect the block structure, that is if they
6590:    request one row/column in a block, they must request all rows/columns that are in
6591:    that block. For example, if the block size is 2 you cannot request just row 0 and
6592:    column 0.

6594:    Fortran Note:
6595:    The Fortran interface is slightly different from that given below; it
6596:    requires one to pass in  as submat a Mat (integer) array of size at least m.

6598:    Level: advanced

6600:    Concepts: matrices^accessing submatrices
6601:    Concepts: submatrices

6603: .seealso: MatDestroyMatrices(), MatGetSubMatrix(), MatGetRow(), MatGetDiagonal(), MatReuse
6604: @*/
6605: PetscErrorCode MatGetSubMatrices(Mat mat,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *submat[])
6606: {
6608:   PetscInt       i;
6609:   PetscBool      eq;

6614:   if (n) {
6619:   }
6621:   if (n && scall == MAT_REUSE_MATRIX) {
6624:   }
6625:   if (!mat->ops->getsubmatrices) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
6626:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
6627:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
6628:   MatCheckPreallocated(mat,1);

6630:   PetscLogEventBegin(MAT_GetSubMatrices,mat,0,0,0);
6631:   (*mat->ops->getsubmatrices)(mat,n,irow,icol,scall,submat);
6632:   PetscLogEventEnd(MAT_GetSubMatrices,mat,0,0,0);
6633:   for (i=0; i<n; i++) {
6634:     (*submat)[i]->factortype = MAT_FACTOR_NONE;  /* in case in place factorization was previously done on submatrix */
6635:     if (mat->symmetric || mat->structurally_symmetric || mat->hermitian) {
6636:       ISEqual(irow[i],icol[i],&eq);
6637:       if (eq) {
6638:         if (mat->symmetric) {
6639:           MatSetOption((*submat)[i],MAT_SYMMETRIC,PETSC_TRUE);
6640:         } else if (mat->hermitian) {
6641:           MatSetOption((*submat)[i],MAT_HERMITIAN,PETSC_TRUE);
6642:         } else if (mat->structurally_symmetric) {
6643:           MatSetOption((*submat)[i],MAT_STRUCTURALLY_SYMMETRIC,PETSC_TRUE);
6644:         }
6645:       }
6646:     }
6647:   }
6648:   return(0);
6649: }

6653: PetscErrorCode MatGetSubMatricesMPI(Mat mat,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *submat[])
6654: {
6656:   PetscInt       i;
6657:   PetscBool      eq;

6662:   if (n) {
6667:   }
6669:   if (n && scall == MAT_REUSE_MATRIX) {
6672:   }
6673:   if (!mat->ops->getsubmatricesmpi) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
6674:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
6675:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
6676:   MatCheckPreallocated(mat,1);

6678:   PetscLogEventBegin(MAT_GetSubMatrices,mat,0,0,0);
6679:   (*mat->ops->getsubmatricesmpi)(mat,n,irow,icol,scall,submat);
6680:   PetscLogEventEnd(MAT_GetSubMatrices,mat,0,0,0);
6681:   for (i=0; i<n; i++) {
6682:     if (mat->symmetric || mat->structurally_symmetric || mat->hermitian) {
6683:       ISEqual(irow[i],icol[i],&eq);
6684:       if (eq) {
6685:         if (mat->symmetric) {
6686:           MatSetOption((*submat)[i],MAT_SYMMETRIC,PETSC_TRUE);
6687:         } else if (mat->hermitian) {
6688:           MatSetOption((*submat)[i],MAT_HERMITIAN,PETSC_TRUE);
6689:         } else if (mat->structurally_symmetric) {
6690:           MatSetOption((*submat)[i],MAT_STRUCTURALLY_SYMMETRIC,PETSC_TRUE);
6691:         }
6692:       }
6693:     }
6694:   }
6695:   return(0);
6696: }

6700: /*@C
6701:    MatDestroyMatrices - Destroys a set of matrices obtained with MatGetSubMatrices().

6703:    Collective on Mat

6705:    Input Parameters:
6706: +  n - the number of local matrices
6707: -  mat - the matrices (note that this is a pointer to the array of matrices, just to match the calling
6708:                        sequence of MatGetSubMatrices())

6710:    Level: advanced

6712:     Notes: Frees not only the matrices, but also the array that contains the matrices
6713:            In Fortran will not free the array.

6715: .seealso: MatGetSubMatrices()
6716: @*/
6717: PetscErrorCode MatDestroyMatrices(PetscInt n,Mat *mat[])
6718: {
6720:   PetscInt       i;

6723:   if (!*mat) return(0);
6724:   if (n < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Trying to destroy negative number of matrices %D",n);
6726:   for (i=0; i<n; i++) {
6727:     MatDestroy(&(*mat)[i]);
6728:   }
6729:   /* memory is allocated even if n = 0 */
6730:   PetscFree(*mat);
6731:   *mat = NULL;
6732:   return(0);
6733: }

6737: /*@C
6738:    MatGetSeqNonzeroStructure - Extracts the sequential nonzero structure from a matrix.

6740:    Collective on Mat

6742:    Input Parameters:
6743: .  mat - the matrix

6745:    Output Parameter:
6746: .  matstruct - the sequential matrix with the nonzero structure of mat

6748:   Level: intermediate

6750: .seealso: MatDestroySeqNonzeroStructure(), MatGetSubMatrices(), MatDestroyMatrices()
6751: @*/
6752: PetscErrorCode MatGetSeqNonzeroStructure(Mat mat,Mat *matstruct)
6753: {


6761:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
6762:   MatCheckPreallocated(mat,1);

6764:   if (!mat->ops->getseqnonzerostructure) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Not for matrix type %s\n",((PetscObject)mat)->type_name);
6765:   PetscLogEventBegin(MAT_GetSeqNonzeroStructure,mat,0,0,0);
6766:   (*mat->ops->getseqnonzerostructure)(mat,matstruct);
6767:   PetscLogEventEnd(MAT_GetSeqNonzeroStructure,mat,0,0,0);
6768:   return(0);
6769: }

6773: /*@C
6774:    MatDestroySeqNonzeroStructure - Destroys matrix obtained with MatGetSeqNonzeroStructure().

6776:    Collective on Mat

6778:    Input Parameters:
6779: .  mat - the matrix (note that this is a pointer to the array of matrices, just to match the calling
6780:                        sequence of MatGetSequentialNonzeroStructure())

6782:    Level: advanced

6784:     Notes: Frees not only the matrices, but also the array that contains the matrices

6786: .seealso: MatGetSeqNonzeroStructure()
6787: @*/
6788: PetscErrorCode MatDestroySeqNonzeroStructure(Mat *mat)
6789: {

6794:   MatDestroy(mat);
6795:   return(0);
6796: }

6800: /*@
6801:    MatIncreaseOverlap - Given a set of submatrices indicated by index sets,
6802:    replaces the index sets by larger ones that represent submatrices with
6803:    additional overlap.

6805:    Collective on Mat

6807:    Input Parameters:
6808: +  mat - the matrix
6809: .  n   - the number of index sets
6810: .  is  - the array of index sets (these index sets will changed during the call)
6811: -  ov  - the additional overlap requested

6813:    Options Database:
6814: .  -mat_increase_overlap_scalable - use a scalable algorithm to compute the overlap (supported by MPIAIJ matrix)

6816:    Level: developer

6818:    Concepts: overlap
6819:    Concepts: ASM^computing overlap

6821: .seealso: MatGetSubMatrices()
6822: @*/
6823: PetscErrorCode MatIncreaseOverlap(Mat mat,PetscInt n,IS is[],PetscInt ov)
6824: {

6830:   if (n < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Must have one or more domains, you have %D",n);
6831:   if (n) {
6834:   }
6835:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
6836:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
6837:   MatCheckPreallocated(mat,1);

6839:   if (!ov) return(0);
6840:   if (!mat->ops->increaseoverlap) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
6841:   PetscLogEventBegin(MAT_IncreaseOverlap,mat,0,0,0);
6842:   (*mat->ops->increaseoverlap)(mat,n,is,ov);
6843:   PetscLogEventEnd(MAT_IncreaseOverlap,mat,0,0,0);
6844:   return(0);
6845: }


6848: PetscErrorCode MatIncreaseOverlapSplit_Single(Mat,IS*,PetscInt);

6852: /*@
6853:    MatIncreaseOverlapSplit - Given a set of submatrices indicated by index sets across
6854:    a sub communicator, replaces the index sets by larger ones that represent submatrices with
6855:    additional overlap.

6857:    Collective on Mat

6859:    Input Parameters:
6860: +  mat - the matrix
6861: .  n   - the number of index sets
6862: .  is  - the array of index sets (these index sets will changed during the call)
6863: -  ov  - the additional overlap requested

6865:    Options Database:
6866: .  -mat_increase_overlap_scalable - use a scalable algorithm to compute the overlap (supported by MPIAIJ matrix)

6868:    Level: developer

6870:    Concepts: overlap
6871:    Concepts: ASM^computing overlap

6873: .seealso: MatGetSubMatrices()
6874: @*/
6875: PetscErrorCode MatIncreaseOverlapSplit(Mat mat,PetscInt n,IS is[],PetscInt ov)
6876: {
6877:   PetscInt       i;

6883:   if (n < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Must have one or more domains, you have %D",n);
6884:   if (n) {
6887:   }
6888:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
6889:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
6890:   MatCheckPreallocated(mat,1);
6891:   if (!ov) return(0);
6892:   PetscLogEventBegin(MAT_IncreaseOverlap,mat,0,0,0);
6893:   for(i=0; i<n; i++){
6894:          MatIncreaseOverlapSplit_Single(mat,&is[i],ov);
6895:   }
6896:   PetscLogEventEnd(MAT_IncreaseOverlap,mat,0,0,0);
6897:   return(0);
6898: }




6905: /*@
6906:    MatGetBlockSize - Returns the matrix block size.

6908:    Not Collective

6910:    Input Parameter:
6911: .  mat - the matrix

6913:    Output Parameter:
6914: .  bs - block size

6916:    Notes:
6917:     Block row formats are MATSEQBAIJ, MATMPIBAIJ, MATSEQSBAIJ, MATMPISBAIJ. These formats ALWAYS have square block storage in the matrix.

6919:    If the block size has not been set yet this routine returns 1.

6921:    Level: intermediate

6923:    Concepts: matrices^block size

6925: .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSizes()
6926: @*/
6927: PetscErrorCode MatGetBlockSize(Mat mat,PetscInt *bs)
6928: {
6932:   *bs = PetscAbs(mat->rmap->bs);
6933:   return(0);
6934: }

6938: /*@
6939:    MatGetBlockSizes - Returns the matrix block row and column sizes.

6941:    Not Collective

6943:    Input Parameter:
6944: .  mat - the matrix

6946:    Output Parameter:
6947: .  rbs - row block size
6948: .  cbs - coumn block size

6950:    Notes:
6951:     Block row formats are MATSEQBAIJ, MATMPIBAIJ, MATSEQSBAIJ, MATMPISBAIJ. These formats ALWAYS have square block storage in the matrix.
6952:     If you pass a different block size for the columns than the rows, the row block size determines the square block storage.

6954:    If a block size has not been set yet this routine returns 1.

6956:    Level: intermediate

6958:    Concepts: matrices^block size

6960: .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize(), MatSetBlockSize(), MatSetBlockSizes()
6961: @*/
6962: PetscErrorCode MatGetBlockSizes(Mat mat,PetscInt *rbs, PetscInt *cbs)
6963: {
6968:   if (rbs) *rbs = PetscAbs(mat->rmap->bs);
6969:   if (cbs) *cbs = PetscAbs(mat->cmap->bs);
6970:   return(0);
6971: }

6975: /*@
6976:    MatSetBlockSize - Sets the matrix block size.

6978:    Logically Collective on Mat

6980:    Input Parameters:
6981: +  mat - the matrix
6982: -  bs - block size

6984:    Notes:
6985:     Block row formats are MATSEQBAIJ, MATMPIBAIJ, MATSEQSBAIJ, MATMPISBAIJ. These formats ALWAYS have square block storage in the matrix.

6987:      This must be called before MatSetUp() or MatXXXSetPreallocation() (or will default to 1) and the block size cannot be changed later

6989:    Level: intermediate

6991:    Concepts: matrices^block size

6993: .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize(), MatSetBlockSizes(), MatGetBlockSizes()
6994: @*/
6995: PetscErrorCode MatSetBlockSize(Mat mat,PetscInt bs)
6996: {

7002:   PetscLayoutSetBlockSize(mat->rmap,bs);
7003:   PetscLayoutSetBlockSize(mat->cmap,bs);
7004:   return(0);
7005: }

7009: /*@
7010:    MatSetBlockSizes - Sets the matrix block row and column sizes.

7012:    Logically Collective on Mat

7014:    Input Parameters:
7015: +  mat - the matrix
7016: -  rbs - row block size
7017: -  cbs - column block size

7019:    Notes:
7020:     Block row formats are MATSEQBAIJ, MATMPIBAIJ, MATSEQSBAIJ, MATMPISBAIJ. These formats ALWAYS have square block storage in the matrix.
7021:     If you pass a different block size for the columns than the rows, the row block size determines the square block storage.

7023:     This must be called before MatSetUp() or MatXXXSetPreallocation() (or will default to 1) and the block size cannot be changed later

7025:     The row and column block size determine the blocksize of the "row" and "column" vectors returned by MatCreateVecs().

7027:    Level: intermediate

7029:    Concepts: matrices^block size

7031: .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize(), MatSetBlockSize(), MatGetBlockSizes()
7032: @*/
7033: PetscErrorCode MatSetBlockSizes(Mat mat,PetscInt rbs,PetscInt cbs)
7034: {

7041:   PetscLayoutSetBlockSize(mat->rmap,rbs);
7042:   PetscLayoutSetBlockSize(mat->cmap,cbs);
7043:   return(0);
7044: }

7048: /*@
7049:    MatSetBlockSizesFromMats - Sets the matrix block row and column sizes to match a pair of matrices

7051:    Logically Collective on Mat

7053:    Input Parameters:
7054: +  mat - the matrix
7055: .  fromRow - matrix from which to copy row block size
7056: -  fromCol - matrix from which to copy column block size (can be same as fromRow)

7058:    Level: developer

7060:    Concepts: matrices^block size

7062: .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize(), MatSetBlockSizes()
7063: @*/
7064: PetscErrorCode MatSetBlockSizesFromMats(Mat mat,Mat fromRow,Mat fromCol)
7065: {

7072:   if (fromRow->rmap->bs > 0) {PetscLayoutSetBlockSize(mat->rmap,fromRow->rmap->bs);}
7073:   if (fromCol->cmap->bs > 0) {PetscLayoutSetBlockSize(mat->cmap,fromCol->cmap->bs);}
7074:   return(0);
7075: }

7079: /*@
7080:    MatResidual - Default routine to calculate the residual.

7082:    Collective on Mat and Vec

7084:    Input Parameters:
7085: +  mat - the matrix
7086: .  b   - the right-hand-side
7087: -  x   - the approximate solution

7089:    Output Parameter:
7090: .  r - location to store the residual

7092:    Level: developer

7094: .keywords: MG, default, multigrid, residual

7096: .seealso: PCMGSetResidual()
7097: @*/
7098: PetscErrorCode MatResidual(Mat mat,Vec b,Vec x,Vec r)
7099: {

7108:   MatCheckPreallocated(mat,1);
7109:   PetscLogEventBegin(MAT_Residual,mat,0,0,0);
7110:   if (!mat->ops->residual) {
7111:     MatMult(mat,x,r);
7112:     VecAYPX(r,-1.0,b);
7113:   } else {
7114:     (*mat->ops->residual)(mat,b,x,r);
7115:   }
7116:   PetscLogEventEnd(MAT_Residual,mat,0,0,0);
7117:   return(0);
7118: }

7122: /*@C
7123:     MatGetRowIJ - Returns the compressed row storage i and j indices for sequential matrices.

7125:    Collective on Mat

7127:     Input Parameters:
7128: +   mat - the matrix
7129: .   shift -  0 or 1 indicating we want the indices starting at 0 or 1
7130: .   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be   symmetrized
7131: -   inodecompressed - PETSC_TRUE or PETSC_FALSE  indicating if the nonzero structure of the
7132:                  inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is
7133:                  always used.

7135:     Output Parameters:
7136: +   n - number of rows in the (possibly compressed) matrix
7137: .   ia - the row pointers [of length n+1]
7138: .   ja - the column indices
7139: -   done - indicates if the routine actually worked and returned appropriate ia[] and ja[] arrays; callers
7140:            are responsible for handling the case when done == PETSC_FALSE and ia and ja are not set

7142:     Level: developer

7144:     Notes: You CANNOT change any of the ia[] or ja[] values.

7146:            Use MatRestoreRowIJ() when you are finished accessing the ia[] and ja[] values

7148:     Fortran Node

7150:            In Fortran use
7151: $           PetscInt ia(1), ja(1)
7152: $           PetscOffset iia, jja
7153: $      call MatGetRowIJ(mat,shift,symmetric,inodecompressed,n,ia,iia,ja,jja,done,ierr)
7154: $      Acess the ith and jth entries via ia(iia + i) and ja(jja + j)
7155: $
7156: $          or
7157: $
7158: $           PetscInt, pointer :: ia(:),ja(:)
7159: $    call  MatGetRowIJF90(mat,shift,symmetric,inodecompressed,n,ia,ja,done,ierr)
7160: $      Acess the ith and jth entries via ia(i) and ja(j)



7164: .seealso: MatGetColumnIJ(), MatRestoreRowIJ(), MatSeqAIJGetArray()
7165: @*/
7166: PetscErrorCode MatGetRowIJ(Mat mat,PetscInt shift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
7167: {

7177:   MatCheckPreallocated(mat,1);
7178:   if (!mat->ops->getrowij) *done = PETSC_FALSE;
7179:   else {
7180:     *done = PETSC_TRUE;
7181:     PetscLogEventBegin(MAT_GetRowIJ,mat,0,0,0);
7182:     (*mat->ops->getrowij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);
7183:     PetscLogEventEnd(MAT_GetRowIJ,mat,0,0,0);
7184:   }
7185:   return(0);
7186: }

7190: /*@C
7191:     MatGetColumnIJ - Returns the compressed column storage i and j indices for sequential matrices.

7193:     Collective on Mat

7195:     Input Parameters:
7196: +   mat - the matrix
7197: .   shift - 1 or zero indicating we want the indices starting at 0 or 1
7198: .   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be
7199:                 symmetrized
7200: .   inodecompressed - PETSC_TRUE or PETSC_FALSE indicating if the nonzero structure of the
7201:                  inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is
7202:                  always used.
7203: .   n - number of columns in the (possibly compressed) matrix
7204: .   ia - the column pointers
7205: -   ja - the row indices

7207:     Output Parameters:
7208: .   done - PETSC_TRUE or PETSC_FALSE, indicating whether the values have been returned

7210:     Note:
7211:     This routine zeros out n, ia, and ja. This is to prevent accidental
7212:     us of the array after it has been restored. If you pass NULL, it will
7213:     not zero the pointers.  Use of ia or ja after MatRestoreColumnIJ() is invalid.

7215:     Level: developer

7217: .seealso: MatGetRowIJ(), MatRestoreColumnIJ()
7218: @*/
7219: PetscErrorCode MatGetColumnIJ(Mat mat,PetscInt shift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
7220: {

7230:   MatCheckPreallocated(mat,1);
7231:   if (!mat->ops->getcolumnij) *done = PETSC_FALSE;
7232:   else {
7233:     *done = PETSC_TRUE;
7234:     (*mat->ops->getcolumnij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);
7235:   }
7236:   return(0);
7237: }

7241: /*@C
7242:     MatRestoreRowIJ - Call after you are completed with the ia,ja indices obtained with
7243:     MatGetRowIJ().

7245:     Collective on Mat

7247:     Input Parameters:
7248: +   mat - the matrix
7249: .   shift - 1 or zero indicating we want the indices starting at 0 or 1
7250: .   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be
7251:                 symmetrized
7252: .   inodecompressed -  PETSC_TRUE or PETSC_FALSE indicating if the nonzero structure of the
7253:                  inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is
7254:                  always used.
7255: .   n - size of (possibly compressed) matrix
7256: .   ia - the row pointers
7257: -   ja - the column indices

7259:     Output Parameters:
7260: .   done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned

7262:     Note:
7263:     This routine zeros out n, ia, and ja. This is to prevent accidental
7264:     us of the array after it has been restored. If you pass NULL, it will
7265:     not zero the pointers.  Use of ia or ja after MatRestoreRowIJ() is invalid.

7267:     Level: developer

7269: .seealso: MatGetRowIJ(), MatRestoreColumnIJ()
7270: @*/
7271: PetscErrorCode MatRestoreRowIJ(Mat mat,PetscInt shift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
7272: {

7281:   MatCheckPreallocated(mat,1);

7283:   if (!mat->ops->restorerowij) *done = PETSC_FALSE;
7284:   else {
7285:     *done = PETSC_TRUE;
7286:     (*mat->ops->restorerowij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);
7287:     if (n)  *n = 0;
7288:     if (ia) *ia = NULL;
7289:     if (ja) *ja = NULL;
7290:   }
7291:   return(0);
7292: }

7296: /*@C
7297:     MatRestoreColumnIJ - Call after you are completed with the ia,ja indices obtained with
7298:     MatGetColumnIJ().

7300:     Collective on Mat

7302:     Input Parameters:
7303: +   mat - the matrix
7304: .   shift - 1 or zero indicating we want the indices starting at 0 or 1
7305: -   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be
7306:                 symmetrized
7307: -   inodecompressed - PETSC_TRUE or PETSC_FALSE indicating if the nonzero structure of the
7308:                  inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is
7309:                  always used.

7311:     Output Parameters:
7312: +   n - size of (possibly compressed) matrix
7313: .   ia - the column pointers
7314: .   ja - the row indices
7315: -   done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned

7317:     Level: developer

7319: .seealso: MatGetColumnIJ(), MatRestoreRowIJ()
7320: @*/
7321: PetscErrorCode MatRestoreColumnIJ(Mat mat,PetscInt shift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
7322: {

7331:   MatCheckPreallocated(mat,1);

7333:   if (!mat->ops->restorecolumnij) *done = PETSC_FALSE;
7334:   else {
7335:     *done = PETSC_TRUE;
7336:     (*mat->ops->restorecolumnij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);
7337:     if (n)  *n = 0;
7338:     if (ia) *ia = NULL;
7339:     if (ja) *ja = NULL;
7340:   }
7341:   return(0);
7342: }

7346: /*@C
7347:     MatColoringPatch -Used inside matrix coloring routines that
7348:     use MatGetRowIJ() and/or MatGetColumnIJ().

7350:     Collective on Mat

7352:     Input Parameters:
7353: +   mat - the matrix
7354: .   ncolors - max color value
7355: .   n   - number of entries in colorarray
7356: -   colorarray - array indicating color for each column

7358:     Output Parameters:
7359: .   iscoloring - coloring generated using colorarray information

7361:     Level: developer

7363: .seealso: MatGetRowIJ(), MatGetColumnIJ()

7365: @*/
7366: PetscErrorCode MatColoringPatch(Mat mat,PetscInt ncolors,PetscInt n,ISColoringValue colorarray[],ISColoring *iscoloring)
7367: {

7375:   MatCheckPreallocated(mat,1);

7377:   if (!mat->ops->coloringpatch) {
7378:     ISColoringCreate(PetscObjectComm((PetscObject)mat),ncolors,n,colorarray,PETSC_OWN_POINTER,iscoloring);
7379:   } else {
7380:     (*mat->ops->coloringpatch)(mat,ncolors,n,colorarray,iscoloring);
7381:   }
7382:   return(0);
7383: }


7388: /*@
7389:    MatSetUnfactored - Resets a factored matrix to be treated as unfactored.

7391:    Logically Collective on Mat

7393:    Input Parameter:
7394: .  mat - the factored matrix to be reset

7396:    Notes:
7397:    This routine should be used only with factored matrices formed by in-place
7398:    factorization via ILU(0) (or by in-place LU factorization for the MATSEQDENSE
7399:    format).  This option can save memory, for example, when solving nonlinear
7400:    systems with a matrix-free Newton-Krylov method and a matrix-based, in-place
7401:    ILU(0) preconditioner.

7403:    Note that one can specify in-place ILU(0) factorization by calling
7404: .vb
7405:      PCType(pc,PCILU);
7406:      PCFactorSeUseInPlace(pc);
7407: .ve
7408:    or by using the options -pc_type ilu -pc_factor_in_place

7410:    In-place factorization ILU(0) can also be used as a local
7411:    solver for the blocks within the block Jacobi or additive Schwarz
7412:    methods (runtime option: -sub_pc_factor_in_place).  See Users-Manual: ch_pc
7413:    for details on setting local solver options.

7415:    Most users should employ the simplified KSP interface for linear solvers
7416:    instead of working directly with matrix algebra routines such as this.
7417:    See, e.g., KSPCreate().

7419:    Level: developer

7421: .seealso: PCFactorSetUseInPlace(), PCFactorGetUseInPlace()

7423:    Concepts: matrices^unfactored

7425: @*/
7426: PetscErrorCode MatSetUnfactored(Mat mat)
7427: {

7433:   MatCheckPreallocated(mat,1);
7434:   mat->factortype = MAT_FACTOR_NONE;
7435:   if (!mat->ops->setunfactored) return(0);
7436:   (*mat->ops->setunfactored)(mat);
7437:   return(0);
7438: }

7440: /*MC
7441:     MatDenseGetArrayF90 - Accesses a matrix array from Fortran90.

7443:     Synopsis:
7444:     MatDenseGetArrayF90(Mat x,{Scalar, pointer :: xx_v(:,:)},integer ierr)

7446:     Not collective

7448:     Input Parameter:
7449: .   x - matrix

7451:     Output Parameters:
7452: +   xx_v - the Fortran90 pointer to the array
7453: -   ierr - error code

7455:     Example of Usage:
7456: .vb
7457:       PetscScalar, pointer xx_v(:,:)
7458:       ....
7459:       call MatDenseGetArrayF90(x,xx_v,ierr)
7460:       a = xx_v(3)
7461:       call MatDenseRestoreArrayF90(x,xx_v,ierr)
7462: .ve

7464:     Level: advanced

7466: .seealso:  MatDenseRestoreArrayF90(), MatDenseGetArray(), MatDenseRestoreArray(), MatSeqAIJGetArrayF90()

7468:     Concepts: matrices^accessing array

7470: M*/

7472: /*MC
7473:     MatDenseRestoreArrayF90 - Restores a matrix array that has been
7474:     accessed with MatDenseGetArrayF90().

7476:     Synopsis:
7477:     MatDenseRestoreArrayF90(Mat x,{Scalar, pointer :: xx_v(:,:)},integer ierr)

7479:     Not collective

7481:     Input Parameters:
7482: +   x - matrix
7483: -   xx_v - the Fortran90 pointer to the array

7485:     Output Parameter:
7486: .   ierr - error code

7488:     Example of Usage:
7489: .vb
7490:        PetscScalar, pointer xx_v(:,:)
7491:        ....
7492:        call MatDenseGetArrayF90(x,xx_v,ierr)
7493:        a = xx_v(3)
7494:        call MatDenseRestoreArrayF90(x,xx_v,ierr)
7495: .ve

7497:     Level: advanced

7499: .seealso:  MatDenseGetArrayF90(), MatDenseGetArray(), MatDenseRestoreArray(), MatSeqAIJRestoreArrayF90()

7501: M*/


7504: /*MC
7505:     MatSeqAIJGetArrayF90 - Accesses a matrix array from Fortran90.

7507:     Synopsis:
7508:     MatSeqAIJGetArrayF90(Mat x,{Scalar, pointer :: xx_v(:)},integer ierr)

7510:     Not collective

7512:     Input Parameter:
7513: .   x - matrix

7515:     Output Parameters:
7516: +   xx_v - the Fortran90 pointer to the array
7517: -   ierr - error code

7519:     Example of Usage:
7520: .vb
7521:       PetscScalar, pointer xx_v(:)
7522:       ....
7523:       call MatSeqAIJGetArrayF90(x,xx_v,ierr)
7524:       a = xx_v(3)
7525:       call MatSeqAIJRestoreArrayF90(x,xx_v,ierr)
7526: .ve

7528:     Level: advanced

7530: .seealso:  MatSeqAIJRestoreArrayF90(), MatSeqAIJGetArray(), MatSeqAIJRestoreArray(), MatDenseGetArrayF90()

7532:     Concepts: matrices^accessing array

7534: M*/

7536: /*MC
7537:     MatSeqAIJRestoreArrayF90 - Restores a matrix array that has been
7538:     accessed with MatSeqAIJGetArrayF90().

7540:     Synopsis:
7541:     MatSeqAIJRestoreArrayF90(Mat x,{Scalar, pointer :: xx_v(:)},integer ierr)

7543:     Not collective

7545:     Input Parameters:
7546: +   x - matrix
7547: -   xx_v - the Fortran90 pointer to the array

7549:     Output Parameter:
7550: .   ierr - error code

7552:     Example of Usage:
7553: .vb
7554:        PetscScalar, pointer xx_v(:)
7555:        ....
7556:        call MatSeqAIJGetArrayF90(x,xx_v,ierr)
7557:        a = xx_v(3)
7558:        call MatSeqAIJRestoreArrayF90(x,xx_v,ierr)
7559: .ve

7561:     Level: advanced

7563: .seealso:  MatSeqAIJGetArrayF90(), MatSeqAIJGetArray(), MatSeqAIJRestoreArray(), MatDenseRestoreArrayF90()

7565: M*/


7570: /*@
7571:     MatGetSubMatrix - Gets a single submatrix on the same number of processors
7572:                       as the original matrix.

7574:     Collective on Mat

7576:     Input Parameters:
7577: +   mat - the original matrix
7578: .   isrow - parallel IS containing the rows this processor should obtain
7579: .   iscol - parallel IS containing all columns you wish to keep. Each process should list the columns that will be in IT's "diagonal part" in the new matrix.
7580: -   cll - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX

7582:     Output Parameter:
7583: .   newmat - the new submatrix, of the same type as the old

7585:     Level: advanced

7587:     Notes:
7588:     The submatrix will be able to be multiplied with vectors using the same layout as iscol.

7590:     Some matrix types place restrictions on the row and column indices, such
7591:     as that they be sorted or that they be equal to each other.

7593:     The index sets may not have duplicate entries.

7595:       The first time this is called you should use a cll of MAT_INITIAL_MATRIX,
7596:    the MatGetSubMatrix() routine will create the newmat for you. Any additional calls
7597:    to this routine with a mat of the same nonzero structure and with a call of MAT_REUSE_MATRIX
7598:    will reuse the matrix generated the first time.  You should call MatDestroy() on newmat when
7599:    you are finished using it.

7601:     The communicator of the newly obtained matrix is ALWAYS the same as the communicator of
7602:     the input matrix.

7604:     If iscol is NULL then all columns are obtained (not supported in Fortran).

7606:    Example usage:
7607:    Consider the following 8x8 matrix with 34 non-zero values, that is
7608:    assembled across 3 processors. Let's assume that proc0 owns 3 rows,
7609:    proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown
7610:    as follows:

7612: .vb
7613:             1  2  0  |  0  3  0  |  0  4
7614:     Proc0   0  5  6  |  7  0  0  |  8  0
7615:             9  0 10  | 11  0  0  | 12  0
7616:     -------------------------------------
7617:            13  0 14  | 15 16 17  |  0  0
7618:     Proc1   0 18  0  | 19 20 21  |  0  0
7619:             0  0  0  | 22 23  0  | 24  0
7620:     -------------------------------------
7621:     Proc2  25 26 27  |  0  0 28  | 29  0
7622:            30  0  0  | 31 32 33  |  0 34
7623: .ve

7625:     Suppose isrow = [0 1 | 4 | 6 7] and iscol = [1 2 | 3 4 5 | 6].  The resulting submatrix is

7627: .vb
7628:             2  0  |  0  3  0  |  0
7629:     Proc0   5  6  |  7  0  0  |  8
7630:     -------------------------------
7631:     Proc1  18  0  | 19 20 21  |  0
7632:     -------------------------------
7633:     Proc2  26 27  |  0  0 28  | 29
7634:             0  0  | 31 32 33  |  0
7635: .ve


7638:     Concepts: matrices^submatrices

7640: .seealso: MatGetSubMatrices()
7641: @*/
7642: PetscErrorCode MatGetSubMatrix(Mat mat,IS isrow,IS iscol,MatReuse cll,Mat *newmat)
7643: {
7645:   PetscMPIInt    size;
7646:   Mat            *local;
7647:   IS             iscoltmp;

7656:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
7657:   if (cll == MAT_IGNORE_MATRIX) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Cannot use MAT_IGNORE_MATRIX");

7659:   MatCheckPreallocated(mat,1);
7660:   MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);

7662:   if (!iscol || isrow == iscol) {
7663:     PetscBool   stride;
7664:     PetscMPIInt grabentirematrix = 0,grab;
7665:     PetscObjectTypeCompare((PetscObject)isrow,ISSTRIDE,&stride);
7666:     if (stride) {
7667:       PetscInt first,step,n,rstart,rend;
7668:       ISStrideGetInfo(isrow,&first,&step);
7669:       if (step == 1) {
7670:         MatGetOwnershipRange(mat,&rstart,&rend);
7671:         if (rstart == first) {
7672:           ISGetLocalSize(isrow,&n);
7673:           if (n == rend-rstart) {
7674:             grabentirematrix = 1;
7675:           }
7676:         }
7677:       }
7678:     }
7679:     MPIU_Allreduce(&grabentirematrix,&grab,1,MPI_INT,MPI_MIN,PetscObjectComm((PetscObject)mat));
7680:     if (grab) {
7681:       PetscInfo(mat,"Getting entire matrix as submatrix\n");
7682:       if (cll == MAT_INITIAL_MATRIX) {
7683:         *newmat = mat;
7684:         PetscObjectReference((PetscObject)mat);
7685:       }
7686:       return(0);
7687:     }
7688:   }

7690:   if (!iscol) {
7691:     ISCreateStride(PetscObjectComm((PetscObject)mat),mat->cmap->n,mat->cmap->rstart,1,&iscoltmp);
7692:   } else {
7693:     iscoltmp = iscol;
7694:   }

7696:   /* if original matrix is on just one processor then use submatrix generated */
7697:   if (mat->ops->getsubmatrices && !mat->ops->getsubmatrix && size == 1 && cll == MAT_REUSE_MATRIX) {
7698:     MatGetSubMatrices(mat,1,&isrow,&iscoltmp,MAT_REUSE_MATRIX,&newmat);
7699:     if (!iscol) {ISDestroy(&iscoltmp);}
7700:     return(0);
7701:   } else if (mat->ops->getsubmatrices && !mat->ops->getsubmatrix && size == 1) {
7702:     MatGetSubMatrices(mat,1,&isrow,&iscoltmp,MAT_INITIAL_MATRIX,&local);
7703:     *newmat = *local;
7704:     PetscFree(local);
7705:     if (!iscol) {ISDestroy(&iscoltmp);}
7706:     return(0);
7707:   } else if (!mat->ops->getsubmatrix) {
7708:     /* Create a new matrix type that implements the operation using the full matrix */
7709:     PetscLogEventBegin(MAT_GetSubMatrix,mat,0,0,0);
7710:     switch (cll) {
7711:     case MAT_INITIAL_MATRIX:
7712:       MatCreateSubMatrix(mat,isrow,iscoltmp,newmat);
7713:       break;
7714:     case MAT_REUSE_MATRIX:
7715:       MatSubMatrixUpdate(*newmat,mat,isrow,iscoltmp);
7716:       break;
7717:     default: SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"Invalid MatReuse, must be either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX");
7718:     }
7719:     PetscLogEventEnd(MAT_GetSubMatrix,mat,0,0,0);
7720:     if (!iscol) {ISDestroy(&iscoltmp);}
7721:     return(0);
7722:   }

7724:   if (!mat->ops->getsubmatrix) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
7725:   PetscLogEventBegin(MAT_GetSubMatrix,mat,0,0,0);
7726:   (*mat->ops->getsubmatrix)(mat,isrow,iscoltmp,cll,newmat);
7727:   PetscLogEventEnd(MAT_GetSubMatrix,mat,0,0,0);
7728:   if (!iscol) {ISDestroy(&iscoltmp);}
7729:   if (*newmat && cll == MAT_INITIAL_MATRIX) {PetscObjectStateIncrease((PetscObject)*newmat);}
7730:   return(0);
7731: }

7735: /*@
7736:    MatStashSetInitialSize - sets the sizes of the matrix stash, that is
7737:    used during the assembly process to store values that belong to
7738:    other processors.

7740:    Not Collective

7742:    Input Parameters:
7743: +  mat   - the matrix
7744: .  size  - the initial size of the stash.
7745: -  bsize - the initial size of the block-stash(if used).

7747:    Options Database Keys:
7748: +   -matstash_initial_size <size> or <size0,size1,...sizep-1>
7749: -   -matstash_block_initial_size <bsize>  or <bsize0,bsize1,...bsizep-1>

7751:    Level: intermediate

7753:    Notes:
7754:      The block-stash is used for values set with MatSetValuesBlocked() while
7755:      the stash is used for values set with MatSetValues()

7757:      Run with the option -info and look for output of the form
7758:      MatAssemblyBegin_MPIXXX:Stash has MM entries, uses nn mallocs.
7759:      to determine the appropriate value, MM, to use for size and
7760:      MatAssemblyBegin_MPIXXX:Block-Stash has BMM entries, uses nn mallocs.
7761:      to determine the value, BMM to use for bsize

7763:    Concepts: stash^setting matrix size
7764:    Concepts: matrices^stash

7766: .seealso: MatAssemblyBegin(), MatAssemblyEnd(), Mat, MatStashGetInfo()

7768: @*/
7769: PetscErrorCode MatStashSetInitialSize(Mat mat,PetscInt size, PetscInt bsize)
7770: {

7776:   MatStashSetInitialSize_Private(&mat->stash,size);
7777:   MatStashSetInitialSize_Private(&mat->bstash,bsize);
7778:   return(0);
7779: }

7783: /*@
7784:    MatInterpolateAdd - w = y + A*x or A'*x depending on the shape of
7785:      the matrix

7787:    Neighbor-wise Collective on Mat

7789:    Input Parameters:
7790: +  mat   - the matrix
7791: .  x,y - the vectors
7792: -  w - where the result is stored

7794:    Level: intermediate

7796:    Notes:
7797:     w may be the same vector as y.

7799:     This allows one to use either the restriction or interpolation (its transpose)
7800:     matrix to do the interpolation

7802:     Concepts: interpolation

7804: .seealso: MatMultAdd(), MatMultTransposeAdd(), MatRestrict()

7806: @*/
7807: PetscErrorCode MatInterpolateAdd(Mat A,Vec x,Vec y,Vec w)
7808: {
7810:   PetscInt       M,N,Ny;

7818:   MatCheckPreallocated(A,1);
7819:   MatGetSize(A,&M,&N);
7820:   VecGetSize(y,&Ny);
7821:   if (M == Ny) {
7822:     MatMultAdd(A,x,y,w);
7823:   } else {
7824:     MatMultTransposeAdd(A,x,y,w);
7825:   }
7826:   return(0);
7827: }

7831: /*@
7832:    MatInterpolate - y = A*x or A'*x depending on the shape of
7833:      the matrix

7835:    Neighbor-wise Collective on Mat

7837:    Input Parameters:
7838: +  mat   - the matrix
7839: -  x,y - the vectors

7841:    Level: intermediate

7843:    Notes:
7844:     This allows one to use either the restriction or interpolation (its transpose)
7845:     matrix to do the interpolation

7847:    Concepts: matrices^interpolation

7849: .seealso: MatMultAdd(), MatMultTransposeAdd(), MatRestrict()

7851: @*/
7852: PetscErrorCode MatInterpolate(Mat A,Vec x,Vec y)
7853: {
7855:   PetscInt       M,N,Ny;

7862:   MatCheckPreallocated(A,1);
7863:   MatGetSize(A,&M,&N);
7864:   VecGetSize(y,&Ny);
7865:   if (M == Ny) {
7866:     MatMult(A,x,y);
7867:   } else {
7868:     MatMultTranspose(A,x,y);
7869:   }
7870:   return(0);
7871: }

7875: /*@
7876:    MatRestrict - y = A*x or A'*x

7878:    Neighbor-wise Collective on Mat

7880:    Input Parameters:
7881: +  mat   - the matrix
7882: -  x,y - the vectors

7884:    Level: intermediate

7886:    Notes:
7887:     This allows one to use either the restriction or interpolation (its transpose)
7888:     matrix to do the restriction

7890:    Concepts: matrices^restriction

7892: .seealso: MatMultAdd(), MatMultTransposeAdd(), MatInterpolate()

7894: @*/
7895: PetscErrorCode MatRestrict(Mat A,Vec x,Vec y)
7896: {
7898:   PetscInt       M,N,Ny;

7905:   MatCheckPreallocated(A,1);

7907:   MatGetSize(A,&M,&N);
7908:   VecGetSize(y,&Ny);
7909:   if (M == Ny) {
7910:     MatMult(A,x,y);
7911:   } else {
7912:     MatMultTranspose(A,x,y);
7913:   }
7914:   return(0);
7915: }

7919: /*@
7920:    MatGetNullSpace - retrieves the null space to a matrix.

7922:    Logically Collective on Mat and MatNullSpace

7924:    Input Parameters:
7925: +  mat - the matrix
7926: -  nullsp - the null space object

7928:    Level: developer

7930:    Concepts: null space^attaching to matrix

7932: .seealso: MatCreate(), MatNullSpaceCreate(), MatSetNearNullSpace(), MatSetNullSpace()
7933: @*/
7934: PetscErrorCode MatGetNullSpace(Mat mat, MatNullSpace *nullsp)
7935: {
7940:   *nullsp = mat->nullsp;
7941:   return(0);
7942: }

7946: /*@
7947:    MatSetNullSpace - attaches a null space to a matrix.

7949:    Logically Collective on Mat and MatNullSpace

7951:    Input Parameters:
7952: +  mat - the matrix
7953: -  nullsp - the null space object

7955:    Level: advanced

7957:    Notes:
7958:       This null space is used by the linear solvers. Overwrites any previous null space that may have been attached

7960:       For inconsistent singular systems (linear systems where the right hand side is not in the range of the operator) you also likely should
7961:       call MatSetTransposeNullSpace(). This allows the linear system to be solved in a least squares sense.

7963:       You can remove the null space by calling this routine with an nullsp of NULL


7966:       The fundamental theorem of linear algebra (Gilbert Strang, Introduction to Applied Mathematics, page 72) states that
7967:    the domain of a matrix A (from R^n to R^m (m rows, n columns) R^n = the direct sum of the null space of A, n(A), + the range of A^T, R(A^T).
7968:    Similarly R^m = direct sum n(A^T) + R(A).  Hence the linear system A x = b has a solution only if b in R(A) (or correspondingly b is orthogonal to
7969:    n(A^T)) and if x is a solution then x + alpha n(A) is a solution for any alpha. The minimum norm solution is orthogonal to n(A). For problems without a solution
7970:    the solution that minimizes the norm of the residual (the least squares solution) can be obtained by solving A x = \hat{b} where \hat{b} is b orthogonalized to the n(A^T).

7972:       Krylov solvers can produce the minimal norm solution to the least squares problem by utilizing MatNullSpaceRemove().

7974:    Concepts: null space^attaching to matrix

7976: .seealso: MatCreate(), MatNullSpaceCreate(), MatSetNearNullSpace(), MatGetNullSpace(), MatSetTransposeNullSpace(), MatGetTransposeNullSpace(), MatNullSpaceRemove()
7977: @*/
7978: PetscErrorCode MatSetNullSpace(Mat mat,MatNullSpace nullsp)
7979: {

7986:   MatCheckPreallocated(mat,1);
7987:   if (nullsp) {PetscObjectReference((PetscObject)nullsp);}
7988:   MatNullSpaceDestroy(&mat->nullsp);
7989:   mat->nullsp = nullsp;
7990:   return(0);
7991: }

7995: /*@
7996:    MatGetTransposeNullSpace - retrieves the null space to a matrix.

7998:    Logically Collective on Mat and MatNullSpace

8000:    Input Parameters:
8001: +  mat - the matrix
8002: -  nullsp - the null space object

8004:    Level: developer

8006:    Notes:
8007:       This null space is used by solvers. Overwrites any previous null space that may have been attached

8009:    Concepts: null space^attaching to matrix

8011: .seealso: MatCreate(), MatNullSpaceCreate(), MatSetNearNullSpace()
8012: @*/
8013: PetscErrorCode MatGetTransposeNullSpace(Mat mat, MatNullSpace *nullsp)
8014: {
8019:   *nullsp = mat->transnullsp;
8020:   return(0);
8021: }

8025: /*@
8026:    MatSetTransposeNullSpace - attaches a null space to a matrix.

8028:    Logically Collective on Mat and MatNullSpace

8030:    Input Parameters:
8031: +  mat - the matrix
8032: -  nullsp - the null space object

8034:    Level: advanced

8036:    Notes:
8037:       For inconsistent singular systems (linear systems where the right hand side is not in the range of the operator) this allows the linear system to be solved in a least squares sense.
8038:       You must also call MatSetNullSpace()


8041:       The fundamental theorem of linear algebra (Gilbert Strang, Introduction to Applied Mathematics, page 72) states that
8042:    the domain of a matrix A (from R^n to R^m (m rows, n columns) R^n = the direct sum of the null space of A, n(A), + the range of A^T, R(A^T).
8043:    Similarly R^m = direct sum n(A^T) + R(A).  Hence the linear system A x = b has a solution only if b in R(A) (or correspondingly b is orthogonal to
8044:    n(A^T)) and if x is a solution then x + alpha n(A) is a solution for any alpha. The minimum norm solution is orthogonal to n(A). For problems without a solution
8045:    the solution that minimizes the norm of the residual (the least squares solution) can be obtained by solving A x = \hat{b} where \hat{b} is b orthogonalized to the n(A^T).

8047:       Krylov solvers can produce the minimal norm solution to the least squares problem by utilizing MatNullSpaceRemove().

8049:    Concepts: null space^attaching to matrix

8051: .seealso: MatCreate(), MatNullSpaceCreate(), MatSetNearNullSpace(), MatGetNullSpace(), MatSetNullSpace(), MatGetNullSpace(), MatNullSpaceRemove()
8052: @*/
8053: PetscErrorCode MatSetTransposeNullSpace(Mat mat,MatNullSpace nullsp)
8054: {

8061:   MatCheckPreallocated(mat,1);
8062:   PetscObjectReference((PetscObject)nullsp);
8063:   MatNullSpaceDestroy(&mat->transnullsp);
8064:   mat->transnullsp = nullsp;
8065:   return(0);
8066: }

8070: /*@
8071:    MatSetNearNullSpace - attaches a null space to a matrix.
8072:         This null space will be used to provide near null space vectors to a multigrid preconditioner built from this matrix.

8074:    Logically Collective on Mat and MatNullSpace

8076:    Input Parameters:
8077: +  mat - the matrix
8078: -  nullsp - the null space object

8080:    Level: advanced

8082:    Notes:
8083:       Overwrites any previous near null space that may have been attached

8085:       You can remove the null space by calling this routine with an nullsp of NULL

8087:    Concepts: null space^attaching to matrix

8089: .seealso: MatCreate(), MatNullSpaceCreate(), MatSetNullSpace()
8090: @*/
8091: PetscErrorCode MatSetNearNullSpace(Mat mat,MatNullSpace nullsp)
8092: {

8099:   MatCheckPreallocated(mat,1);
8100:   if (nullsp) {PetscObjectReference((PetscObject)nullsp);}
8101:   MatNullSpaceDestroy(&mat->nearnullsp);
8102:   mat->nearnullsp = nullsp;
8103:   return(0);
8104: }

8108: /*@
8109:    MatGetNearNullSpace -Get null space attached with MatSetNearNullSpace()

8111:    Not Collective

8113:    Input Parameters:
8114: .  mat - the matrix

8116:    Output Parameters:
8117: .  nullsp - the null space object, NULL if not set

8119:    Level: developer

8121:    Concepts: null space^attaching to matrix

8123: .seealso: MatSetNearNullSpace(), MatGetNullSpace()
8124: @*/
8125: PetscErrorCode MatGetNearNullSpace(Mat mat,MatNullSpace *nullsp)
8126: {
8131:   MatCheckPreallocated(mat,1);
8132:   *nullsp = mat->nearnullsp;
8133:   return(0);
8134: }

8138: /*@C
8139:    MatICCFactor - Performs in-place incomplete Cholesky factorization of matrix.

8141:    Collective on Mat

8143:    Input Parameters:
8144: +  mat - the matrix
8145: .  row - row/column permutation
8146: .  fill - expected fill factor >= 1.0
8147: -  level - level of fill, for ICC(k)

8149:    Notes:
8150:    Probably really in-place only when level of fill is zero, otherwise allocates
8151:    new space to store factored matrix and deletes previous memory.

8153:    Most users should employ the simplified KSP interface for linear solvers
8154:    instead of working directly with matrix algebra routines such as this.
8155:    See, e.g., KSPCreate().

8157:    Level: developer

8159:    Concepts: matrices^incomplete Cholesky factorization
8160:    Concepts: Cholesky factorization

8162: .seealso: MatICCFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor()

8164:     Developer Note: fortran interface is not autogenerated as the f90
8165:     interface defintion cannot be generated correctly [due to MatFactorInfo]

8167: @*/
8168: PetscErrorCode MatICCFactor(Mat mat,IS row,const MatFactorInfo *info)
8169: {

8177:   if (mat->rmap->N != mat->cmap->N) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONG,"matrix must be square");
8178:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8179:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8180:   if (!mat->ops->iccfactor) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
8181:   MatCheckPreallocated(mat,1);
8182:   (*mat->ops->iccfactor)(mat,row,info);
8183:   PetscObjectStateIncrease((PetscObject)mat);
8184:   return(0);
8185: }

8189: /*@
8190:    MatSetValuesAdifor - Sets values computed with automatic differentiation into a matrix.

8192:    Not Collective

8194:    Input Parameters:
8195: +  mat - the matrix
8196: .  nl - leading dimension of v
8197: -  v - the values compute with ADIFOR

8199:    Level: developer

8201:    Notes:
8202:      Must call MatSetColoring() before using this routine. Also this matrix must already
8203:      have its nonzero pattern determined.

8205: .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(),
8206:           MatSetValues(), MatSetColoring()
8207: @*/
8208: PetscErrorCode MatSetValuesAdifor(Mat mat,PetscInt nl,void *v)
8209: {


8217:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Matrix must be already assembled");
8218:   PetscLogEventBegin(MAT_SetValues,mat,0,0,0);
8219:   if (!mat->ops->setvaluesadifor) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
8220:   (*mat->ops->setvaluesadifor)(mat,nl,v);
8221:   PetscLogEventEnd(MAT_SetValues,mat,0,0,0);
8222:   PetscObjectStateIncrease((PetscObject)mat);
8223:   return(0);
8224: }

8228: /*@
8229:    MatDiagonalScaleLocal - Scales columns of a matrix given the scaling values including the
8230:          ghosted ones.

8232:    Not Collective

8234:    Input Parameters:
8235: +  mat - the matrix
8236: -  diag = the diagonal values, including ghost ones

8238:    Level: developer

8240:    Notes: Works only for MPIAIJ and MPIBAIJ matrices

8242: .seealso: MatDiagonalScale()
8243: @*/
8244: PetscErrorCode MatDiagonalScaleLocal(Mat mat,Vec diag)
8245: {
8247:   PetscMPIInt    size;


8254:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Matrix must be already assembled");
8255:   PetscLogEventBegin(MAT_Scale,mat,0,0,0);
8256:   MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);
8257:   if (size == 1) {
8258:     PetscInt n,m;
8259:     VecGetSize(diag,&n);
8260:     MatGetSize(mat,0,&m);
8261:     if (m == n) {
8262:       MatDiagonalScale(mat,0,diag);
8263:     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only supported for sequential matrices when no ghost points/periodic conditions");
8264:   } else {
8265:     PetscUseMethod(mat,"MatDiagonalScaleLocal_C",(Mat,Vec),(mat,diag));
8266:   }
8267:   PetscLogEventEnd(MAT_Scale,mat,0,0,0);
8268:   PetscObjectStateIncrease((PetscObject)mat);
8269:   return(0);
8270: }

8274: /*@
8275:    MatGetInertia - Gets the inertia from a factored matrix

8277:    Collective on Mat

8279:    Input Parameter:
8280: .  mat - the matrix

8282:    Output Parameters:
8283: +   nneg - number of negative eigenvalues
8284: .   nzero - number of zero eigenvalues
8285: -   npos - number of positive eigenvalues

8287:    Level: advanced

8289:    Notes: Matrix must have been factored by MatCholeskyFactor()


8292: @*/
8293: PetscErrorCode MatGetInertia(Mat mat,PetscInt *nneg,PetscInt *nzero,PetscInt *npos)
8294: {

8300:   if (!mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
8301:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Numeric factor mat is not assembled");
8302:   if (!mat->ops->getinertia) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
8303:   (*mat->ops->getinertia)(mat,nneg,nzero,npos);
8304:   return(0);
8305: }

8307: /* ----------------------------------------------------------------*/
8310: /*@C
8311:    MatSolves - Solves A x = b, given a factored matrix, for a collection of vectors

8313:    Neighbor-wise Collective on Mat and Vecs

8315:    Input Parameters:
8316: +  mat - the factored matrix
8317: -  b - the right-hand-side vectors

8319:    Output Parameter:
8320: .  x - the result vectors

8322:    Notes:
8323:    The vectors b and x cannot be the same.  I.e., one cannot
8324:    call MatSolves(A,x,x).

8326:    Notes:
8327:    Most users should employ the simplified KSP interface for linear solvers
8328:    instead of working directly with matrix algebra routines such as this.
8329:    See, e.g., KSPCreate().

8331:    Level: developer

8333:    Concepts: matrices^triangular solves

8335: .seealso: MatSolveAdd(), MatSolveTranspose(), MatSolveTransposeAdd(), MatSolve()
8336: @*/
8337: PetscErrorCode MatSolves(Mat mat,Vecs b,Vecs x)
8338: {

8344:   if (x == b) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"x and b must be different vectors");
8345:   if (!mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
8346:   if (!mat->rmap->N && !mat->cmap->N) return(0);

8348:   if (!mat->ops->solves) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
8349:   MatCheckPreallocated(mat,1);
8350:   PetscLogEventBegin(MAT_Solves,mat,0,0,0);
8351:   (*mat->ops->solves)(mat,b,x);
8352:   PetscLogEventEnd(MAT_Solves,mat,0,0,0);
8353:   return(0);
8354: }

8358: /*@
8359:    MatIsSymmetric - Test whether a matrix is symmetric

8361:    Collective on Mat

8363:    Input Parameter:
8364: +  A - the matrix to test
8365: -  tol - difference between value and its transpose less than this amount counts as equal (use 0.0 for exact transpose)

8367:    Output Parameters:
8368: .  flg - the result

8370:    Notes: For real numbers MatIsSymmetric() and MatIsHermitian() return identical results

8372:    Level: intermediate

8374:    Concepts: matrix^symmetry

8376: .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), MatIsSymmetricKnown()
8377: @*/
8378: PetscErrorCode MatIsSymmetric(Mat A,PetscReal tol,PetscBool  *flg)
8379: {


8386:   if (!A->symmetric_set) {
8387:     if (!A->ops->issymmetric) {
8388:       MatType mattype;
8389:       MatGetType(A,&mattype);
8390:       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type <%s> does not support checking for symmetric",mattype);
8391:     }
8392:     (*A->ops->issymmetric)(A,tol,flg);
8393:     if (!tol) {
8394:       A->symmetric_set = PETSC_TRUE;
8395:       A->symmetric     = *flg;
8396:       if (A->symmetric) {
8397:         A->structurally_symmetric_set = PETSC_TRUE;
8398:         A->structurally_symmetric     = PETSC_TRUE;
8399:       }
8400:     }
8401:   } else if (A->symmetric) {
8402:     *flg = PETSC_TRUE;
8403:   } else if (!tol) {
8404:     *flg = PETSC_FALSE;
8405:   } else {
8406:     if (!A->ops->issymmetric) {
8407:       MatType mattype;
8408:       MatGetType(A,&mattype);
8409:       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type <%s> does not support checking for symmetric",mattype);
8410:     }
8411:     (*A->ops->issymmetric)(A,tol,flg);
8412:   }
8413:   return(0);
8414: }

8418: /*@
8419:    MatIsHermitian - Test whether a matrix is Hermitian

8421:    Collective on Mat

8423:    Input Parameter:
8424: +  A - the matrix to test
8425: -  tol - difference between value and its transpose less than this amount counts as equal (use 0.0 for exact Hermitian)

8427:    Output Parameters:
8428: .  flg - the result

8430:    Level: intermediate

8432:    Concepts: matrix^symmetry

8434: .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(),
8435:           MatIsSymmetricKnown(), MatIsSymmetric()
8436: @*/
8437: PetscErrorCode MatIsHermitian(Mat A,PetscReal tol,PetscBool  *flg)
8438: {


8445:   if (!A->hermitian_set) {
8446:     if (!A->ops->ishermitian) {
8447:       MatType mattype;
8448:       MatGetType(A,&mattype);
8449:       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type <%s> does not support checking for hermitian",mattype);
8450:     }
8451:     (*A->ops->ishermitian)(A,tol,flg);
8452:     if (!tol) {
8453:       A->hermitian_set = PETSC_TRUE;
8454:       A->hermitian     = *flg;
8455:       if (A->hermitian) {
8456:         A->structurally_symmetric_set = PETSC_TRUE;
8457:         A->structurally_symmetric     = PETSC_TRUE;
8458:       }
8459:     }
8460:   } else if (A->hermitian) {
8461:     *flg = PETSC_TRUE;
8462:   } else if (!tol) {
8463:     *flg = PETSC_FALSE;
8464:   } else {
8465:     if (!A->ops->ishermitian) {
8466:       MatType mattype;
8467:       MatGetType(A,&mattype);
8468:       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type <%s> does not support checking for hermitian",mattype);
8469:     }
8470:     (*A->ops->ishermitian)(A,tol,flg);
8471:   }
8472:   return(0);
8473: }

8477: /*@
8478:    MatIsSymmetricKnown - Checks the flag on the matrix to see if it is symmetric.

8480:    Not Collective

8482:    Input Parameter:
8483: .  A - the matrix to check

8485:    Output Parameters:
8486: +  set - if the symmetric flag is set (this tells you if the next flag is valid)
8487: -  flg - the result

8489:    Level: advanced

8491:    Concepts: matrix^symmetry

8493:    Note: Does not check the matrix values directly, so this may return unknown (set = PETSC_FALSE). Use MatIsSymmetric()
8494:          if you want it explicitly checked

8496: .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), MatIsSymmetric()
8497: @*/
8498: PetscErrorCode MatIsSymmetricKnown(Mat A,PetscBool  *set,PetscBool  *flg)
8499: {
8504:   if (A->symmetric_set) {
8505:     *set = PETSC_TRUE;
8506:     *flg = A->symmetric;
8507:   } else {
8508:     *set = PETSC_FALSE;
8509:   }
8510:   return(0);
8511: }

8515: /*@
8516:    MatIsHermitianKnown - Checks the flag on the matrix to see if it is hermitian.

8518:    Not Collective

8520:    Input Parameter:
8521: .  A - the matrix to check

8523:    Output Parameters:
8524: +  set - if the hermitian flag is set (this tells you if the next flag is valid)
8525: -  flg - the result

8527:    Level: advanced

8529:    Concepts: matrix^symmetry

8531:    Note: Does not check the matrix values directly, so this may return unknown (set = PETSC_FALSE). Use MatIsHermitian()
8532:          if you want it explicitly checked

8534: .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), MatIsSymmetric()
8535: @*/
8536: PetscErrorCode MatIsHermitianKnown(Mat A,PetscBool  *set,PetscBool  *flg)
8537: {
8542:   if (A->hermitian_set) {
8543:     *set = PETSC_TRUE;
8544:     *flg = A->hermitian;
8545:   } else {
8546:     *set = PETSC_FALSE;
8547:   }
8548:   return(0);
8549: }

8553: /*@
8554:    MatIsStructurallySymmetric - Test whether a matrix is structurally symmetric

8556:    Collective on Mat

8558:    Input Parameter:
8559: .  A - the matrix to test

8561:    Output Parameters:
8562: .  flg - the result

8564:    Level: intermediate

8566:    Concepts: matrix^symmetry

8568: .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsSymmetric(), MatSetOption()
8569: @*/
8570: PetscErrorCode MatIsStructurallySymmetric(Mat A,PetscBool  *flg)
8571: {

8577:   if (!A->structurally_symmetric_set) {
8578:     if (!A->ops->isstructurallysymmetric) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Matrix does not support checking for structural symmetric");
8579:     (*A->ops->isstructurallysymmetric)(A,&A->structurally_symmetric);

8581:     A->structurally_symmetric_set = PETSC_TRUE;
8582:   }
8583:   *flg = A->structurally_symmetric;
8584:   return(0);
8585: }

8589: extern PetscErrorCode MatStashGetInfo_Private(MatStash*,PetscInt*,PetscInt*);
8590: /*@
8591:    MatStashGetInfo - Gets how many values are currently in the matrix stash, i.e. need
8592:        to be communicated to other processors during the MatAssemblyBegin/End() process

8594:     Not collective

8596:    Input Parameter:
8597: .   vec - the vector

8599:    Output Parameters:
8600: +   nstash   - the size of the stash
8601: .   reallocs - the number of additional mallocs incurred.
8602: .   bnstash   - the size of the block stash
8603: -   breallocs - the number of additional mallocs incurred.in the block stash

8605:    Level: advanced

8607: .seealso: MatAssemblyBegin(), MatAssemblyEnd(), Mat, MatStashSetInitialSize()

8609: @*/
8610: PetscErrorCode MatStashGetInfo(Mat mat,PetscInt *nstash,PetscInt *reallocs,PetscInt *bnstash,PetscInt *breallocs)
8611: {

8615:   MatStashGetInfo_Private(&mat->stash,nstash,reallocs);
8616:   MatStashGetInfo_Private(&mat->bstash,bnstash,breallocs);
8617:   return(0);
8618: }

8622: /*@C
8623:    MatCreateVecs - Get vector(s) compatible with the matrix, i.e. with the same
8624:      parallel layout

8626:    Collective on Mat

8628:    Input Parameter:
8629: .  mat - the matrix

8631:    Output Parameter:
8632: +   right - (optional) vector that the matrix can be multiplied against
8633: -   left - (optional) vector that the matrix vector product can be stored in

8635:    Notes:
8636:     The blocksize of the returned vectors is determined by the row and column block sizes set with MatSetBlockSizes() or the single blocksize (same for both) set by MatSetBlockSize().

8638:   Notes: These are new vectors which are not owned by the Mat, they should be destroyed in VecDestroy() when no longer needed

8640:   Level: advanced

8642: .seealso: MatCreate(), VecDestroy()
8643: @*/
8644: PetscErrorCode MatCreateVecs(Mat mat,Vec *right,Vec *left)
8645: {

8651:   if (mat->ops->getvecs) {
8652:     (*mat->ops->getvecs)(mat,right,left);
8653:   } else {
8654:     PetscInt rbs,cbs;
8655:     MatGetBlockSizes(mat,&rbs,&cbs);
8656:     if (right) {
8657:       if (mat->cmap->n < 0) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"PetscLayout for columns not yet setup");
8658:       VecCreate(PetscObjectComm((PetscObject)mat),right);
8659:       VecSetSizes(*right,mat->cmap->n,PETSC_DETERMINE);
8660:       VecSetBlockSize(*right,cbs);
8661:       VecSetType(*right,VECSTANDARD);
8662:       PetscLayoutReference(mat->cmap,&(*right)->map);
8663:     }
8664:     if (left) {
8665:       if (mat->rmap->n < 0) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"PetscLayout for rows not yet setup");
8666:       VecCreate(PetscObjectComm((PetscObject)mat),left);
8667:       VecSetSizes(*left,mat->rmap->n,PETSC_DETERMINE);
8668:       VecSetBlockSize(*left,rbs);
8669:       VecSetType(*left,VECSTANDARD);
8670:       PetscLayoutReference(mat->rmap,&(*left)->map);
8671:     }
8672:   }
8673:   return(0);
8674: }

8678: /*@C
8679:    MatFactorInfoInitialize - Initializes a MatFactorInfo data structure
8680:      with default values.

8682:    Not Collective

8684:    Input Parameters:
8685: .    info - the MatFactorInfo data structure


8688:    Notes: The solvers are generally used through the KSP and PC objects, for example
8689:           PCLU, PCILU, PCCHOLESKY, PCICC

8691:    Level: developer

8693: .seealso: MatFactorInfo

8695:     Developer Note: fortran interface is not autogenerated as the f90
8696:     interface defintion cannot be generated correctly [due to MatFactorInfo]

8698: @*/

8700: PetscErrorCode MatFactorInfoInitialize(MatFactorInfo *info)
8701: {

8705:   PetscMemzero(info,sizeof(MatFactorInfo));
8706:   return(0);
8707: }

8711: /*@
8712:    MatFactorSetSchurIS - Set indices corresponding to the Schur complement

8714:    Collective on Mat

8716:    Input Parameters:
8717: +  mat - the factored matrix
8718: -  is - the index set defining the Schur indices (0-based)

8720:    Notes:

8722:    Level: developer

8724:    Concepts:

8726: .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorRestoreSchurComplement(), MatFactorCreateSchurComplement()

8728: @*/
8729: PetscErrorCode MatFactorSetSchurIS(Mat mat,IS is)
8730: {
8731:   PetscErrorCode ierr,(*f)(Mat,IS);

8739:   if (!mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Only for factored matrix");
8740:   PetscObjectQueryFunction((PetscObject)mat,"MatFactorSetSchurIS_C",&f);
8741:   if (!f) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"The selected MatSolverPackage does not support Schur complement computation. You should use MATSOLVERMUMPS or MATSOLVERMKL_PARDISO");
8742:   (*f)(mat,is);
8743:   return(0);
8744: }

8748: /*@
8749:   MatFactorCreateSchurComplement - Create a Schur complement matrix object using Schur data computed during the factorization step

8751:    Logically Collective on Mat

8753:    Input Parameters:
8754: +  F - the factored matrix obtained by calling MatGetFactor() from PETSc-MUMPS interface
8755: .  *S - location where to return the Schur complement (MATDENSE)

8757:    Notes:
8758:    The routine provides a copy of the Schur data stored within solver's data strutures. The caller must destroy the object when it is no longer needed.
8759:    If MatFactorInvertSchurComplement has been called, the routine gets back the inverse

8761:    Level: advanced

8763:    References:

8765: .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorGetSchurComplement()
8766: @*/
8767: PetscErrorCode MatFactorCreateSchurComplement(Mat F,Mat* S)
8768: {

8773:   PetscUseMethod(F,"MatFactorCreateSchurComplement_C",(Mat,Mat*),(F,S));
8774:   return(0);
8775: }

8779: /*@
8780:   MatFactorGetSchurComplement - Get a Schur complement matrix object using the current Schur data

8782:    Logically Collective on Mat

8784:    Input Parameters:
8785: +  F - the factored matrix obtained by calling MatGetFactor()
8786: .  *S - location where to return the Schur complement (in MATDENSE format)

8788:    Notes:
8789:    Schur complement mode is currently implemented for sequential matrices.
8790:    The routine returns a dense matrix pointing to the raw data of the Schur Complement stored within the data strutures of the solver; e.g. if MatFactorInvertSchurComplement has been called, the returned matrix is actually the inverse of the Schur complement.
8791:    The caller should call MatFactorRestoreSchurComplement when the object is no longer needed.

8793:    Level: advanced

8795:    References:

8797: .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorRestoreSchurComplement(), MatFactorCreateSchurComplement()
8798: @*/
8799: PetscErrorCode MatFactorGetSchurComplement(Mat F,Mat* S)
8800: {

8805:   PetscUseMethod(F,"MatFactorGetSchurComplement_C",(Mat,Mat*),(F,S));
8806:   return(0);
8807: }

8811: /*@
8812:   MatFactorRestoreSchurComplement - Restore the Schur complement matrix object obtained from a call to MatFactorGetSchurComplement

8814:    Logically Collective on Mat

8816:    Input Parameters:
8817: +  F - the factored matrix obtained by calling MatGetFactor()
8818: .  *S - location where the Schur complement is stored

8820:    Notes:

8822:    Level: advanced

8824:    References:

8826: .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorRestoreSchurComplement(), MatFactorCreateSchurComplement()
8827: @*/
8828: PetscErrorCode MatFactorRestoreSchurComplement(Mat F,Mat* S)
8829: {

8835:   MatDestroy(S);
8836:   return(0);
8837: }

8841: /*@
8842:   MatFactorSolveSchurComplementTranspose - Solve the transpose of the Schur complement system computed during the factorization step

8844:    Logically Collective on Mat

8846:    Input Parameters:
8847: +  F - the factored matrix obtained by calling MatGetFactor()
8848: .  rhs - location where the right hand side of the Schur complement system is stored
8849: -  sol - location where the solution of the Schur complement system has to be returned

8851:    Notes:
8852:    The sizes of the vectors should match the size of the Schur complement

8854:    Level: advanced

8856:    References:

8858: .seealso: MatGetFactor(), MatFactorSetSchurIS()
8859: @*/
8860: PetscErrorCode MatFactorSolveSchurComplementTranspose(Mat F, Vec rhs, Vec sol)
8861: {

8870:   PetscUseMethod(F,"MatFactorSolveSchurComplementTranspose_C",(Mat,Vec,Vec),(F,rhs,sol));
8871:   return(0);
8872: }

8876: /*@
8877:   MatFactorSolveSchurComplement - Solve the Schur complement system computed during the factorization step

8879:    Logically Collective on Mat

8881:    Input Parameters:
8882: +  F - the factored matrix obtained by calling MatGetFactor()
8883: .  rhs - location where the right hand side of the Schur complement system is stored
8884: -  sol - location where the solution of the Schur complement system has to be returned

8886:    Notes:
8887:    The sizes of the vectors should match the size of the Schur complement

8889:    Level: advanced

8891:    References:

8893: .seealso: MatGetFactor(), MatFactorSetSchurIS()
8894: @*/
8895: PetscErrorCode MatFactorSolveSchurComplement(Mat F, Vec rhs, Vec sol)
8896: {

8905:   PetscUseMethod(F,"MatFactorSolveSchurComplement_C",(Mat,Vec,Vec),(F,rhs,sol));
8906:   return(0);
8907: }

8911: /*@
8912:   MatFactorInvertSchurComplement - Invert the raw Schur data computed during the factorization step

8914:    Logically Collective on Mat

8916:    Input Parameters:
8917: +  F - the factored matrix obtained by calling MatGetFactor()

8919:    Notes:

8921:    Level: advanced

8923:    References:

8925: .seealso: MatGetFactor(), MatFactorSetSchurIS()
8926: @*/
8927: PetscErrorCode MatFactorInvertSchurComplement(Mat F)
8928: {

8933:   PetscUseMethod(F,"MatFactorInvertSchurComplement_C",(Mat),(F));
8934:   return(0);
8935: }


8940: /*@
8941:    MatPtAP - Creates the matrix product C = P^T * A * P

8943:    Neighbor-wise Collective on Mat

8945:    Input Parameters:
8946: +  A - the matrix
8947: .  P - the projection matrix
8948: .  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
8949: -  fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(P))

8951:    Output Parameters:
8952: .  C - the product matrix

8954:    Notes:
8955:    C will be created and must be destroyed by the user with MatDestroy().

8957:    This routine is currently only implemented for pairs of AIJ matrices and classes
8958:    which inherit from AIJ.

8960:    Level: intermediate

8962: .seealso: MatPtAPSymbolic(), MatPtAPNumeric(), MatMatMult(), MatRARt()
8963: @*/
8964: PetscErrorCode MatPtAP(Mat A,Mat P,MatReuse scall,PetscReal fill,Mat *C)
8965: {
8967:   PetscErrorCode (*fA)(Mat,Mat,MatReuse,PetscReal,Mat*);
8968:   PetscErrorCode (*fP)(Mat,Mat,MatReuse,PetscReal,Mat*);
8969:   PetscErrorCode (*ptap)(Mat,Mat,MatReuse,PetscReal,Mat*)=NULL;
8970:   PetscBool      viatranspose=PETSC_FALSE,viamatmatmatmult=PETSC_FALSE;

8973:   PetscOptionsGetBool(((PetscObject)A)->options,((PetscObject)A)->prefix,"-matptap_viatranspose",&viatranspose,NULL);
8974:   PetscOptionsGetBool(((PetscObject)A)->options,((PetscObject)A)->prefix,"-matptap_viamatmatmatmult",&viamatmatmatmult,NULL);

8978:   MatCheckPreallocated(A,1);
8979:   if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8980:   if (A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8983:   MatCheckPreallocated(P,2);
8984:   if (!P->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8985:   if (P->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");

8987:   if (P->rmap->N!=A->cmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",P->rmap->N,A->cmap->N);
8988:   if (fill < 1.0) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Expected fill=%g must be >= 1.0",(double)fill);

8990:   if (scall == MAT_REUSE_MATRIX) {
8993:     if (viatranspose || viamatmatmatmult) {
8994:       Mat Pt;
8995:       MatTranspose(P,MAT_INITIAL_MATRIX,&Pt);
8996:       if (viamatmatmatmult) {
8997:         MatMatMatMult(Pt,A,P,scall,fill,C);
8998:       } else {
8999:         Mat AP;
9000:         MatMatMult(A,P,MAT_INITIAL_MATRIX,fill,&AP);
9001:         MatMatMult(Pt,AP,scall,fill,C);
9002:         MatDestroy(&AP);
9003:       }
9004:       MatDestroy(&Pt);
9005:     } else {
9006:       PetscLogEventBegin(MAT_PtAP,A,P,0,0);
9007:       PetscLogEventBegin(MAT_PtAPNumeric,A,P,0,0);
9008:       (*(*C)->ops->ptapnumeric)(A,P,*C);
9009:       PetscLogEventEnd(MAT_PtAPNumeric,A,P,0,0);
9010:       PetscLogEventEnd(MAT_PtAP,A,P,0,0);
9011:     }
9012:     return(0);
9013:   }

9015:   if (fill == PETSC_DEFAULT || fill == PETSC_DECIDE) fill = 2.0;
9016:   if (fill < 1.0) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Expected fill=%g must be >= 1.0",(double)fill);

9018:   fA = A->ops->ptap;
9019:   fP = P->ops->ptap;
9020:   if (fP == fA) {
9021:     if (!fA) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"MatPtAP not supported for A of type %s",((PetscObject)A)->type_name);
9022:     ptap = fA;
9023:   } else {
9024:     /* dispatch based on the type of A and P from their PetscObject's PetscFunctionLists. */
9025:     char ptapname[256];
9026:     PetscStrcpy(ptapname,"MatPtAP_");
9027:     PetscStrcat(ptapname,((PetscObject)A)->type_name);
9028:     PetscStrcat(ptapname,"_");
9029:     PetscStrcat(ptapname,((PetscObject)P)->type_name);
9030:     PetscStrcat(ptapname,"_C"); /* e.g., ptapname = "MatPtAP_seqdense_seqaij_C" */
9031:     PetscObjectQueryFunction((PetscObject)P,ptapname,&ptap);
9032:     if (!ptap) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_INCOMP,"MatPtAP requires A, %s, to be compatible with P, %s",((PetscObject)A)->type_name,((PetscObject)P)->type_name);
9033:   }

9035:   if (viatranspose || viamatmatmatmult) {
9036:     Mat Pt;
9037:     MatTranspose(P,MAT_INITIAL_MATRIX,&Pt);
9038:     if (viamatmatmatmult) {
9039:       MatMatMatMult(Pt,A,P,scall,fill,C);
9040:       PetscInfo(*C,"MatPtAP via MatMatMatMult\n");
9041:     } else {
9042:       Mat AP;
9043:       MatMatMult(A,P,MAT_INITIAL_MATRIX,fill,&AP);
9044:       MatMatMult(Pt,AP,scall,fill,C);
9045:       MatDestroy(&AP);
9046:       PetscInfo(*C,"MatPtAP via MatTranspose and MatMatMult\n");
9047:     }
9048:     MatDestroy(&Pt);
9049:   } else {
9050:     PetscLogEventBegin(MAT_PtAP,A,P,0,0);
9051:     (*ptap)(A,P,scall,fill,C);
9052:     PetscLogEventEnd(MAT_PtAP,A,P,0,0);
9053:   }
9054:   return(0);
9055: }

9059: /*@
9060:    MatPtAPNumeric - Computes the matrix product C = P^T * A * P

9062:    Neighbor-wise Collective on Mat

9064:    Input Parameters:
9065: +  A - the matrix
9066: -  P - the projection matrix

9068:    Output Parameters:
9069: .  C - the product matrix

9071:    Notes:
9072:    C must have been created by calling MatPtAPSymbolic and must be destroyed by
9073:    the user using MatDeatroy().

9075:    This routine is currently only implemented for pairs of AIJ matrices and classes
9076:    which inherit from AIJ.  C will be of type MATAIJ.

9078:    Level: intermediate

9080: .seealso: MatPtAP(), MatPtAPSymbolic(), MatMatMultNumeric()
9081: @*/
9082: PetscErrorCode MatPtAPNumeric(Mat A,Mat P,Mat C)
9083: {

9089:   if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
9090:   if (A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
9093:   MatCheckPreallocated(P,2);
9094:   if (!P->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
9095:   if (P->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
9098:   MatCheckPreallocated(C,3);
9099:   if (C->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
9100:   if (P->cmap->N!=C->rmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",P->cmap->N,C->rmap->N);
9101:   if (P->rmap->N!=A->cmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",P->rmap->N,A->cmap->N);
9102:   if (A->rmap->N!=A->cmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Matrix 'A' must be square, %D != %D",A->rmap->N,A->cmap->N);
9103:   if (P->cmap->N!=C->cmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",P->cmap->N,C->cmap->N);
9104:   MatCheckPreallocated(A,1);

9106:   PetscLogEventBegin(MAT_PtAPNumeric,A,P,0,0);
9107:   (*C->ops->ptapnumeric)(A,P,C);
9108:   PetscLogEventEnd(MAT_PtAPNumeric,A,P,0,0);
9109:   return(0);
9110: }

9114: /*@
9115:    MatPtAPSymbolic - Creates the (i,j) structure of the matrix product C = P^T * A * P

9117:    Neighbor-wise Collective on Mat

9119:    Input Parameters:
9120: +  A - the matrix
9121: -  P - the projection matrix

9123:    Output Parameters:
9124: .  C - the (i,j) structure of the product matrix

9126:    Notes:
9127:    C will be created and must be destroyed by the user with MatDestroy().

9129:    This routine is currently only implemented for pairs of SeqAIJ matrices and classes
9130:    which inherit from SeqAIJ.  C will be of type MATSEQAIJ.  The product is computed using
9131:    this (i,j) structure by calling MatPtAPNumeric().

9133:    Level: intermediate

9135: .seealso: MatPtAP(), MatPtAPNumeric(), MatMatMultSymbolic()
9136: @*/
9137: PetscErrorCode MatPtAPSymbolic(Mat A,Mat P,PetscReal fill,Mat *C)
9138: {

9144:   if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
9145:   if (A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
9146:   if (fill <1.0) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Expected fill=%g must be >= 1.0",(double)fill);
9149:   MatCheckPreallocated(P,2);
9150:   if (!P->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
9151:   if (P->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");

9154:   if (P->rmap->N!=A->cmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",P->rmap->N,A->cmap->N);
9155:   if (A->rmap->N!=A->cmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Matrix 'A' must be square, %D != %D",A->rmap->N,A->cmap->N);
9156:   MatCheckPreallocated(A,1);
9157:   PetscLogEventBegin(MAT_PtAPSymbolic,A,P,0,0);
9158:   (*A->ops->ptapsymbolic)(A,P,fill,C);
9159:   PetscLogEventEnd(MAT_PtAPSymbolic,A,P,0,0);

9161:   /* MatSetBlockSize(*C,A->rmap->bs); NO! this is not always true -ma */
9162:   return(0);
9163: }

9167: /*@
9168:    MatRARt - Creates the matrix product C = R * A * R^T

9170:    Neighbor-wise Collective on Mat

9172:    Input Parameters:
9173: +  A - the matrix
9174: .  R - the projection matrix
9175: .  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
9176: -  fill - expected fill as ratio of nnz(C)/nnz(A)

9178:    Output Parameters:
9179: .  C - the product matrix

9181:    Notes:
9182:    C will be created and must be destroyed by the user with MatDestroy().

9184:    This routine is currently only implemented for pairs of AIJ matrices and classes
9185:    which inherit from AIJ.

9187:    Level: intermediate

9189: .seealso: MatRARtSymbolic(), MatRARtNumeric(), MatMatMult(), MatPtAP()
9190: @*/
9191: PetscErrorCode MatRARt(Mat A,Mat R,MatReuse scall,PetscReal fill,Mat *C)
9192: {

9198:   if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
9199:   if (A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
9202:   MatCheckPreallocated(R,2);
9203:   if (!R->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
9204:   if (R->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
9206:   if (R->cmap->N!=A->rmap->N) SETERRQ2(PetscObjectComm((PetscObject)R),PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",R->cmap->N,A->rmap->N);
9207:   if (fill < 1.0) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Expected fill=%g must be >= 1.0",(double)fill);
9208:   MatCheckPreallocated(A,1);

9210:   if (!A->ops->rart) {
9211:     MatType mattype;
9212:     MatGetType(A,&mattype);
9213:     SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Matrix of type <%s> does not support RARt",mattype);
9214:   }
9215:   PetscLogEventBegin(MAT_RARt,A,R,0,0);
9216:   (*A->ops->rart)(A,R,scall,fill,C);
9217:   PetscLogEventEnd(MAT_RARt,A,R,0,0);
9218:   return(0);
9219: }

9223: /*@
9224:    MatRARtNumeric - Computes the matrix product C = R * A * R^T

9226:    Neighbor-wise Collective on Mat

9228:    Input Parameters:
9229: +  A - the matrix
9230: -  R - the projection matrix

9232:    Output Parameters:
9233: .  C - the product matrix

9235:    Notes:
9236:    C must have been created by calling MatRARtSymbolic and must be destroyed by
9237:    the user using MatDestroy().

9239:    This routine is currently only implemented for pairs of AIJ matrices and classes
9240:    which inherit from AIJ.  C will be of type MATAIJ.

9242:    Level: intermediate

9244: .seealso: MatRARt(), MatRARtSymbolic(), MatMatMultNumeric()
9245: @*/
9246: PetscErrorCode MatRARtNumeric(Mat A,Mat R,Mat C)
9247: {

9253:   if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
9254:   if (A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
9257:   MatCheckPreallocated(R,2);
9258:   if (!R->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
9259:   if (R->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
9262:   MatCheckPreallocated(C,3);
9263:   if (C->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
9264:   if (R->rmap->N!=C->rmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",R->rmap->N,C->rmap->N);
9265:   if (R->cmap->N!=A->rmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",R->cmap->N,A->rmap->N);
9266:   if (A->rmap->N!=A->cmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Matrix 'A' must be square, %D != %D",A->rmap->N,A->cmap->N);
9267:   if (R->rmap->N!=C->cmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",R->rmap->N,C->cmap->N);
9268:   MatCheckPreallocated(A,1);

9270:   PetscLogEventBegin(MAT_RARtNumeric,A,R,0,0);
9271:   (*A->ops->rartnumeric)(A,R,C);
9272:   PetscLogEventEnd(MAT_RARtNumeric,A,R,0,0);
9273:   return(0);
9274: }

9278: /*@
9279:    MatRARtSymbolic - Creates the (i,j) structure of the matrix product C = R * A * R^T

9281:    Neighbor-wise Collective on Mat

9283:    Input Parameters:
9284: +  A - the matrix
9285: -  R - the projection matrix

9287:    Output Parameters:
9288: .  C - the (i,j) structure of the product matrix

9290:    Notes:
9291:    C will be created and must be destroyed by the user with MatDestroy().

9293:    This routine is currently only implemented for pairs of SeqAIJ matrices and classes
9294:    which inherit from SeqAIJ.  C will be of type MATSEQAIJ.  The product is computed using
9295:    this (i,j) structure by calling MatRARtNumeric().

9297:    Level: intermediate

9299: .seealso: MatRARt(), MatRARtNumeric(), MatMatMultSymbolic()
9300: @*/
9301: PetscErrorCode MatRARtSymbolic(Mat A,Mat R,PetscReal fill,Mat *C)
9302: {

9308:   if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
9309:   if (A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
9310:   if (fill <1.0) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Expected fill=%g must be >= 1.0",(double)fill);
9313:   MatCheckPreallocated(R,2);
9314:   if (!R->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
9315:   if (R->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");

9318:   if (R->cmap->N!=A->rmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",R->cmap->N,A->rmap->N);
9319:   if (A->rmap->N!=A->cmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Matrix 'A' must be square, %D != %D",A->rmap->N,A->cmap->N);
9320:   MatCheckPreallocated(A,1);
9321:   PetscLogEventBegin(MAT_RARtSymbolic,A,R,0,0);
9322:   (*A->ops->rartsymbolic)(A,R,fill,C);
9323:   PetscLogEventEnd(MAT_RARtSymbolic,A,R,0,0);

9325:   MatSetBlockSizes(*C,PetscAbs(R->rmap->bs),PetscAbs(R->rmap->bs));
9326:   return(0);
9327: }

9331: /*@
9332:    MatMatMult - Performs Matrix-Matrix Multiplication C=A*B.

9334:    Neighbor-wise Collective on Mat

9336:    Input Parameters:
9337: +  A - the left matrix
9338: .  B - the right matrix
9339: .  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
9340: -  fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if you do not have a good estimate
9341:           if the result is a dense matrix this is irrelevent

9343:    Output Parameters:
9344: .  C - the product matrix

9346:    Notes:
9347:    Unless scall is MAT_REUSE_MATRIX C will be created.

9349:    MAT_REUSE_MATRIX can only be used if the matrices A and B have the same nonzero pattern as in the previous call

9351:    To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value
9352:    actually needed.

9354:    If you have many matrices with the same non-zero structure to multiply, you
9355:    should either
9356: $   1) use MAT_REUSE_MATRIX in all calls but the first or
9357: $   2) call MatMatMultSymbolic() once and then MatMatMultNumeric() for each product needed
9358:    In the special case where matrix B (and hence C) are dense you can create the correctly sized matrix C yourself and then call this routine
9359:    with MAT_REUSE_MATRIX, rather than first having MatMatMult() create it for you. You can NEVER do this if the matrix C is sparse.

9361:    Level: intermediate

9363: .seealso: MatMatMultSymbolic(), MatMatMultNumeric(), MatTransposeMatMult(),  MatMatTransposeMult(), MatPtAP()
9364: @*/
9365: PetscErrorCode MatMatMult(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
9366: {
9368:   PetscErrorCode (*fA)(Mat,Mat,MatReuse,PetscReal,Mat*);
9369:   PetscErrorCode (*fB)(Mat,Mat,MatReuse,PetscReal,Mat*);
9370:   PetscErrorCode (*mult)(Mat,Mat,MatReuse,PetscReal,Mat*)=NULL;

9375:   MatCheckPreallocated(A,1);
9376:   if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
9377:   if (A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
9380:   MatCheckPreallocated(B,2);
9381:   if (!B->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
9382:   if (B->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
9384:   if (B->rmap->N!=A->cmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",B->rmap->N,A->cmap->N);
9385:   if (scall == MAT_REUSE_MATRIX) {
9388:     PetscLogEventBegin(MAT_MatMult,A,B,0,0);
9389:     PetscLogEventBegin(MAT_MatMultNumeric,A,B,0,0);
9390:     (*(*C)->ops->matmultnumeric)(A,B,*C);
9391:     PetscLogEventEnd(MAT_MatMultNumeric,A,B,0,0);
9392:     PetscLogEventEnd(MAT_MatMult,A,B,0,0);
9393:     return(0);
9394:   }
9395:   if (fill == PETSC_DEFAULT || fill == PETSC_DECIDE) fill = 2.0;
9396:   if (fill < 1.0) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Expected fill=%g must be >= 1.0",(double)fill);

9398:   fA = A->ops->matmult;
9399:   fB = B->ops->matmult;
9400:   if (fB == fA) {
9401:     if (!fB) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"MatMatMult not supported for B of type %s",((PetscObject)B)->type_name);
9402:     mult = fB;
9403:   } else {
9404:     /* dispatch based on the type of A and B from their PetscObject's PetscFunctionLists. */
9405:     char multname[256];
9406:     PetscStrcpy(multname,"MatMatMult_");
9407:     PetscStrcat(multname,((PetscObject)A)->type_name);
9408:     PetscStrcat(multname,"_");
9409:     PetscStrcat(multname,((PetscObject)B)->type_name);
9410:     PetscStrcat(multname,"_C"); /* e.g., multname = "MatMatMult_seqdense_seqaij_C" */
9411:     PetscObjectQueryFunction((PetscObject)B,multname,&mult);
9412:     if (!mult) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_INCOMP,"MatMatMult requires A, %s, to be compatible with B, %s",((PetscObject)A)->type_name,((PetscObject)B)->type_name);
9413:   }
9414:   PetscLogEventBegin(MAT_MatMult,A,B,0,0);
9415:   (*mult)(A,B,scall,fill,C);
9416:   PetscLogEventEnd(MAT_MatMult,A,B,0,0);
9417:   return(0);
9418: }

9422: /*@
9423:    MatMatMultSymbolic - Performs construction, preallocation, and computes the ij structure
9424:    of the matrix-matrix product C=A*B.  Call this routine before calling MatMatMultNumeric().

9426:    Neighbor-wise Collective on Mat

9428:    Input Parameters:
9429: +  A - the left matrix
9430: .  B - the right matrix
9431: -  fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if you do not have a good estimate,
9432:       if C is a dense matrix this is irrelevent

9434:    Output Parameters:
9435: .  C - the product matrix

9437:    Notes:
9438:    Unless scall is MAT_REUSE_MATRIX C will be created.

9440:    To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value
9441:    actually needed.

9443:    This routine is currently implemented for
9444:     - pairs of AIJ matrices and classes which inherit from AIJ, C will be of type AIJ
9445:     - pairs of AIJ (A) and Dense (B) matrix, C will be of type Dense.
9446:     - pairs of Dense (A) and AIJ (B) matrix, C will be of type Dense.

9448:    Level: intermediate

9450:    Developers Note: There are ways to estimate the number of nonzeros in the resulting product, see for example, http://arxiv.org/abs/1006.4173
9451:      We should incorporate them into PETSc.

9453: .seealso: MatMatMult(), MatMatMultNumeric()
9454: @*/
9455: PetscErrorCode MatMatMultSymbolic(Mat A,Mat B,PetscReal fill,Mat *C)
9456: {
9458:   PetscErrorCode (*Asymbolic)(Mat,Mat,PetscReal,Mat*);
9459:   PetscErrorCode (*Bsymbolic)(Mat,Mat,PetscReal,Mat*);
9460:   PetscErrorCode (*symbolic)(Mat,Mat,PetscReal,Mat*)=NULL;

9465:   if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
9466:   if (A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");

9470:   MatCheckPreallocated(B,2);
9471:   if (!B->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
9472:   if (B->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");

9475:   if (B->rmap->N!=A->cmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",B->rmap->N,A->cmap->N);
9476:   if (fill == PETSC_DEFAULT) fill = 2.0;
9477:   if (fill < 1.0) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Expected fill=%g must be > 1.0",(double)fill);
9478:   MatCheckPreallocated(A,1);

9480:   Asymbolic = A->ops->matmultsymbolic;
9481:   Bsymbolic = B->ops->matmultsymbolic;
9482:   if (Asymbolic == Bsymbolic) {
9483:     if (!Bsymbolic) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"C=A*B not implemented for B of type %s",((PetscObject)B)->type_name);
9484:     symbolic = Bsymbolic;
9485:   } else { /* dispatch based on the type of A and B */
9486:     char symbolicname[256];
9487:     PetscStrcpy(symbolicname,"MatMatMultSymbolic_");
9488:     PetscStrcat(symbolicname,((PetscObject)A)->type_name);
9489:     PetscStrcat(symbolicname,"_");
9490:     PetscStrcat(symbolicname,((PetscObject)B)->type_name);
9491:     PetscStrcat(symbolicname,"_C");
9492:     PetscObjectQueryFunction((PetscObject)B,symbolicname,&symbolic);
9493:     if (!symbolic) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_INCOMP,"MatMatMultSymbolic requires A, %s, to be compatible with B, %s",((PetscObject)A)->type_name,((PetscObject)B)->type_name);
9494:   }
9495:   PetscLogEventBegin(MAT_MatMultSymbolic,A,B,0,0);
9496:   (*symbolic)(A,B,fill,C);
9497:   PetscLogEventEnd(MAT_MatMultSymbolic,A,B,0,0);
9498:   return(0);
9499: }

9503: /*@
9504:    MatMatMultNumeric - Performs the numeric matrix-matrix product.
9505:    Call this routine after first calling MatMatMultSymbolic().

9507:    Neighbor-wise Collective on Mat

9509:    Input Parameters:
9510: +  A - the left matrix
9511: -  B - the right matrix

9513:    Output Parameters:
9514: .  C - the product matrix, which was created by from MatMatMultSymbolic() or a call to MatMatMult().

9516:    Notes:
9517:    C must have been created with MatMatMultSymbolic().

9519:    This routine is currently implemented for
9520:     - pairs of AIJ matrices and classes which inherit from AIJ, C will be of type MATAIJ.
9521:     - pairs of AIJ (A) and Dense (B) matrix, C will be of type Dense.
9522:     - pairs of Dense (A) and AIJ (B) matrix, C will be of type Dense.

9524:    Level: intermediate

9526: .seealso: MatMatMult(), MatMatMultSymbolic()
9527: @*/
9528: PetscErrorCode MatMatMultNumeric(Mat A,Mat B,Mat C)
9529: {

9533:   MatMatMult(A,B,MAT_REUSE_MATRIX,0.0,&C);
9534:   return(0);
9535: }

9539: /*@
9540:    MatMatTransposeMult - Performs Matrix-Matrix Multiplication C=A*B^T.

9542:    Neighbor-wise Collective on Mat

9544:    Input Parameters:
9545: +  A - the left matrix
9546: .  B - the right matrix
9547: .  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
9548: -  fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if not known

9550:    Output Parameters:
9551: .  C - the product matrix

9553:    Notes:
9554:    C will be created if MAT_INITIAL_MATRIX and must be destroyed by the user with MatDestroy().

9556:    MAT_REUSE_MATRIX can only be used if the matrices A and B have the same nonzero pattern as in the previous call

9558:   To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value
9559:    actually needed.

9561:    This routine is currently only implemented for pairs of SeqAIJ matrices.  C will be of type MATSEQAIJ.

9563:    Level: intermediate

9565: .seealso: MatMatTransposeMultSymbolic(), MatMatTransposeMultNumeric(), MatMatMult(), MatTransposeMatMult() MatPtAP()
9566: @*/
9567: PetscErrorCode MatMatTransposeMult(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
9568: {
9570:   PetscErrorCode (*fA)(Mat,Mat,MatReuse,PetscReal,Mat*);
9571:   PetscErrorCode (*fB)(Mat,Mat,MatReuse,PetscReal,Mat*);

9576:   if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
9577:   if (A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
9580:   MatCheckPreallocated(B,2);
9581:   if (!B->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
9582:   if (B->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
9584:   if (B->cmap->N!=A->cmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, AN %D != BN %D",A->cmap->N,B->cmap->N);
9585:   if (fill == PETSC_DEFAULT || fill == PETSC_DECIDE) fill = 2.0;
9586:   if (fill < 1.0) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Expected fill=%g must be > 1.0",(double)fill);
9587:   MatCheckPreallocated(A,1);

9589:   fA = A->ops->mattransposemult;
9590:   if (!fA) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"MatMatTransposeMult not supported for A of type %s",((PetscObject)A)->type_name);
9591:   fB = B->ops->mattransposemult;
9592:   if (!fB) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"MatMatTransposeMult not supported for B of type %s",((PetscObject)B)->type_name);
9593:   if (fB!=fA) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_INCOMP,"MatMatTransposeMult requires A, %s, to be compatible with B, %s",((PetscObject)A)->type_name,((PetscObject)B)->type_name);

9595:   PetscLogEventBegin(MAT_MatTransposeMult,A,B,0,0);
9596:   if (scall == MAT_INITIAL_MATRIX) {
9597:     PetscLogEventBegin(MAT_MatTransposeMultSymbolic,A,B,0,0);
9598:     (*A->ops->mattransposemultsymbolic)(A,B,fill,C);
9599:     PetscLogEventEnd(MAT_MatTransposeMultSymbolic,A,B,0,0);
9600:   }
9601:   PetscLogEventBegin(MAT_MatTransposeMultNumeric,A,B,0,0);
9602:   (*A->ops->mattransposemultnumeric)(A,B,*C);
9603:   PetscLogEventEnd(MAT_MatTransposeMultNumeric,A,B,0,0);
9604:   PetscLogEventEnd(MAT_MatTransposeMult,A,B,0,0);
9605:   return(0);
9606: }

9610: /*@
9611:    MatTransposeMatMult - Performs Matrix-Matrix Multiplication C=A^T*B.

9613:    Neighbor-wise Collective on Mat

9615:    Input Parameters:
9616: +  A - the left matrix
9617: .  B - the right matrix
9618: .  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
9619: -  fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if not known

9621:    Output Parameters:
9622: .  C - the product matrix

9624:    Notes:
9625:    C will be created if MAT_INITIAL_MATRIX and must be destroyed by the user with MatDestroy().

9627:    MAT_REUSE_MATRIX can only be used if the matrices A and B have the same nonzero pattern as in the previous call

9629:   To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value
9630:    actually needed.

9632:    This routine is currently implemented for pairs of AIJ matrices and pairs of SeqDense matrices and classes
9633:    which inherit from SeqAIJ.  C will be of same type as the input matrices.

9635:    Level: intermediate

9637: .seealso: MatTransposeMatMultSymbolic(), MatTransposeMatMultNumeric(), MatMatMult(), MatMatTransposeMult(), MatPtAP()
9638: @*/
9639: PetscErrorCode MatTransposeMatMult(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
9640: {
9642:   PetscErrorCode (*fA)(Mat,Mat,MatReuse,PetscReal,Mat*);
9643:   PetscErrorCode (*fB)(Mat,Mat,MatReuse,PetscReal,Mat*);
9644:   PetscErrorCode (*transposematmult)(Mat,Mat,MatReuse,PetscReal,Mat*) = NULL;

9649:   if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
9650:   if (A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
9653:   MatCheckPreallocated(B,2);
9654:   if (!B->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
9655:   if (B->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
9657:   if (B->rmap->N!=A->rmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",B->rmap->N,A->rmap->N);
9658:   if (fill == PETSC_DEFAULT || fill == PETSC_DECIDE) fill = 2.0;
9659:   if (fill < 1.0) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Expected fill=%g must be > 1.0",(double)fill);
9660:   MatCheckPreallocated(A,1);

9662:   fA = A->ops->transposematmult;
9663:   fB = B->ops->transposematmult;
9664:   if (fB==fA) {
9665:     if (!fA) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"MatTransposeMatMult not supported for A of type %s",((PetscObject)A)->type_name);
9666:     transposematmult = fA;
9667:   } else {
9668:     /* dispatch based on the type of A and B from their PetscObject's PetscFunctionLists. */
9669:     char multname[256];
9670:     PetscStrcpy(multname,"MatTransposeMatMult_");
9671:     PetscStrcat(multname,((PetscObject)A)->type_name);
9672:     PetscStrcat(multname,"_");
9673:     PetscStrcat(multname,((PetscObject)B)->type_name);
9674:     PetscStrcat(multname,"_C"); /* e.g., multname = "MatMatMult_seqdense_seqaij_C" */
9675:     PetscObjectQueryFunction((PetscObject)B,multname,&transposematmult);
9676:     if (!transposematmult) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_INCOMP,"MatTransposeMatMult requires A, %s, to be compatible with B, %s",((PetscObject)A)->type_name,((PetscObject)B)->type_name);
9677:   }
9678:   PetscLogEventBegin(MAT_TransposeMatMult,A,B,0,0);
9679:   (*transposematmult)(A,B,scall,fill,C);
9680:   PetscLogEventEnd(MAT_TransposeMatMult,A,B,0,0);
9681:   return(0);
9682: }

9686: /*@
9687:    MatMatMatMult - Performs Matrix-Matrix-Matrix Multiplication D=A*B*C.

9689:    Neighbor-wise Collective on Mat

9691:    Input Parameters:
9692: +  A - the left matrix
9693: .  B - the middle matrix
9694: .  C - the right matrix
9695: .  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
9696: -  fill - expected fill as ratio of nnz(D)/(nnz(A) + nnz(B)+nnz(C)), use PETSC_DEFAULT if you do not have a good estimate
9697:           if the result is a dense matrix this is irrelevent

9699:    Output Parameters:
9700: .  D - the product matrix

9702:    Notes:
9703:    Unless scall is MAT_REUSE_MATRIX D will be created.

9705:    MAT_REUSE_MATRIX can only be used if the matrices A, B and C have the same nonzero pattern as in the previous call

9707:    To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value
9708:    actually needed.

9710:    If you have many matrices with the same non-zero structure to multiply, you
9711:    should either
9712: $   1) use MAT_REUSE_MATRIX in all calls but the first or
9713: $   2) call MatMatMatMultSymbolic() once and then MatMatMatMultNumeric() for each product needed

9715:    Level: intermediate

9717: .seealso: MatMatMult, MatPtAP()
9718: @*/
9719: PetscErrorCode MatMatMatMult(Mat A,Mat B,Mat C,MatReuse scall,PetscReal fill,Mat *D)
9720: {
9722:   PetscErrorCode (*fA)(Mat,Mat,Mat,MatReuse,PetscReal,Mat*);
9723:   PetscErrorCode (*fB)(Mat,Mat,Mat,MatReuse,PetscReal,Mat*);
9724:   PetscErrorCode (*fC)(Mat,Mat,Mat,MatReuse,PetscReal,Mat*);
9725:   PetscErrorCode (*mult)(Mat,Mat,Mat,MatReuse,PetscReal,Mat*)=NULL;

9730:   MatCheckPreallocated(A,1);
9731:   if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
9732:   if (A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
9735:   MatCheckPreallocated(B,2);
9736:   if (!B->assembled) SETERRQ(PetscObjectComm((PetscObject)B),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
9737:   if (B->factortype) SETERRQ(PetscObjectComm((PetscObject)B),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
9740:   MatCheckPreallocated(C,3);
9741:   if (!C->assembled) SETERRQ(PetscObjectComm((PetscObject)C),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
9742:   if (C->factortype) SETERRQ(PetscObjectComm((PetscObject)C),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
9743:   if (B->rmap->N!=A->cmap->N) SETERRQ2(PetscObjectComm((PetscObject)B),PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",B->rmap->N,A->cmap->N);
9744:   if (C->rmap->N!=B->cmap->N) SETERRQ2(PetscObjectComm((PetscObject)C),PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",C->rmap->N,B->cmap->N);
9745:   if (scall == MAT_REUSE_MATRIX) {
9748:     PetscLogEventBegin(MAT_MatMatMult,A,B,0,0);
9749:     (*(*D)->ops->matmatmult)(A,B,C,scall,fill,D);
9750:     PetscLogEventEnd(MAT_MatMatMult,A,B,0,0);
9751:     return(0);
9752:   }
9753:   if (fill == PETSC_DEFAULT || fill == PETSC_DECIDE) fill = 2.0;
9754:   if (fill < 1.0) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Expected fill=%g must be >= 1.0",(double)fill);

9756:   fA = A->ops->matmatmult;
9757:   fB = B->ops->matmatmult;
9758:   fC = C->ops->matmatmult;
9759:   if (fA == fB && fA == fC) {
9760:     if (!fA) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"MatMatMatMult not supported for A of type %s",((PetscObject)A)->type_name);
9761:     mult = fA;
9762:   } else {
9763:     /* dispatch based on the type of A, B and C from their PetscObject's PetscFunctionLists. */
9764:     char multname[256];
9765:     PetscStrcpy(multname,"MatMatMatMult_");
9766:     PetscStrcat(multname,((PetscObject)A)->type_name);
9767:     PetscStrcat(multname,"_");
9768:     PetscStrcat(multname,((PetscObject)B)->type_name);
9769:     PetscStrcat(multname,"_");
9770:     PetscStrcat(multname,((PetscObject)C)->type_name);
9771:     PetscStrcat(multname,"_C");
9772:     PetscObjectQueryFunction((PetscObject)B,multname,&mult);
9773:     if (!mult) SETERRQ3(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_INCOMP,"MatMatMatMult requires A, %s, to be compatible with B, %s, C, %s",((PetscObject)A)->type_name,((PetscObject)B)->type_name,((PetscObject)C)->type_name);
9774:   }
9775:   PetscLogEventBegin(MAT_MatMatMult,A,B,0,0);
9776:   (*mult)(A,B,C,scall,fill,D);
9777:   PetscLogEventEnd(MAT_MatMatMult,A,B,0,0);
9778:   return(0);
9779: }

9783: /*@C
9784:    MatCreateRedundantMatrix - Create redundant matrices and put them into processors of subcommunicators.

9786:    Collective on Mat

9788:    Input Parameters:
9789: +  mat - the matrix
9790: .  nsubcomm - the number of subcommunicators (= number of redundant parallel or sequential matrices)
9791: .  subcomm - MPI communicator split from the communicator where mat resides in (or MPI_COMM_NULL if nsubcomm is used)
9792: -  reuse - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX

9794:    Output Parameter:
9795: .  matredundant - redundant matrix

9797:    Notes:
9798:    MAT_REUSE_MATRIX can only be used when the nonzero structure of the
9799:    original matrix has not changed from that last call to MatCreateRedundantMatrix().

9801:    This routine creates the duplicated matrices in subcommunicators; you should NOT create them before
9802:    calling it.

9804:    Level: advanced

9806:    Concepts: subcommunicator
9807:    Concepts: duplicate matrix

9809: .seealso: MatDestroy()
9810: @*/
9811: PetscErrorCode MatCreateRedundantMatrix(Mat mat,PetscInt nsubcomm,MPI_Comm subcomm,MatReuse reuse,Mat *matredundant)
9812: {
9814:   MPI_Comm       comm;
9815:   PetscMPIInt    size;
9816:   PetscInt       mloc_sub,rstart,rend,M=mat->rmap->N,N=mat->cmap->N,bs=mat->rmap->bs;
9817:   Mat_Redundant  *redund=NULL;
9818:   PetscSubcomm   psubcomm=NULL;
9819:   MPI_Comm       subcomm_in=subcomm;
9820:   Mat            *matseq;
9821:   IS             isrow,iscol;
9822:   PetscBool      newsubcomm=PETSC_FALSE;

9825:   MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);
9826:   if (size == 1 || nsubcomm == 1) {
9827:     if (reuse == MAT_INITIAL_MATRIX) {
9828:       MatDuplicate(mat,MAT_COPY_VALUES,matredundant);
9829:     } else {
9830:       MatCopy(mat,*matredundant,SAME_NONZERO_PATTERN);
9831:     }
9832:     return(0);
9833:   }

9836:   if (nsubcomm && reuse == MAT_REUSE_MATRIX) {
9839:   }
9840:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
9841:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
9842:   MatCheckPreallocated(mat,1);

9844:   PetscLogEventBegin(MAT_RedundantMat,mat,0,0,0);
9845:   if (subcomm_in == MPI_COMM_NULL && reuse == MAT_INITIAL_MATRIX) { /* get subcomm if user does not provide subcomm */
9846:     /* create psubcomm, then get subcomm */
9847:     PetscObjectGetComm((PetscObject)mat,&comm);
9848:     MPI_Comm_size(comm,&size);
9849:     if (nsubcomm < 1 || nsubcomm > size) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"nsubcomm must between 1 and %D",size);

9851:     PetscSubcommCreate(comm,&psubcomm);