Actual source code: matrix.c

petsc-3.14.3 2021-01-09
Report Typos and Errors
  1: /*
  2:    This is where the abstract matrix operations are defined
  3: */

  5: #include <petsc/private/matimpl.h>
  6: #include <petsc/private/isimpl.h>
  7: #include <petsc/private/vecimpl.h>

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

 15: PetscLogEvent MAT_Mult, MAT_Mults, MAT_MultConstrained, MAT_MultAdd, MAT_MultTranspose;
 16: PetscLogEvent MAT_MultTransposeConstrained, MAT_MultTransposeAdd, MAT_Solve, MAT_Solves, MAT_SolveAdd, MAT_SolveTranspose, MAT_MatSolve,MAT_MatTrSolve;
 17: PetscLogEvent MAT_SolveTransposeAdd, MAT_SOR, MAT_ForwardSolve, MAT_BackwardSolve, MAT_LUFactor, MAT_LUFactorSymbolic;
 18: PetscLogEvent MAT_LUFactorNumeric, MAT_CholeskyFactor, MAT_CholeskyFactorSymbolic, MAT_CholeskyFactorNumeric, MAT_ILUFactor;
 19: PetscLogEvent MAT_ILUFactorSymbolic, MAT_ICCFactorSymbolic, MAT_Copy, MAT_Convert, MAT_Scale, MAT_AssemblyBegin;
 20: PetscLogEvent MAT_AssemblyEnd, MAT_SetValues, MAT_GetValues, MAT_GetRow, MAT_GetRowIJ, MAT_CreateSubMats, MAT_GetOrdering, MAT_RedundantMat, MAT_GetSeqNonzeroStructure;
 21: PetscLogEvent MAT_IncreaseOverlap, MAT_Partitioning, MAT_PartitioningND, MAT_Coarsen, MAT_ZeroEntries, MAT_Load, MAT_View, MAT_AXPY, MAT_FDColoringCreate;
 22: PetscLogEvent MAT_FDColoringSetUp, MAT_FDColoringApply,MAT_Transpose,MAT_FDColoringFunction, MAT_CreateSubMat;
 23: PetscLogEvent MAT_TransposeColoringCreate;
 24: PetscLogEvent MAT_MatMult, MAT_MatMultSymbolic, MAT_MatMultNumeric;
 25: PetscLogEvent MAT_PtAP, MAT_PtAPSymbolic, MAT_PtAPNumeric,MAT_RARt, MAT_RARtSymbolic, MAT_RARtNumeric;
 26: PetscLogEvent MAT_MatTransposeMult, MAT_MatTransposeMultSymbolic, MAT_MatTransposeMultNumeric;
 27: PetscLogEvent MAT_TransposeMatMult, MAT_TransposeMatMultSymbolic, MAT_TransposeMatMultNumeric;
 28: PetscLogEvent MAT_MatMatMult, MAT_MatMatMultSymbolic, MAT_MatMatMultNumeric;
 29: PetscLogEvent MAT_MultHermitianTranspose,MAT_MultHermitianTransposeAdd;
 30: PetscLogEvent MAT_Getsymtranspose, MAT_Getsymtransreduced, MAT_GetBrowsOfAcols;
 31: PetscLogEvent MAT_GetBrowsOfAocols, MAT_Getlocalmat, MAT_Getlocalmatcondensed, MAT_Seqstompi, MAT_Seqstompinum, MAT_Seqstompisym;
 32: PetscLogEvent MAT_Applypapt, MAT_Applypapt_numeric, MAT_Applypapt_symbolic, MAT_GetSequentialNonzeroStructure;
 33: PetscLogEvent MAT_GetMultiProcBlock;
 34: PetscLogEvent MAT_CUSPARSECopyToGPU, MAT_CUSPARSEGenerateTranspose, MAT_SetValuesBatch;
 35: PetscLogEvent MAT_ViennaCLCopyToGPU;
 36: PetscLogEvent MAT_DenseCopyToGPU, MAT_DenseCopyFromGPU;
 37: PetscLogEvent MAT_Merge,MAT_Residual,MAT_SetRandom;
 38: PetscLogEvent MAT_FactorFactS,MAT_FactorInvS;
 39: PetscLogEvent MATCOLORING_Apply,MATCOLORING_Comm,MATCOLORING_Local,MATCOLORING_ISCreate,MATCOLORING_SetUp,MATCOLORING_Weights;

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

 43: /*@
 44:    MatSetRandom - Sets all components of a matrix to random numbers. For sparse matrices that have been preallocated but not been assembled it randomly selects appropriate locations,
 45:                   for sparse matrices that already have locations it fills the locations with random numbers

 47:    Logically Collective on Mat

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

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

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

 64:    Level: intermediate


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


 79:   if (!x->ops->setrandom) SETERRQ1(PetscObjectComm((PetscObject)x),PETSC_ERR_SUP,"Mat type %s",((PetscObject)x)->type_name);

 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(MAT_SetRandom,x,rctx,0,0);
 90:   (*x->ops->setrandom)(x,rctx);
 91:   PetscLogEventEnd(MAT_SetRandom,x,rctx,0,0);

 93:   MatAssemblyBegin(x, MAT_FINAL_ASSEMBLY);
 94:   MatAssemblyEnd(x, MAT_FINAL_ASSEMBLY);
 95:   PetscRandomDestroy(&randObj);
 96:   return(0);
 97: }

 99: /*@
100:    MatFactorGetErrorZeroPivot - returns the pivot value that was determined to be zero and the row it occurred in

102:    Logically Collective on Mat

104:    Input Parameters:
105: .  mat - the factored matrix

107:    Output Parameter:
108: +  pivot - the pivot value computed
109: -  row - the row that the zero pivot occurred. Note that this row must be interpreted carefully due to row reorderings and which processes
110:          the share the matrix

112:    Level: advanced

114:    Notes:
115:     This routine does not work for factorizations done with external packages.

117:     This routine should only be called if MatGetFactorError() returns a value of MAT_FACTOR_NUMERIC_ZEROPIVOT

119:     This can be called on non-factored matrices that come from, for example, matrices used in SOR.

121: .seealso: MatZeroEntries(), MatFactor(), MatGetFactor(), MatLUFactorSymbolic(), MatCholeskyFactorSymbolic(), MatFactorClearError(), MatFactorGetErrorZeroPivot()
122: @*/
123: PetscErrorCode MatFactorGetErrorZeroPivot(Mat mat,PetscReal *pivot,PetscInt *row)
124: {
127:   *pivot = mat->factorerror_zeropivot_value;
128:   *row   = mat->factorerror_zeropivot_row;
129:   return(0);
130: }

132: /*@
133:    MatFactorGetError - gets the error code from a factorization

135:    Logically Collective on Mat

137:    Input Parameters:
138: .  mat - the factored matrix

140:    Output Parameter:
141: .  err  - the error code

143:    Level: advanced

145:    Notes:
146:     This can be called on non-factored matrices that come from, for example, matrices used in SOR.

148: .seealso: MatZeroEntries(), MatFactor(), MatGetFactor(), MatLUFactorSymbolic(), MatCholeskyFactorSymbolic(), MatFactorClearError(), MatFactorGetErrorZeroPivot()
149: @*/
150: PetscErrorCode MatFactorGetError(Mat mat,MatFactorError *err)
151: {
154:   *err = mat->factorerrortype;
155:   return(0);
156: }

158: /*@
159:    MatFactorClearError - clears the error code in a factorization

161:    Logically Collective on Mat

163:    Input Parameter:
164: .  mat - the factored matrix

166:    Level: developer

168:    Notes:
169:     This can be called on non-factored matrices that come from, for example, matrices used in SOR.

171: .seealso: MatZeroEntries(), MatFactor(), MatGetFactor(), MatLUFactorSymbolic(), MatCholeskyFactorSymbolic(), MatFactorGetError(), MatFactorGetErrorZeroPivot()
172: @*/
173: PetscErrorCode MatFactorClearError(Mat mat)
174: {
177:   mat->factorerrortype             = MAT_FACTOR_NOERROR;
178:   mat->factorerror_zeropivot_value = 0.0;
179:   mat->factorerror_zeropivot_row   = 0;
180:   return(0);
181: }

183: PETSC_INTERN PetscErrorCode MatFindNonzeroRowsOrCols_Basic(Mat mat,PetscBool cols,PetscReal tol,IS *nonzero)
184: {
185:   PetscErrorCode    ierr;
186:   Vec               r,l;
187:   const PetscScalar *al;
188:   PetscInt          i,nz,gnz,N,n;

191:   MatCreateVecs(mat,&r,&l);
192:   if (!cols) { /* nonzero rows */
193:     MatGetSize(mat,&N,NULL);
194:     MatGetLocalSize(mat,&n,NULL);
195:     VecSet(l,0.0);
196:     VecSetRandom(r,NULL);
197:     MatMult(mat,r,l);
198:     VecGetArrayRead(l,&al);
199:   } else { /* nonzero columns */
200:     MatGetSize(mat,NULL,&N);
201:     MatGetLocalSize(mat,NULL,&n);
202:     VecSet(r,0.0);
203:     VecSetRandom(l,NULL);
204:     MatMultTranspose(mat,l,r);
205:     VecGetArrayRead(r,&al);
206:   }
207:   if (tol <= 0.0) { for (i=0,nz=0;i<n;i++) if (al[i] != 0.0) nz++; }
208:   else { for (i=0,nz=0;i<n;i++) if (PetscAbsScalar(al[i]) > tol) nz++; }
209:   MPIU_Allreduce(&nz,&gnz,1,MPIU_INT,MPI_SUM,PetscObjectComm((PetscObject)mat));
210:   if (gnz != N) {
211:     PetscInt *nzr;
212:     PetscMalloc1(nz,&nzr);
213:     if (nz) {
214:       if (tol < 0) { for (i=0,nz=0;i<n;i++) if (al[i] != 0.0) nzr[nz++] = i; }
215:       else { for (i=0,nz=0;i<n;i++) if (PetscAbsScalar(al[i]) > tol) nzr[nz++] = i; }
216:     }
217:     ISCreateGeneral(PetscObjectComm((PetscObject)mat),nz,nzr,PETSC_OWN_POINTER,nonzero);
218:   } else *nonzero = NULL;
219:   if (!cols) { /* nonzero rows */
220:     VecRestoreArrayRead(l,&al);
221:   } else {
222:     VecRestoreArrayRead(r,&al);
223:   }
224:   VecDestroy(&l);
225:   VecDestroy(&r);
226:   return(0);
227: }

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

232:   Input Parameter:
233: .    A  - the matrix

235:   Output Parameter:
236: .    keptrows - the rows that are not completely zero

238:   Notes:
239:     keptrows is set to NULL if all rows are nonzero.

241:   Level: intermediate

243:  @*/
244: PetscErrorCode MatFindNonzeroRows(Mat mat,IS *keptrows)
245: {

252:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
253:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
254:   if (!mat->ops->findnonzerorows) {
255:     MatFindNonzeroRowsOrCols_Basic(mat,PETSC_FALSE,0.0,keptrows);
256:   } else {
257:     (*mat->ops->findnonzerorows)(mat,keptrows);
258:   }
259:   return(0);
260: }

262: /*@
263:       MatFindZeroRows - Locate all rows that are completely zero in the matrix

265:   Input Parameter:
266: .    A  - the matrix

268:   Output Parameter:
269: .    zerorows - the rows that are completely zero

271:   Notes:
272:     zerorows is set to NULL if no rows are zero.

274:   Level: intermediate

276:  @*/
277: PetscErrorCode MatFindZeroRows(Mat mat,IS *zerorows)
278: {
280:   IS keptrows;
281:   PetscInt m, n;


286:   MatFindNonzeroRows(mat, &keptrows);
287:   /* MatFindNonzeroRows sets keptrows to NULL if there are no zero rows.
288:      In keeping with this convention, we set zerorows to NULL if there are no zero
289:      rows. */
290:   if (keptrows == NULL) {
291:     *zerorows = NULL;
292:   } else {
293:     MatGetOwnershipRange(mat,&m,&n);
294:     ISComplement(keptrows,m,n,zerorows);
295:     ISDestroy(&keptrows);
296:   }
297:   return(0);
298: }

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

303:    Not Collective

305:    Input Parameters:
306: .   A - the matrix

308:    Output Parameters:
309: .   a - the diagonal part (which is a SEQUENTIAL matrix)

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

316:    Level: advanced

318: @*/
319: PetscErrorCode MatGetDiagonalBlock(Mat A,Mat *a)
320: {

327:   if (A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
328:   if (!A->ops->getdiagonalblock) {
329:     PetscMPIInt size;
330:     MPI_Comm_size(PetscObjectComm((PetscObject)A),&size);
331:     if (size == 1) {
332:       *a = A;
333:       return(0);
334:     } else SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Not coded for matrix type %s",((PetscObject)A)->type_name);
335:   }
336:   (*A->ops->getdiagonalblock)(A,a);
337:   return(0);
338: }

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

343:    Collective on Mat

345:    Input Parameters:
346: .  mat - the matrix

348:    Output Parameter:
349: .   trace - the sum of the diagonal entries

351:    Level: advanced

353: @*/
354: PetscErrorCode MatGetTrace(Mat mat,PetscScalar *trace)
355: {
357:   Vec            diag;

360:   MatCreateVecs(mat,&diag,NULL);
361:   MatGetDiagonal(mat,diag);
362:   VecSum(diag,trace);
363:   VecDestroy(&diag);
364:   return(0);
365: }

367: /*@
368:    MatRealPart - Zeros out the imaginary part of the matrix

370:    Logically Collective on Mat

372:    Input Parameters:
373: .  mat - the matrix

375:    Level: advanced


378: .seealso: MatImaginaryPart()
379: @*/
380: PetscErrorCode MatRealPart(Mat mat)
381: {

387:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
388:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
389:   if (!mat->ops->realpart) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
390:   MatCheckPreallocated(mat,1);
391:   (*mat->ops->realpart)(mat);
392:   return(0);
393: }

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

398:    Collective on Mat

400:    Input Parameter:
401: .  mat - the matrix

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

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

410:    Level: advanced

412: @*/
413: PetscErrorCode MatGetGhosts(Mat mat,PetscInt *nghosts,const PetscInt *ghosts[])
414: {

420:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
421:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
422:   if (!mat->ops->getghosts) {
423:     if (nghosts) *nghosts = 0;
424:     if (ghosts) *ghosts = NULL;
425:   } else {
426:     (*mat->ops->getghosts)(mat,nghosts,ghosts);
427:   }
428:   return(0);
429: }


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

435:    Logically Collective on Mat

437:    Input Parameters:
438: .  mat - the matrix

440:    Level: advanced


443: .seealso: MatRealPart()
444: @*/
445: PetscErrorCode MatImaginaryPart(Mat mat)
446: {

452:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
453:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
454:   if (!mat->ops->imaginarypart) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
455:   MatCheckPreallocated(mat,1);
456:   (*mat->ops->imaginarypart)(mat);
457:   return(0);
458: }

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

463:    Not Collective

465:    Input Parameter:
466: .  mat - the matrix

468:    Output Parameters:
469: +  missing - is any diagonal missing
470: -  dd - first diagonal entry that is missing (optional) on this process

472:    Level: advanced


475: .seealso: MatRealPart()
476: @*/
477: PetscErrorCode MatMissingDiagonal(Mat mat,PetscBool *missing,PetscInt *dd)
478: {

485:   if (!mat->assembled) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix %s",((PetscObject)mat)->type_name);
486:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
487:   if (!mat->ops->missingdiagonal) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
488:   (*mat->ops->missingdiagonal)(mat,missing,dd);
489:   return(0);
490: }

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

497:    Not Collective

499:    Input Parameters:
500: +  mat - the matrix
501: -  row - the row to get

503:    Output Parameters:
504: +  ncols -  if not NULL, the number of nonzeros in the row
505: .  cols - if not NULL, the column numbers
506: -  vals - if not NULL, the values

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

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

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

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

523:    You can only have one call to MatGetRow() outstanding for a particular
524:    matrix at a time, per processor. MatGetRow() can only obtain rows
525:    associated with the given processor, it cannot get rows from the
526:    other processors; for that we suggest using MatCreateSubMatrices(), then
527:    MatGetRow() on the submatrix. The row index passed to MatGetRow()
528:    is in the global number of rows.

530:    Fortran Notes:
531:    The calling sequence from Fortran is
532: .vb
533:    MatGetRow(matrix,row,ncols,cols,values,ierr)
534:          Mat     matrix (input)
535:          integer row    (input)
536:          integer ncols  (output)
537:          integer cols(maxcols) (output)
538:          double precision (or double complex) values(maxcols) output
539: .ve
540:    where maxcols >= maximum nonzeros in any row of the matrix.


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

547:    Level: advanced

549: .seealso: MatRestoreRow(), MatSetValues(), MatGetValues(), MatCreateSubMatrices(), MatGetDiagonal()
550: @*/
551: PetscErrorCode MatGetRow(Mat mat,PetscInt row,PetscInt *ncols,const PetscInt *cols[],const PetscScalar *vals[])
552: {
554:   PetscInt       incols;

559:   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
560:   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
561:   if (!mat->ops->getrow) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
562:   MatCheckPreallocated(mat,1);
563:   PetscLogEventBegin(MAT_GetRow,mat,0,0,0);
564:   (*mat->ops->getrow)(mat,row,&incols,(PetscInt**)cols,(PetscScalar**)vals);
565:   if (ncols) *ncols = incols;
566:   PetscLogEventEnd(MAT_GetRow,mat,0,0,0);
567:   return(0);
568: }

570: /*@
571:    MatConjugate - replaces the matrix values with their complex conjugates

573:    Logically Collective on Mat

575:    Input Parameters:
576: .  mat - the matrix

578:    Level: advanced

580: .seealso:  VecConjugate()
581: @*/
582: PetscErrorCode MatConjugate(Mat mat)
583: {
584: #if defined(PETSC_USE_COMPLEX)

589:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
590:   if (!mat->ops->conjugate) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Not provided for matrix type %s, send email to petsc-maint@mcs.anl.gov",((PetscObject)mat)->type_name);
591:   (*mat->ops->conjugate)(mat);
592: #else
594: #endif
595:   return(0);
596: }

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

601:    Not Collective

603:    Input Parameters:
604: +  mat - the matrix
605: .  row - the row to get
606: .  ncols, cols - the number of nonzeros and their columns
607: -  vals - if nonzero the column values

609:    Notes:
610:    This routine should be called after you have finished examining the entries.

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

616:    Fortran Notes:
617:    The calling sequence from Fortran is
618: .vb
619:    MatRestoreRow(matrix,row,ncols,cols,values,ierr)
620:       Mat     matrix (input)
621:       integer row    (input)
622:       integer ncols  (output)
623:       integer cols(maxcols) (output)
624:       double precision (or double complex) values(maxcols) output
625: .ve
626:    Where maxcols >= maximum nonzeros in any row of the matrix.

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

631:    Level: advanced

633: .seealso:  MatGetRow()
634: @*/
635: PetscErrorCode MatRestoreRow(Mat mat,PetscInt row,PetscInt *ncols,const PetscInt *cols[],const PetscScalar *vals[])
636: {

642:   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
643:   if (!mat->ops->restorerow) return(0);
644:   (*mat->ops->restorerow)(mat,row,ncols,(PetscInt **)cols,(PetscScalar **)vals);
645:   if (ncols) *ncols = 0;
646:   if (cols)  *cols = NULL;
647:   if (vals)  *vals = NULL;
648:   return(0);
649: }

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

655:    Not Collective

657:    Input Parameters:
658: .  mat - the matrix

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

663:    Level: advanced

665: .seealso: MatRestoreRowUpperTriangular()
666: @*/
667: PetscErrorCode MatGetRowUpperTriangular(Mat mat)
668: {

674:   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
675:   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
676:   MatCheckPreallocated(mat,1);
677:   if (!mat->ops->getrowuppertriangular) return(0);
678:   (*mat->ops->getrowuppertriangular)(mat);
679:   return(0);
680: }

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

685:    Not Collective

687:    Input Parameters:
688: .  mat - the matrix

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


694:    Level: advanced

696: .seealso:  MatGetRowUpperTriangular()
697: @*/
698: PetscErrorCode MatRestoreRowUpperTriangular(Mat mat)
699: {

705:   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
706:   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
707:   MatCheckPreallocated(mat,1);
708:   if (!mat->ops->restorerowuppertriangular) return(0);
709:   (*mat->ops->restorerowuppertriangular)(mat);
710:   return(0);
711: }

713: /*@C
714:    MatSetOptionsPrefix - Sets the prefix used for searching for all
715:    Mat options in the database.

717:    Logically Collective on Mat

719:    Input Parameter:
720: +  A - the Mat context
721: -  prefix - the prefix to prepend to all option names

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

727:    Level: advanced

729: .seealso: MatSetFromOptions()
730: @*/
731: PetscErrorCode MatSetOptionsPrefix(Mat A,const char prefix[])
732: {

737:   PetscObjectSetOptionsPrefix((PetscObject)A,prefix);
738:   return(0);
739: }

741: /*@C
742:    MatAppendOptionsPrefix - Appends to the prefix used for searching for all
743:    Mat options in the database.

745:    Logically Collective on Mat

747:    Input Parameters:
748: +  A - the Mat context
749: -  prefix - the prefix to prepend to all option names

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

755:    Level: advanced

757: .seealso: MatGetOptionsPrefix()
758: @*/
759: PetscErrorCode MatAppendOptionsPrefix(Mat A,const char prefix[])
760: {

765:   PetscObjectAppendOptionsPrefix((PetscObject)A,prefix);
766:   return(0);
767: }

769: /*@C
770:    MatGetOptionsPrefix - Gets the prefix used for searching for all
771:    Mat options in the database.

773:    Not Collective

775:    Input Parameter:
776: .  A - the Mat context

778:    Output Parameter:
779: .  prefix - pointer to the prefix string used

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

785:    Level: advanced

787: .seealso: MatAppendOptionsPrefix()
788: @*/
789: PetscErrorCode MatGetOptionsPrefix(Mat A,const char *prefix[])
790: {

795:   PetscObjectGetOptionsPrefix((PetscObject)A,prefix);
796:   return(0);
797: }

799: /*@
800:    MatResetPreallocation - Reset mat to use the original nonzero pattern provided by users.

802:    Collective on Mat

804:    Input Parameters:
805: .  A - the Mat context

807:    Notes:
808:    The allocated memory will be shrunk after calling MatAssembly with MAT_FINAL_ASSEMBLY. Users can reset the preallocation to access the original memory.
809:    Currently support MPIAIJ and SEQAIJ.

811:    Level: beginner

813: .seealso: MatSeqAIJSetPreallocation(), MatMPIAIJSetPreallocation(), MatXAIJSetPreallocation()
814: @*/
815: PetscErrorCode MatResetPreallocation(Mat A)
816: {

822:   PetscUseMethod(A,"MatResetPreallocation_C",(Mat),(A));
823:   return(0);
824: }


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

830:    Collective on Mat

832:    Input Parameters:
833: .  A - the Mat context

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

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

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

842:    Level: beginner

844: .seealso: MatCreate(), MatDestroy()
845: @*/
846: PetscErrorCode MatSetUp(Mat A)
847: {
848:   PetscMPIInt    size;

853:   if (!((PetscObject)A)->type_name) {
854:     MPI_Comm_size(PetscObjectComm((PetscObject)A), &size);
855:     if (size == 1) {
856:       MatSetType(A, MATSEQAIJ);
857:     } else {
858:       MatSetType(A, MATMPIAIJ);
859:     }
860:   }
861:   if (!A->preallocated && A->ops->setup) {
862:     PetscInfo(A,"Warning not preallocating matrix storage\n");
863:     (*A->ops->setup)(A);
864:   }
865:   PetscLayoutSetUp(A->rmap);
866:   PetscLayoutSetUp(A->cmap);
867:   A->preallocated = PETSC_TRUE;
868:   return(0);
869: }

871: #if defined(PETSC_HAVE_SAWS)
872: #include <petscviewersaws.h>
873: #endif

875: /*@C
876:    MatViewFromOptions - View from Options

878:    Collective on Mat

880:    Input Parameters:
881: +  A - the Mat context
882: .  obj - Optional object
883: -  name - command line option

885:    Level: intermediate
886: .seealso:  Mat, MatView, PetscObjectViewFromOptions(), MatCreate()
887: @*/
888: PetscErrorCode  MatViewFromOptions(Mat A,PetscObject obj,const char name[])
889: {

894:   PetscObjectViewFromOptions((PetscObject)A,obj,name);
895:   return(0);
896: }

898: /*@C
899:    MatView - Visualizes a matrix object.

901:    Collective on Mat

903:    Input Parameters:
904: +  mat - the matrix
905: -  viewer - visualization context

907:   Notes:
908:   The available visualization contexts include
909: +    PETSC_VIEWER_STDOUT_SELF - for sequential matrices
910: .    PETSC_VIEWER_STDOUT_WORLD - for parallel matrices created on PETSC_COMM_WORLD
911: .    PETSC_VIEWER_STDOUT_(comm) - for matrices created on MPI communicator comm
912: -     PETSC_VIEWER_DRAW_WORLD - graphical display of nonzero structure

914:    The user can open alternative visualization contexts with
915: +    PetscViewerASCIIOpen() - Outputs matrix to a specified file
916: .    PetscViewerBinaryOpen() - Outputs matrix in binary to a
917:          specified file; corresponding input uses MatLoad()
918: .    PetscViewerDrawOpen() - Outputs nonzero matrix structure to
919:          an X window display
920: -    PetscViewerSocketOpen() - Outputs matrix to Socket viewer.
921:          Currently only the sequential dense and AIJ
922:          matrix types support the Socket viewer.

924:    The user can call PetscViewerPushFormat() to specify the output
925:    format of ASCII printed objects (when using PETSC_VIEWER_STDOUT_SELF,
926:    PETSC_VIEWER_STDOUT_WORLD and PetscViewerASCIIOpen).  Available formats include
927: +    PETSC_VIEWER_DEFAULT - default, prints matrix contents
928: .    PETSC_VIEWER_ASCII_MATLAB - prints matrix contents in Matlab format
929: .    PETSC_VIEWER_ASCII_DENSE - prints entire matrix including zeros
930: .    PETSC_VIEWER_ASCII_COMMON - prints matrix contents, using a sparse
931:          format common among all matrix types
932: .    PETSC_VIEWER_ASCII_IMPL - prints matrix contents, using an implementation-specific
933:          format (which is in many cases the same as the default)
934: .    PETSC_VIEWER_ASCII_INFO - prints basic information about the matrix
935:          size and structure (not the matrix entries)
936: -    PETSC_VIEWER_ASCII_INFO_DETAIL - prints more detailed information about
937:          the matrix structure

939:    Options Database Keys:
940: +  -mat_view ::ascii_info - Prints info on matrix at conclusion of MatAssemblyEnd()
941: .  -mat_view ::ascii_info_detail - Prints more detailed info
942: .  -mat_view - Prints matrix in ASCII format
943: .  -mat_view ::ascii_matlab - Prints matrix in Matlab format
944: .  -mat_view draw - PetscDraws nonzero structure of matrix, using MatView() and PetscDrawOpenX().
945: .  -display <name> - Sets display name (default is host)
946: .  -draw_pause <sec> - Sets number of seconds to pause after display
947: .  -mat_view socket - Sends matrix to socket, can be accessed from Matlab (see Users-Manual: ch_matlab for details)
948: .  -viewer_socket_machine <machine> -
949: .  -viewer_socket_port <port> -
950: .  -mat_view binary - save matrix to file in binary format
951: -  -viewer_binary_filename <name> -
952:    Level: beginner

954:    Notes:
955:     The ASCII viewers are only recommended for small matrices on at most a moderate number of processes,
956:     the program will seemingly hang and take hours for larger matrices, for larger matrices one should use the binary format.

958:     In the debugger you can do "call MatView(mat,0)" to display the matrix. (The same holds for any PETSc object viewer).

960:     See the manual page for MatLoad() for the exact format of the binary file when the binary
961:       viewer is used.

963:       See share/petsc/matlab/PetscBinaryRead.m for a Matlab code that can read in the binary file when the binary
964:       viewer is used and lib/petsc/bin/PetscBinaryIO.py for loading them into Python.

966:       One can use '-mat_view draw -draw_pause -1' to pause the graphical display of matrix nonzero structure,
967:       and then use the following mouse functions.
968: + left mouse: zoom in
969: . middle mouse: zoom out
970: - right mouse: continue with the simulation

972: .seealso: PetscViewerPushFormat(), PetscViewerASCIIOpen(), PetscViewerDrawOpen(),
973:           PetscViewerSocketOpen(), PetscViewerBinaryOpen(), MatLoad()
974: @*/
975: PetscErrorCode MatView(Mat mat,PetscViewer viewer)
976: {
977:   PetscErrorCode    ierr;
978:   PetscInt          rows,cols,rbs,cbs;
979:   PetscBool         isascii,isstring,issaws;
980:   PetscViewerFormat format;
981:   PetscMPIInt       size;

986:   if (!viewer) {PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)mat),&viewer);}
989:   MatCheckPreallocated(mat,1);

991:   PetscViewerGetFormat(viewer,&format);
992:   MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);
993:   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) return(0);

995:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);
996:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii);
997:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);
998:   if ((!isascii || (format != PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL)) && mat->factortype) {
999:     SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"No viewers for factored matrix except ASCII info or info_detail");
1000:   }

1002:   PetscLogEventBegin(MAT_View,mat,viewer,0,0);
1003:   if (isascii) {
1004:     if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ORDER,"Must call MatAssemblyBegin/End() before viewing matrix");
1005:     PetscObjectPrintClassNamePrefixType((PetscObject)mat,viewer);
1006:     if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
1007:       MatNullSpace nullsp,transnullsp;

1009:       PetscViewerASCIIPushTab(viewer);
1010:       MatGetSize(mat,&rows,&cols);
1011:       MatGetBlockSizes(mat,&rbs,&cbs);
1012:       if (rbs != 1 || cbs != 1) {
1013:         if (rbs != cbs) {PetscViewerASCIIPrintf(viewer,"rows=%D, cols=%D, rbs=%D, cbs=%D\n",rows,cols,rbs,cbs);}
1014:         else            {PetscViewerASCIIPrintf(viewer,"rows=%D, cols=%D, bs=%D\n",rows,cols,rbs);}
1015:       } else {
1016:         PetscViewerASCIIPrintf(viewer,"rows=%D, cols=%D\n",rows,cols);
1017:       }
1018:       if (mat->factortype) {
1019:         MatSolverType solver;
1020:         MatFactorGetSolverType(mat,&solver);
1021:         PetscViewerASCIIPrintf(viewer,"package used to perform factorization: %s\n",solver);
1022:       }
1023:       if (mat->ops->getinfo) {
1024:         MatInfo info;
1025:         MatGetInfo(mat,MAT_GLOBAL_SUM,&info);
1026:         PetscViewerASCIIPrintf(viewer,"total: nonzeros=%.f, allocated nonzeros=%.f\n",info.nz_used,info.nz_allocated);
1027:         if (!mat->factortype) {
1028:           PetscViewerASCIIPrintf(viewer,"total number of mallocs used during MatSetValues calls=%D\n",(PetscInt)info.mallocs);
1029:         }
1030:       }
1031:       MatGetNullSpace(mat,&nullsp);
1032:       MatGetTransposeNullSpace(mat,&transnullsp);
1033:       if (nullsp) {PetscViewerASCIIPrintf(viewer,"  has attached null space\n");}
1034:       if (transnullsp && transnullsp != nullsp) {PetscViewerASCIIPrintf(viewer,"  has attached transposed null space\n");}
1035:       MatGetNearNullSpace(mat,&nullsp);
1036:       if (nullsp) {PetscViewerASCIIPrintf(viewer,"  has attached near null space\n");}
1037:       PetscViewerASCIIPushTab(viewer);
1038:       MatProductView(mat,viewer);
1039:       PetscViewerASCIIPopTab(viewer);
1040:     }
1041:   } else if (issaws) {
1042: #if defined(PETSC_HAVE_SAWS)
1043:     PetscMPIInt rank;

1045:     PetscObjectName((PetscObject)mat);
1046:     MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
1047:     if (!((PetscObject)mat)->amsmem && !rank) {
1048:       PetscObjectViewSAWs((PetscObject)mat,viewer);
1049:     }
1050: #endif
1051:   } else if (isstring) {
1052:     const char *type;
1053:     MatGetType(mat,&type);
1054:     PetscViewerStringSPrintf(viewer," MatType: %-7.7s",type);
1055:     if (mat->ops->view) {(*mat->ops->view)(mat,viewer);}
1056:   }
1057:   if ((format == PETSC_VIEWER_NATIVE || format == PETSC_VIEWER_LOAD_BALANCE) && mat->ops->viewnative) {
1058:     PetscViewerASCIIPushTab(viewer);
1059:     (*mat->ops->viewnative)(mat,viewer);
1060:     PetscViewerASCIIPopTab(viewer);
1061:   } else if (mat->ops->view) {
1062:     PetscViewerASCIIPushTab(viewer);
1063:     (*mat->ops->view)(mat,viewer);
1064:     PetscViewerASCIIPopTab(viewer);
1065:   }
1066:   if (isascii) {
1067:     PetscViewerGetFormat(viewer,&format);
1068:     if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
1069:       PetscViewerASCIIPopTab(viewer);
1070:     }
1071:   }
1072:   PetscLogEventEnd(MAT_View,mat,viewer,0,0);
1073:   return(0);
1074: }

1076: #if defined(PETSC_USE_DEBUG)
1077: #include <../src/sys/totalview/tv_data_display.h>
1078: PETSC_UNUSED static int TV_display_type(const struct _p_Mat *mat)
1079: {
1080:   TV_add_row("Local rows", "int", &mat->rmap->n);
1081:   TV_add_row("Local columns", "int", &mat->cmap->n);
1082:   TV_add_row("Global rows", "int", &mat->rmap->N);
1083:   TV_add_row("Global columns", "int", &mat->cmap->N);
1084:   TV_add_row("Typename", TV_ascii_string_type, ((PetscObject)mat)->type_name);
1085:   return TV_format_OK;
1086: }
1087: #endif

1089: /*@C
1090:    MatLoad - Loads a matrix that has been stored in binary/HDF5 format
1091:    with MatView().  The matrix format is determined from the options database.
1092:    Generates a parallel MPI matrix if the communicator has more than one
1093:    processor.  The default matrix type is AIJ.

1095:    Collective on PetscViewer

1097:    Input Parameters:
1098: +  mat - the newly loaded matrix, this needs to have been created with MatCreate()
1099:             or some related function before a call to MatLoad()
1100: -  viewer - binary/HDF5 file viewer

1102:    Options Database Keys:
1103:    Used with block matrix formats (MATSEQBAIJ,  ...) to specify
1104:    block size
1105: .    -matload_block_size <bs>

1107:    Level: beginner

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

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

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

1122:    In parallel, each processor can load a subset of rows (or the
1123:    entire matrix).  This routine is especially useful when a large
1124:    matrix is stored on disk and only part of it is desired on each
1125:    processor.  For example, a parallel solver may access only some of
1126:    the rows from each processor.  The algorithm used here reads
1127:    relatively small blocks of data rather than reading the entire
1128:    matrix and then subsetting it.

1130:    Viewer's PetscViewerType must be either PETSCVIEWERBINARY or PETSCVIEWERHDF5.
1131:    Such viewer can be created using PetscViewerBinaryOpen()/PetscViewerHDF5Open(),
1132:    or the sequence like
1133: $    PetscViewer v;
1134: $    PetscViewerCreate(PETSC_COMM_WORLD,&v);
1135: $    PetscViewerSetType(v,PETSCVIEWERBINARY);
1136: $    PetscViewerSetFromOptions(v);
1137: $    PetscViewerFileSetMode(v,FILE_MODE_READ);
1138: $    PetscViewerFileSetName(v,"datafile");
1139:    The optional PetscViewerSetFromOptions() call allows to override PetscViewerSetType() using option
1140: $ -viewer_type {binary,hdf5}

1142:    See the example src/ksp/ksp/tutorials/ex27.c with the first approach,
1143:    and src/mat/tutorials/ex10.c with the second approach.

1145:    Notes about the PETSc binary format:
1146:    In case of PETSCVIEWERBINARY, a native PETSc binary format is used. Each of the blocks
1147:    is read onto rank 0 and then shipped to its destination rank, one after another.
1148:    Multiple objects, both matrices and vectors, can be stored within the same file.
1149:    Their PetscObject name is ignored; they are loaded in the order of their storage.

1151:    Most users should not need to know the details of the binary storage
1152:    format, since MatLoad() and MatView() completely hide these details.
1153:    But for anyone who's interested, the standard binary matrix storage
1154:    format is

1156: $    PetscInt    MAT_FILE_CLASSID
1157: $    PetscInt    number of rows
1158: $    PetscInt    number of columns
1159: $    PetscInt    total number of nonzeros
1160: $    PetscInt    *number nonzeros in each row
1161: $    PetscInt    *column indices of all nonzeros (starting index is zero)
1162: $    PetscScalar *values of all nonzeros

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

1170:    Notes about the HDF5 (MATLAB MAT-File Version 7.3) format:
1171:    In case of PETSCVIEWERHDF5, a parallel HDF5 reader is used.
1172:    Each processor's chunk is loaded independently by its owning rank.
1173:    Multiple objects, both matrices and vectors, can be stored within the same file.
1174:    They are looked up by their PetscObject name.

1176:    As the MATLAB MAT-File Version 7.3 format is also a HDF5 flavor, we decided to use
1177:    by default the same structure and naming of the AIJ arrays and column count
1178:    within the HDF5 file. This means that a MAT file saved with -v7.3 flag, e.g.
1179: $    save example.mat A b -v7.3
1180:    can be directly read by this routine (see Reference 1 for details).
1181:    Note that depending on your MATLAB version, this format might be a default,
1182:    otherwise you can set it as default in Preferences.

1184:    Unless -nocompression flag is used to save the file in MATLAB,
1185:    PETSc must be configured with ZLIB package.

1187:    See also examples src/mat/tutorials/ex10.c and src/ksp/ksp/tutorials/ex27.c

1189:    Current HDF5 (MAT-File) limitations:
1190:    This reader currently supports only real MATSEQAIJ, MATMPIAIJ, MATSEQDENSE and MATMPIDENSE matrices.

1192:    Corresponding MatView() is not yet implemented.

1194:    The loaded matrix is actually a transpose of the original one in MATLAB,
1195:    unless you push PETSC_VIEWER_HDF5_MAT format (see examples above).
1196:    With this format, matrix is automatically transposed by PETSc,
1197:    unless the matrix is marked as SPD or symmetric
1198:    (see MatSetOption(), MAT_SPD, MAT_SYMMETRIC).

1200:    References:
1201: 1. MATLAB(R) Documentation, manual page of save(), https://www.mathworks.com/help/matlab/ref/save.html#btox10b-1-version

1203: .seealso: PetscViewerBinaryOpen(), PetscViewerSetType(), MatView(), VecLoad()

1205:  @*/
1206: PetscErrorCode MatLoad(Mat mat,PetscViewer viewer)
1207: {
1209:   PetscBool      flg;


1215:   if (!((PetscObject)mat)->type_name) {
1216:     MatSetType(mat,MATAIJ);
1217:   }

1219:   flg  = PETSC_FALSE;
1220:   PetscOptionsGetBool(((PetscObject)mat)->options,((PetscObject)mat)->prefix,"-matload_symmetric",&flg,NULL);
1221:   if (flg) {
1222:     MatSetOption(mat,MAT_SYMMETRIC,PETSC_TRUE);
1223:     MatSetOption(mat,MAT_SYMMETRY_ETERNAL,PETSC_TRUE);
1224:   }
1225:   flg  = PETSC_FALSE;
1226:   PetscOptionsGetBool(((PetscObject)mat)->options,((PetscObject)mat)->prefix,"-matload_spd",&flg,NULL);
1227:   if (flg) {
1228:     MatSetOption(mat,MAT_SPD,PETSC_TRUE);
1229:   }

1231:   if (!mat->ops->load) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"MatLoad is not supported for type %s",((PetscObject)mat)->type_name);
1232:   PetscLogEventBegin(MAT_Load,mat,viewer,0,0);
1233:   (*mat->ops->load)(mat,viewer);
1234:   PetscLogEventEnd(MAT_Load,mat,viewer,0,0);
1235:   return(0);
1236: }

1238: static PetscErrorCode MatDestroy_Redundant(Mat_Redundant **redundant)
1239: {
1241:   Mat_Redundant  *redund = *redundant;
1242:   PetscInt       i;

1245:   if (redund){
1246:     if (redund->matseq) { /* via MatCreateSubMatrices()  */
1247:       ISDestroy(&redund->isrow);
1248:       ISDestroy(&redund->iscol);
1249:       MatDestroySubMatrices(1,&redund->matseq);
1250:     } else {
1251:       PetscFree2(redund->send_rank,redund->recv_rank);
1252:       PetscFree(redund->sbuf_j);
1253:       PetscFree(redund->sbuf_a);
1254:       for (i=0; i<redund->nrecvs; i++) {
1255:         PetscFree(redund->rbuf_j[i]);
1256:         PetscFree(redund->rbuf_a[i]);
1257:       }
1258:       PetscFree4(redund->sbuf_nz,redund->rbuf_nz,redund->rbuf_j,redund->rbuf_a);
1259:     }

1261:     if (redund->subcomm) {
1262:       PetscCommDestroy(&redund->subcomm);
1263:     }
1264:     PetscFree(redund);
1265:   }
1266:   return(0);
1267: }

1269: /*@
1270:    MatDestroy - Frees space taken by a matrix.

1272:    Collective on Mat

1274:    Input Parameter:
1275: .  A - the matrix

1277:    Level: beginner

1279: @*/
1280: PetscErrorCode MatDestroy(Mat *A)
1281: {

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

1289:   /* if memory was published with SAWs then destroy it */
1290:   PetscObjectSAWsViewOff((PetscObject)*A);
1291:   if ((*A)->ops->destroy) {
1292:     (*(*A)->ops->destroy)(*A);
1293:   }

1295:   PetscFree((*A)->defaultvectype);
1296:   PetscFree((*A)->bsizes);
1297:   PetscFree((*A)->solvertype);
1298:   MatDestroy_Redundant(&(*A)->redundant);
1299:   MatProductClear(*A);
1300:   MatNullSpaceDestroy(&(*A)->nullsp);
1301:   MatNullSpaceDestroy(&(*A)->transnullsp);
1302:   MatNullSpaceDestroy(&(*A)->nearnullsp);
1303:   MatDestroy(&(*A)->schur);
1304:   PetscLayoutDestroy(&(*A)->rmap);
1305:   PetscLayoutDestroy(&(*A)->cmap);
1306:   PetscHeaderDestroy(A);
1307:   return(0);
1308: }

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

1315:    Not Collective

1317:    Input Parameters:
1318: +  mat - the matrix
1319: .  v - a logically two-dimensional array of values
1320: .  m, idxm - the number of rows and their global indices
1321: .  n, idxn - the number of columns and their global indices
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:    If you create the matrix yourself (that is not with a call to DMCreateMatrix()) then you MUST call MatXXXXSetPreallocation() or
1328:       MatSetUp() before using this routine

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

1332:    Calls to MatSetValues() with the INSERT_VALUES and ADD_VALUES
1333:    options cannot be mixed without intervening calls to the assembly
1334:    routines.

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

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

1344:    Efficiency Alert:
1345:    The routine MatSetValuesBlocked() may offer much better efficiency
1346:    for users of block sparse formats (MATSEQBAIJ and MATMPIBAIJ).

1348:    Level: beginner

1350:    Developer Notes:
1351:     This is labeled with C so does not automatically generate Fortran stubs and interfaces
1352:                     because it requires multiple Fortran interfaces depending on which arguments are scalar or arrays.

1354: .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(),
1355:           InsertMode, INSERT_VALUES, ADD_VALUES
1356: @*/
1357: PetscErrorCode MatSetValues(Mat mat,PetscInt m,const PetscInt idxm[],PetscInt n,const PetscInt idxn[],const PetscScalar v[],InsertMode addv)
1358: {

1364:   if (!m || !n) return(0); /* no values to insert */
1367:   MatCheckPreallocated(mat,1);

1369:   if (mat->insertmode == NOT_SET_VALUES) {
1370:     mat->insertmode = addv;
1371:   } else if (PetscUnlikely(mat->insertmode != addv)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values");
1372:   if (PetscDefined(USE_DEBUG)) {
1373:     PetscInt       i,j;

1375:     if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1376:     if (!mat->ops->setvalues) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);

1378:     for (i=0; i<m; i++) {
1379:       for (j=0; j<n; j++) {
1380:         if (mat->erroriffailure && PetscIsInfOrNanScalar(v[i*n+j]))
1381: #if defined(PETSC_USE_COMPLEX)
1382:           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]);
1383: #else
1384:           SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_FP,"Inserting %g at matrix entry (%D,%D)",(double)v[i*n+j],idxm[i],idxn[j]);
1385: #endif
1386:       }
1387:     }
1388:   }

1390:   if (mat->assembled) {
1391:     mat->was_assembled = PETSC_TRUE;
1392:     mat->assembled     = PETSC_FALSE;
1393:   }
1394:   PetscLogEventBegin(MAT_SetValues,mat,0,0,0);
1395:   (*mat->ops->setvalues)(mat,m,idxm,n,idxn,v,addv);
1396:   PetscLogEventEnd(MAT_SetValues,mat,0,0,0);
1397:   return(0);
1398: }


1401: /*@
1402:    MatSetValuesRowLocal - Inserts a row (block row for BAIJ matrices) of nonzero
1403:         values into a matrix

1405:    Not Collective

1407:    Input Parameters:
1408: +  mat - the matrix
1409: .  row - the (block) row to set
1410: -  v - a logically two-dimensional array of values

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

1415:    All the nonzeros in the row must be provided

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

1419:    The row must belong to this process

1421:    Level: intermediate

1423: .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(),
1424:           InsertMode, INSERT_VALUES, ADD_VALUES, MatSetValues(), MatSetValuesRow(), MatSetLocalToGlobalMapping()
1425: @*/
1426: PetscErrorCode MatSetValuesRowLocal(Mat mat,PetscInt row,const PetscScalar v[])
1427: {
1429:   PetscInt       globalrow;

1435:   ISLocalToGlobalMappingApply(mat->rmap->mapping,1,&row,&globalrow);
1436:   MatSetValuesRow(mat,globalrow,v);
1437:   return(0);
1438: }

1440: /*@
1441:    MatSetValuesRow - Inserts a row (block row for BAIJ matrices) of nonzero
1442:         values into a matrix

1444:    Not Collective

1446:    Input Parameters:
1447: +  mat - the matrix
1448: .  row - the (block) row to set
1449: -  v - a logically two-dimensional (column major) array of values for  block matrices with blocksize larger than one, otherwise a one dimensional array of values

1451:    Notes:
1452:    The values, v, are column-oriented for the block version.

1454:    All the nonzeros in the row must be provided

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

1458:    The row must belong to this process

1460:    Level: advanced

1462: .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(),
1463:           InsertMode, INSERT_VALUES, ADD_VALUES, MatSetValues()
1464: @*/
1465: PetscErrorCode MatSetValuesRow(Mat mat,PetscInt row,const PetscScalar v[])
1466: {

1472:   MatCheckPreallocated(mat,1);
1474:   if (PetscUnlikely(mat->insertmode == ADD_VALUES)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add and insert values");
1475:   if (PetscUnlikely(mat->factortype)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1476:   mat->insertmode = INSERT_VALUES;

1478:   if (mat->assembled) {
1479:     mat->was_assembled = PETSC_TRUE;
1480:     mat->assembled     = PETSC_FALSE;
1481:   }
1482:   PetscLogEventBegin(MAT_SetValues,mat,0,0,0);
1483:   if (!mat->ops->setvaluesrow) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
1484:   (*mat->ops->setvaluesrow)(mat,row,v);
1485:   PetscLogEventEnd(MAT_SetValues,mat,0,0,0);
1486:   return(0);
1487: }

1489: /*@
1490:    MatSetValuesStencil - Inserts or adds a block of values into a matrix.
1491:      Using structured grid indexing

1493:    Not Collective

1495:    Input Parameters:
1496: +  mat - the matrix
1497: .  m - number of rows being entered
1498: .  idxm - grid coordinates (and component number when dof > 1) for matrix rows being entered
1499: .  n - number of columns being entered
1500: .  idxn - grid coordinates (and component number when dof > 1) for matrix columns being entered
1501: .  v - a logically two-dimensional array of values
1502: -  addv - either ADD_VALUES or INSERT_VALUES, where
1503:    ADD_VALUES adds values to any existing entries, and
1504:    INSERT_VALUES replaces existing entries with new values

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

1509:    Calls to MatSetValuesStencil() with the INSERT_VALUES and ADD_VALUES
1510:    options cannot be mixed without intervening calls to the assembly
1511:    routines.

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

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

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

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

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

1529:    In Fortran idxm and idxn should be declared as
1530: $     MatStencil idxm(4,m),idxn(4,n)
1531:    and the values inserted using
1532: $    idxm(MatStencil_i,1) = i
1533: $    idxm(MatStencil_j,1) = j
1534: $    idxm(MatStencil_k,1) = k
1535: $    idxm(MatStencil_c,1) = c
1536:    etc

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

1543:    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
1544:    a single value per point) you can skip filling those indices.

1546:    Inspired by the structured grid interface to the HYPRE package
1547:    (https://computation.llnl.gov/projects/hypre-scalable-linear-solvers-multigrid-methods)

1549:    Efficiency Alert:
1550:    The routine MatSetValuesBlockedStencil() may offer much better efficiency
1551:    for users of block sparse formats (MATSEQBAIJ and MATMPIBAIJ).

1553:    Level: beginner

1555: .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal()
1556:           MatSetValues(), MatSetValuesBlockedStencil(), MatSetStencil(), DMCreateMatrix(), DMDAVecGetArray(), MatStencil
1557: @*/
1558: PetscErrorCode MatSetValuesStencil(Mat mat,PetscInt m,const MatStencil idxm[],PetscInt n,const MatStencil idxn[],const PetscScalar v[],InsertMode addv)
1559: {
1561:   PetscInt       buf[8192],*bufm=NULL,*bufn=NULL,*jdxm,*jdxn;
1562:   PetscInt       j,i,dim = mat->stencil.dim,*dims = mat->stencil.dims+1,tmp;
1563:   PetscInt       *starts = mat->stencil.starts,*dxm = (PetscInt*)idxm,*dxn = (PetscInt*)idxn,sdim = dim - (1 - (PetscInt)mat->stencil.noc);

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

1572:   if ((m+n) <= (PetscInt)(sizeof(buf)/sizeof(PetscInt))) {
1573:     jdxm = buf; jdxn = buf+m;
1574:   } else {
1575:     PetscMalloc2(m,&bufm,n,&bufn);
1576:     jdxm = bufm; jdxn = bufn;
1577:   }
1578:   for (i=0; i<m; i++) {
1579:     for (j=0; j<3-sdim; j++) dxm++;
1580:     tmp = *dxm++ - starts[0];
1581:     for (j=0; j<dim-1; j++) {
1582:       if ((*dxm++ - starts[j+1]) < 0 || tmp < 0) tmp = -1;
1583:       else                                       tmp = tmp*dims[j] + *(dxm-1) - starts[j+1];
1584:     }
1585:     if (mat->stencil.noc) dxm++;
1586:     jdxm[i] = tmp;
1587:   }
1588:   for (i=0; i<n; i++) {
1589:     for (j=0; j<3-sdim; j++) dxn++;
1590:     tmp = *dxn++ - starts[0];
1591:     for (j=0; j<dim-1; j++) {
1592:       if ((*dxn++ - starts[j+1]) < 0 || tmp < 0) tmp = -1;
1593:       else                                       tmp = tmp*dims[j] + *(dxn-1) - starts[j+1];
1594:     }
1595:     if (mat->stencil.noc) dxn++;
1596:     jdxn[i] = tmp;
1597:   }
1598:   MatSetValuesLocal(mat,m,jdxm,n,jdxn,v,addv);
1599:   PetscFree2(bufm,bufn);
1600:   return(0);
1601: }

1603: /*@
1604:    MatSetValuesBlockedStencil - Inserts or adds a block of values into a matrix.
1605:      Using structured grid indexing

1607:    Not Collective

1609:    Input Parameters:
1610: +  mat - the matrix
1611: .  m - number of rows being entered
1612: .  idxm - grid coordinates for matrix rows being entered
1613: .  n - number of columns being entered
1614: .  idxn - grid coordinates for matrix columns being entered
1615: .  v - a logically two-dimensional array of values
1616: -  addv - either ADD_VALUES or INSERT_VALUES, where
1617:    ADD_VALUES adds values to any existing entries, and
1618:    INSERT_VALUES replaces existing entries with new values

1620:    Notes:
1621:    By default the values, v, are row-oriented and unsorted.
1622:    See MatSetOption() for other options.

1624:    Calls to MatSetValuesBlockedStencil() with the INSERT_VALUES and ADD_VALUES
1625:    options cannot be mixed without intervening calls to the assembly
1626:    routines.

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

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

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

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

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

1644:    In Fortran idxm and idxn should be declared as
1645: $     MatStencil idxm(4,m),idxn(4,n)
1646:    and the values inserted using
1647: $    idxm(MatStencil_i,1) = i
1648: $    idxm(MatStencil_j,1) = j
1649: $    idxm(MatStencil_k,1) = k
1650:    etc

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

1657:    Inspired by the structured grid interface to the HYPRE package
1658:    (https://computation.llnl.gov/projects/hypre-scalable-linear-solvers-multigrid-methods)

1660:    Level: beginner

1662: .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal()
1663:           MatSetValues(), MatSetValuesStencil(), MatSetStencil(), DMCreateMatrix(), DMDAVecGetArray(), MatStencil,
1664:           MatSetBlockSize(), MatSetLocalToGlobalMapping()
1665: @*/
1666: PetscErrorCode MatSetValuesBlockedStencil(Mat mat,PetscInt m,const MatStencil idxm[],PetscInt n,const MatStencil idxn[],const PetscScalar v[],InsertMode addv)
1667: {
1669:   PetscInt       buf[8192],*bufm=NULL,*bufn=NULL,*jdxm,*jdxn;
1670:   PetscInt       j,i,dim = mat->stencil.dim,*dims = mat->stencil.dims+1,tmp;
1671:   PetscInt       *starts = mat->stencil.starts,*dxm = (PetscInt*)idxm,*dxn = (PetscInt*)idxn,sdim = dim - (1 - (PetscInt)mat->stencil.noc);

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

1681:   if ((m+n) <= (PetscInt)(sizeof(buf)/sizeof(PetscInt))) {
1682:     jdxm = buf; jdxn = buf+m;
1683:   } else {
1684:     PetscMalloc2(m,&bufm,n,&bufn);
1685:     jdxm = bufm; jdxn = bufn;
1686:   }
1687:   for (i=0; i<m; i++) {
1688:     for (j=0; j<3-sdim; j++) dxm++;
1689:     tmp = *dxm++ - starts[0];
1690:     for (j=0; j<sdim-1; j++) {
1691:       if ((*dxm++ - starts[j+1]) < 0 || tmp < 0) tmp = -1;
1692:       else                                       tmp = tmp*dims[j] + *(dxm-1) - starts[j+1];
1693:     }
1694:     dxm++;
1695:     jdxm[i] = tmp;
1696:   }
1697:   for (i=0; i<n; i++) {
1698:     for (j=0; j<3-sdim; j++) dxn++;
1699:     tmp = *dxn++ - starts[0];
1700:     for (j=0; j<sdim-1; j++) {
1701:       if ((*dxn++ - starts[j+1]) < 0 || tmp < 0) tmp = -1;
1702:       else                                       tmp = tmp*dims[j] + *(dxn-1) - starts[j+1];
1703:     }
1704:     dxn++;
1705:     jdxn[i] = tmp;
1706:   }
1707:   MatSetValuesBlockedLocal(mat,m,jdxm,n,jdxn,v,addv);
1708:   PetscFree2(bufm,bufn);
1709:   return(0);
1710: }

1712: /*@
1713:    MatSetStencil - Sets the grid information for setting values into a matrix via
1714:         MatSetValuesStencil()

1716:    Not Collective

1718:    Input Parameters:
1719: +  mat - the matrix
1720: .  dim - dimension of the grid 1, 2, or 3
1721: .  dims - number of grid points in x, y, and z direction, including ghost points on your processor
1722: .  starts - starting point of ghost nodes on your processor in x, y, and z direction
1723: -  dof - number of degrees of freedom per node


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

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

1732:    Level: beginner

1734: .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal()
1735:           MatSetValues(), MatSetValuesBlockedStencil(), MatSetValuesStencil()
1736: @*/
1737: PetscErrorCode MatSetStencil(Mat mat,PetscInt dim,const PetscInt dims[],const PetscInt starts[],PetscInt dof)
1738: {
1739:   PetscInt i;


1746:   mat->stencil.dim = dim + (dof > 1);
1747:   for (i=0; i<dim; i++) {
1748:     mat->stencil.dims[i]   = dims[dim-i-1];      /* copy the values in backwards */
1749:     mat->stencil.starts[i] = starts[dim-i-1];
1750:   }
1751:   mat->stencil.dims[dim]   = dof;
1752:   mat->stencil.starts[dim] = 0;
1753:   mat->stencil.noc         = (PetscBool)(dof == 1);
1754:   return(0);
1755: }

1757: /*@C
1758:    MatSetValuesBlocked - Inserts or adds a block of values into a matrix.

1760:    Not Collective

1762:    Input Parameters:
1763: +  mat - the matrix
1764: .  v - a logically two-dimensional array of values
1765: .  m, idxm - the number of block rows and their global block indices
1766: .  n, idxn - the number of block columns and their global block indices
1767: -  addv - either ADD_VALUES or INSERT_VALUES, where
1768:    ADD_VALUES adds values to any existing entries, and
1769:    INSERT_VALUES replaces existing entries with new values

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

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

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

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

1787:    Calls to MatSetValuesBlocked() with the INSERT_VALUES and ADD_VALUES
1788:    options cannot be mixed without intervening calls to the assembly
1789:    routines.

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

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

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

1805:    Example:
1806: $   Suppose m=n=2 and block size(bs) = 2 The array is
1807: $
1808: $   1  2  | 3  4
1809: $   5  6  | 7  8
1810: $   - - - | - - -
1811: $   9  10 | 11 12
1812: $   13 14 | 15 16
1813: $
1814: $   v[] should be passed in like
1815: $   v[] = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
1816: $
1817: $  If you are not using row oriented storage of v (that is you called MatSetOption(mat,MAT_ROW_ORIENTED,PETSC_FALSE)) then
1818: $   v[] = [1,5,9,13,2,6,10,14,3,7,11,15,4,8,12,16]

1820:    Level: intermediate

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

1831:   if (!m || !n) return(0); /* no values to insert */
1835:   MatCheckPreallocated(mat,1);
1836:   if (mat->insertmode == NOT_SET_VALUES) {
1837:     mat->insertmode = addv;
1838:   } else if (PetscUnlikely(mat->insertmode != addv)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values");
1839:   if (PetscDefined(USE_DEBUG)) {
1840:     if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1841:     if (!mat->ops->setvaluesblocked && !mat->ops->setvalues) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
1842:   }

1844:   if (mat->assembled) {
1845:     mat->was_assembled = PETSC_TRUE;
1846:     mat->assembled     = PETSC_FALSE;
1847:   }
1848:   PetscLogEventBegin(MAT_SetValues,mat,0,0,0);
1849:   if (mat->ops->setvaluesblocked) {
1850:     (*mat->ops->setvaluesblocked)(mat,m,idxm,n,idxn,v,addv);
1851:   } else {
1852:     PetscInt buf[8192],*bufr=NULL,*bufc=NULL,*iidxm,*iidxn;
1853:     PetscInt i,j,bs,cbs;
1854:     MatGetBlockSizes(mat,&bs,&cbs);
1855:     if (m*bs+n*cbs <= (PetscInt)(sizeof(buf)/sizeof(PetscInt))) {
1856:       iidxm = buf; iidxn = buf + m*bs;
1857:     } else {
1858:       PetscMalloc2(m*bs,&bufr,n*cbs,&bufc);
1859:       iidxm = bufr; iidxn = bufc;
1860:     }
1861:     for (i=0; i<m; i++) {
1862:       for (j=0; j<bs; j++) {
1863:         iidxm[i*bs+j] = bs*idxm[i] + j;
1864:       }
1865:     }
1866:     for (i=0; i<n; i++) {
1867:       for (j=0; j<cbs; j++) {
1868:         iidxn[i*cbs+j] = cbs*idxn[i] + j;
1869:       }
1870:     }
1871:     MatSetValues(mat,m*bs,iidxm,n*cbs,iidxn,v,addv);
1872:     PetscFree2(bufr,bufc);
1873:   }
1874:   PetscLogEventEnd(MAT_SetValues,mat,0,0,0);
1875:   return(0);
1876: }

1878: /*@C
1879:    MatGetValues - Gets a block of values from a matrix.

1881:    Not Collective; currently only returns a local block

1883:    Input Parameters:
1884: +  mat - the matrix
1885: .  v - a logically two-dimensional array for storing the values
1886: .  m, idxm - the number of rows and their global indices
1887: -  n, idxn - the number of columns and their global indices

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

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

1897:    MatGetValues() requires that the matrix has been assembled
1898:    with MatAssemblyBegin()/MatAssemblyEnd().  Thus, calls to
1899:    MatSetValues() and MatGetValues() CANNOT be made in succession
1900:    without intermediate matrix assembly.

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

1905:    Level: advanced

1907: .seealso: MatGetRow(), MatCreateSubMatrices(), MatSetValues()
1908: @*/
1909: PetscErrorCode MatGetValues(Mat mat,PetscInt m,const PetscInt idxm[],PetscInt n,const PetscInt idxn[],PetscScalar v[])
1910: {

1916:   if (!m || !n) return(0);
1920:   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
1921:   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1922:   if (!mat->ops->getvalues) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
1923:   MatCheckPreallocated(mat,1);

1925:   PetscLogEventBegin(MAT_GetValues,mat,0,0,0);
1926:   (*mat->ops->getvalues)(mat,m,idxm,n,idxn,v);
1927:   PetscLogEventEnd(MAT_GetValues,mat,0,0,0);
1928:   return(0);
1929: }

1931: /*@C
1932:    MatGetValuesLocal - retrieves values into certain locations of a matrix,
1933:    using a local numbering of the nodes.

1935:    Not Collective

1937:    Input Parameters:
1938: +  mat - the matrix
1939: .  nrow, irow - number of rows and their local indices
1940: -  ncol, icol - number of columns and their local indices

1942:    Output Parameter:
1943: .  y -  a logically two-dimensional array of values

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

1948:    Level: advanced

1950:    Developer Notes:
1951:     This is labelled with C so does not automatically generate Fortran stubs and interfaces
1952:                     because it requires multiple Fortran interfaces depending on which arguments are scalar or arrays.

1954: .seealso:  MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetLocalToGlobalMapping(),
1955:            MatSetValuesLocal()
1956: @*/
1957: PetscErrorCode MatGetValuesLocal(Mat mat,PetscInt nrow,const PetscInt irow[],PetscInt ncol,const PetscInt icol[],PetscScalar y[])
1958: {

1964:   MatCheckPreallocated(mat,1);
1965:   if (!nrow || !ncol) return(0); /* no values to retrieve */
1968:   if (PetscDefined(USE_DEBUG)) {
1969:     if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1970:     if (!mat->ops->getvalueslocal && !mat->ops->getvalues) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
1971:   }
1972:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
1973:   PetscLogEventBegin(MAT_GetValues,mat,0,0,0);
1974:   if (mat->ops->getvalueslocal) {
1975:     (*mat->ops->getvalueslocal)(mat,nrow,irow,ncol,icol,y);
1976:   } else {
1977:     PetscInt buf[8192],*bufr=NULL,*bufc=NULL,*irowm,*icolm;
1978:     if ((nrow+ncol) <= (PetscInt)(sizeof(buf)/sizeof(PetscInt))) {
1979:       irowm = buf; icolm = buf+nrow;
1980:     } else {
1981:       PetscMalloc2(nrow,&bufr,ncol,&bufc);
1982:       irowm = bufr; icolm = bufc;
1983:     }
1984:     if (!mat->rmap->mapping) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"MatGetValuesLocal() cannot proceed without local-to-global row mapping (See MatSetLocalToGlobalMapping()).");
1985:     if (!mat->cmap->mapping) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"MatGetValuesLocal() cannot proceed without local-to-global column mapping (See MatSetLocalToGlobalMapping()).");
1986:     ISLocalToGlobalMappingApply(mat->rmap->mapping,nrow,irow,irowm);
1987:     ISLocalToGlobalMappingApply(mat->cmap->mapping,ncol,icol,icolm);
1988:     MatGetValues(mat,nrow,irowm,ncol,icolm,y);
1989:     PetscFree2(bufr,bufc);
1990:   }
1991:   PetscLogEventEnd(MAT_GetValues,mat,0,0,0);
1992:   return(0);
1993: }

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

1999:   Not Collective

2001:   Input Parameters:
2002: + mat - the matrix
2003: . nb - the number of blocks
2004: . bs - the number of rows (and columns) in each block
2005: . rows - a concatenation of the rows for each block
2006: - v - a concatenation of logically two-dimensional arrays of values

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

2011:   Level: advanced

2013: .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(),
2014:           InsertMode, INSERT_VALUES, ADD_VALUES, MatSetValues()
2015: @*/
2016: PetscErrorCode MatSetValuesBatch(Mat mat, PetscInt nb, PetscInt bs, PetscInt rows[], const PetscScalar v[])
2017: {

2025:   if (PetscUnlikelyDebug(mat->factortype)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");

2027:   PetscLogEventBegin(MAT_SetValuesBatch,mat,0,0,0);
2028:   if (mat->ops->setvaluesbatch) {
2029:     (*mat->ops->setvaluesbatch)(mat,nb,bs,rows,v);
2030:   } else {
2031:     PetscInt b;
2032:     for (b = 0; b < nb; ++b) {
2033:       MatSetValues(mat, bs, &rows[b*bs], bs, &rows[b*bs], &v[b*bs*bs], ADD_VALUES);
2034:     }
2035:   }
2036:   PetscLogEventEnd(MAT_SetValuesBatch,mat,0,0,0);
2037:   return(0);
2038: }

2040: /*@
2041:    MatSetLocalToGlobalMapping - Sets a local-to-global numbering for use by
2042:    the routine MatSetValuesLocal() to allow users to insert matrix entries
2043:    using a local (per-processor) numbering.

2045:    Not Collective

2047:    Input Parameters:
2048: +  x - the matrix
2049: .  rmapping - row mapping created with ISLocalToGlobalMappingCreate()   or ISLocalToGlobalMappingCreateIS()
2050: - cmapping - column mapping

2052:    Level: intermediate


2055: .seealso:  MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetValuesLocal()
2056: @*/
2057: PetscErrorCode MatSetLocalToGlobalMapping(Mat x,ISLocalToGlobalMapping rmapping,ISLocalToGlobalMapping cmapping)
2058: {


2067:   if (x->ops->setlocaltoglobalmapping) {
2068:     (*x->ops->setlocaltoglobalmapping)(x,rmapping,cmapping);
2069:   } else {
2070:     PetscLayoutSetISLocalToGlobalMapping(x->rmap,rmapping);
2071:     PetscLayoutSetISLocalToGlobalMapping(x->cmap,cmapping);
2072:   }
2073:   return(0);
2074: }


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

2080:    Not Collective

2082:    Input Parameters:
2083: .  A - the matrix

2085:    Output Parameters:
2086: + rmapping - row mapping
2087: - cmapping - column mapping

2089:    Level: advanced


2092: .seealso:  MatSetValuesLocal()
2093: @*/
2094: PetscErrorCode MatGetLocalToGlobalMapping(Mat A,ISLocalToGlobalMapping *rmapping,ISLocalToGlobalMapping *cmapping)
2095: {
2101:   if (rmapping) *rmapping = A->rmap->mapping;
2102:   if (cmapping) *cmapping = A->cmap->mapping;
2103:   return(0);
2104: }

2106: /*@
2107:    MatSetLayouts - Sets the PetscLayout objects for rows and columns of a matrix

2109:    Logically Collective on A

2111:    Input Parameters:
2112: +  A - the matrix
2113: . rmap - row layout
2114: - cmap - column layout

2116:    Level: advanced

2118: .seealso:  MatCreateVecs(), MatGetLocalToGlobalMapping(), MatGetLayouts()
2119: @*/
2120: PetscErrorCode MatSetLayouts(Mat A,PetscLayout rmap,PetscLayout cmap)
2121: {


2127:   PetscLayoutReference(rmap,&A->rmap);
2128:   PetscLayoutReference(cmap,&A->cmap);
2129:   return(0);
2130: }

2132: /*@
2133:    MatGetLayouts - Gets the PetscLayout objects for rows and columns

2135:    Not Collective

2137:    Input Parameters:
2138: .  A - the matrix

2140:    Output Parameters:
2141: + rmap - row layout
2142: - cmap - column layout

2144:    Level: advanced

2146: .seealso:  MatCreateVecs(), MatGetLocalToGlobalMapping(), MatSetLayouts()
2147: @*/
2148: PetscErrorCode MatGetLayouts(Mat A,PetscLayout *rmap,PetscLayout *cmap)
2149: {
2155:   if (rmap) *rmap = A->rmap;
2156:   if (cmap) *cmap = A->cmap;
2157:   return(0);
2158: }

2160: /*@C
2161:    MatSetValuesLocal - Inserts or adds values into certain locations of a matrix,
2162:    using a local numbering of the nodes.

2164:    Not Collective

2166:    Input Parameters:
2167: +  mat - the matrix
2168: .  nrow, irow - number of rows and their local indices
2169: .  ncol, icol - number of columns and their local indices
2170: .  y -  a logically two-dimensional array of values
2171: -  addv - either INSERT_VALUES or ADD_VALUES, where
2172:    ADD_VALUES adds values to any existing entries, and
2173:    INSERT_VALUES replaces existing entries with new values

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

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

2181:    Calls to MatSetValuesLocal() with the INSERT_VALUES and ADD_VALUES
2182:    options cannot be mixed without intervening calls to the assembly
2183:    routines.

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

2188:    Level: intermediate

2190:    Developer Notes:
2191:     This is labeled with C so does not automatically generate Fortran stubs and interfaces
2192:                     because it requires multiple Fortran interfaces depending on which arguments are scalar or arrays.

2194: .seealso:  MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetLocalToGlobalMapping(),
2195:            MatSetValueLocal(), MatGetValuesLocal()
2196: @*/
2197: PetscErrorCode MatSetValuesLocal(Mat mat,PetscInt nrow,const PetscInt irow[],PetscInt ncol,const PetscInt icol[],const PetscScalar y[],InsertMode addv)
2198: {

2204:   MatCheckPreallocated(mat,1);
2205:   if (!nrow || !ncol) return(0); /* no values to insert */
2208:   if (mat->insertmode == NOT_SET_VALUES) {
2209:     mat->insertmode = addv;
2210:   }
2211:   else if (PetscUnlikely(mat->insertmode != addv)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values");
2212:   if (PetscDefined(USE_DEBUG)) {
2213:     if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2214:     if (!mat->ops->setvalueslocal && !mat->ops->setvalues) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
2215:   }

2217:   if (mat->assembled) {
2218:     mat->was_assembled = PETSC_TRUE;
2219:     mat->assembled     = PETSC_FALSE;
2220:   }
2221:   PetscLogEventBegin(MAT_SetValues,mat,0,0,0);
2222:   if (mat->ops->setvalueslocal) {
2223:     (*mat->ops->setvalueslocal)(mat,nrow,irow,ncol,icol,y,addv);
2224:   } else {
2225:     PetscInt buf[8192],*bufr=NULL,*bufc=NULL,*irowm,*icolm;
2226:     if ((nrow+ncol) <= (PetscInt)(sizeof(buf)/sizeof(PetscInt))) {
2227:       irowm = buf; icolm = buf+nrow;
2228:     } else {
2229:       PetscMalloc2(nrow,&bufr,ncol,&bufc);
2230:       irowm = bufr; icolm = bufc;
2231:     }
2232:     if (!mat->rmap->mapping) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"MatSetValuesLocal() cannot proceed without local-to-global row mapping (See MatSetLocalToGlobalMapping()).");
2233:     if (!mat->cmap->mapping) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"MatSetValuesLocal() cannot proceed without local-to-global column mapping (See MatSetLocalToGlobalMapping()).");
2234:     ISLocalToGlobalMappingApply(mat->rmap->mapping,nrow,irow,irowm);
2235:     ISLocalToGlobalMappingApply(mat->cmap->mapping,ncol,icol,icolm);
2236:     MatSetValues(mat,nrow,irowm,ncol,icolm,y,addv);
2237:     PetscFree2(bufr,bufc);
2238:   }
2239:   PetscLogEventEnd(MAT_SetValues,mat,0,0,0);
2240:   return(0);
2241: }

2243: /*@C
2244:    MatSetValuesBlockedLocal - Inserts or adds values into certain locations of a matrix,
2245:    using a local ordering of the nodes a block at a time.

2247:    Not Collective

2249:    Input Parameters:
2250: +  x - the matrix
2251: .  nrow, irow - number of rows and their local indices
2252: .  ncol, icol - number of columns and their local indices
2253: .  y -  a logically two-dimensional array of values
2254: -  addv - either INSERT_VALUES or ADD_VALUES, where
2255:    ADD_VALUES adds values to any existing entries, and
2256:    INSERT_VALUES replaces existing entries with new values

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

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

2265:    Calls to MatSetValuesBlockedLocal() with the INSERT_VALUES and ADD_VALUES
2266:    options cannot be mixed without intervening calls to the assembly
2267:    routines.

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

2272:    Level: intermediate

2274:    Developer Notes:
2275:     This is labeled with C so does not automatically generate Fortran stubs and interfaces
2276:                     because it requires multiple Fortran interfaces depending on which arguments are scalar or arrays.

2278: .seealso:  MatSetBlockSize(), MatSetLocalToGlobalMapping(), MatAssemblyBegin(), MatAssemblyEnd(),
2279:            MatSetValuesLocal(),  MatSetValuesBlocked()
2280: @*/
2281: PetscErrorCode MatSetValuesBlockedLocal(Mat mat,PetscInt nrow,const PetscInt irow[],PetscInt ncol,const PetscInt icol[],const PetscScalar y[],InsertMode addv)
2282: {

2288:   MatCheckPreallocated(mat,1);
2289:   if (!nrow || !ncol) return(0); /* no values to insert */
2293:   if (mat->insertmode == NOT_SET_VALUES) {
2294:     mat->insertmode = addv;
2295:   } else if (PetscUnlikely(mat->insertmode != addv)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values");
2296:   if (PetscDefined(USE_DEBUG)) {
2297:     if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2298:     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);
2299:   }

2301:   if (mat->assembled) {
2302:     mat->was_assembled = PETSC_TRUE;
2303:     mat->assembled     = PETSC_FALSE;
2304:   }
2305:   if (PetscUnlikelyDebug(mat->rmap->mapping)) { /* Condition on the mapping existing, because MatSetValuesBlockedLocal_IS does not require it to be set. */
2306:     PetscInt irbs, rbs;
2307:     MatGetBlockSizes(mat, &rbs, NULL);
2308:     ISLocalToGlobalMappingGetBlockSize(mat->rmap->mapping,&irbs);
2309:     if (rbs != irbs) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Different row block sizes! mat %D, row l2g map %D",rbs,irbs);
2310:   }
2311:   if (PetscUnlikelyDebug(mat->cmap->mapping)) {
2312:     PetscInt icbs, cbs;
2313:     MatGetBlockSizes(mat,NULL,&cbs);
2314:     ISLocalToGlobalMappingGetBlockSize(mat->cmap->mapping,&icbs);
2315:     if (cbs != icbs) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Different col block sizes! mat %D, col l2g map %D",cbs,icbs);
2316:   }
2317:   PetscLogEventBegin(MAT_SetValues,mat,0,0,0);
2318:   if (mat->ops->setvaluesblockedlocal) {
2319:     (*mat->ops->setvaluesblockedlocal)(mat,nrow,irow,ncol,icol,y,addv);
2320:   } else {
2321:     PetscInt buf[8192],*bufr=NULL,*bufc=NULL,*irowm,*icolm;
2322:     if ((nrow+ncol) <= (PetscInt)(sizeof(buf)/sizeof(PetscInt))) {
2323:       irowm = buf; icolm = buf + nrow;
2324:     } else {
2325:       PetscMalloc2(nrow,&bufr,ncol,&bufc);
2326:       irowm = bufr; icolm = bufc;
2327:     }
2328:     ISLocalToGlobalMappingApplyBlock(mat->rmap->mapping,nrow,irow,irowm);
2329:     ISLocalToGlobalMappingApplyBlock(mat->cmap->mapping,ncol,icol,icolm);
2330:     MatSetValuesBlocked(mat,nrow,irowm,ncol,icolm,y,addv);
2331:     PetscFree2(bufr,bufc);
2332:   }
2333:   PetscLogEventEnd(MAT_SetValues,mat,0,0,0);
2334:   return(0);
2335: }

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

2340:    Collective on Mat

2342:    Input Parameters:
2343: +  mat - the matrix
2344: -  x   - the vector to be multiplied

2346:    Output Parameters:
2347: .  y - the result

2349:    Notes:
2350:    The vectors x and y cannot be the same.  I.e., one cannot
2351:    call MatMult(A,y,y).

2353:    Level: developer

2355: .seealso: MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd()
2356: @*/
2357: PetscErrorCode MatMultDiagonalBlock(Mat mat,Vec x,Vec y)
2358: {


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

2372:   if (!mat->ops->multdiagonalblock) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Matrix type %s does not have a multiply defined",((PetscObject)mat)->type_name);
2373:   (*mat->ops->multdiagonalblock)(mat,x,y);
2374:   PetscObjectStateIncrease((PetscObject)y);
2375:   return(0);
2376: }

2378: /* --------------------------------------------------------*/
2379: /*@
2380:    MatMult - Computes the matrix-vector product, y = Ax.

2382:    Neighbor-wise Collective on Mat

2384:    Input Parameters:
2385: +  mat - the matrix
2386: -  x   - the vector to be multiplied

2388:    Output Parameters:
2389: .  y - the result

2391:    Notes:
2392:    The vectors x and y cannot be the same.  I.e., one cannot
2393:    call MatMult(A,y,y).

2395:    Level: beginner

2397: .seealso: MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd()
2398: @*/
2399: PetscErrorCode MatMult(Mat mat,Vec x,Vec y)
2400: {

2408:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2409:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2410:   if (x == y) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors");
2411: #if !defined(PETSC_HAVE_CONSTRAINTS)
2412:   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);
2413:   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);
2414:   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);
2415: #endif
2416:   VecSetErrorIfLocked(y,3);
2417:   if (mat->erroriffailure) {VecValidValues(x,2,PETSC_TRUE);}
2418:   MatCheckPreallocated(mat,1);

2420:   VecLockReadPush(x);
2421:   if (!mat->ops->mult) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Matrix type %s does not have a multiply defined",((PetscObject)mat)->type_name);
2422:   PetscLogEventBegin(MAT_Mult,mat,x,y,0);
2423:   (*mat->ops->mult)(mat,x,y);
2424:   PetscLogEventEnd(MAT_Mult,mat,x,y,0);
2425:   if (mat->erroriffailure) {VecValidValues(y,3,PETSC_FALSE);}
2426:   VecLockReadPop(x);
2427:   return(0);
2428: }

2430: /*@
2431:    MatMultTranspose - Computes matrix transpose times a vector y = A^T * x.

2433:    Neighbor-wise Collective on Mat

2435:    Input Parameters:
2436: +  mat - the matrix
2437: -  x   - the vector to be multiplied

2439:    Output Parameters:
2440: .  y - the result

2442:    Notes:
2443:    The vectors x and y cannot be the same.  I.e., one cannot
2444:    call MatMultTranspose(A,y,y).

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

2449:    Level: beginner

2451: .seealso: MatMult(), MatMultAdd(), MatMultTransposeAdd(), MatMultHermitianTranspose(), MatTranspose()
2452: @*/
2453: PetscErrorCode MatMultTranspose(Mat mat,Vec x,Vec y)
2454: {
2455:   PetscErrorCode (*op)(Mat,Vec,Vec)=NULL,ierr;


2463:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2464:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2465:   if (x == y) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors");
2466: #if !defined(PETSC_HAVE_CONSTRAINTS)
2467:   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);
2468:   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);
2469: #endif
2470:   if (mat->erroriffailure) {VecValidValues(x,2,PETSC_TRUE);}
2471:   MatCheckPreallocated(mat,1);

2473:   if (!mat->ops->multtranspose) {
2474:     if (mat->symmetric && mat->ops->mult) op = mat->ops->mult;
2475:     if (!op) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Matrix type %s does not have a multiply transpose defined or is symmetric and does not have a multiply defined",((PetscObject)mat)->type_name);
2476:   } else op = mat->ops->multtranspose;
2477:   PetscLogEventBegin(MAT_MultTranspose,mat,x,y,0);
2478:   VecLockReadPush(x);
2479:   (*op)(mat,x,y);
2480:   VecLockReadPop(x);
2481:   PetscLogEventEnd(MAT_MultTranspose,mat,x,y,0);
2482:   PetscObjectStateIncrease((PetscObject)y);
2483:   if (mat->erroriffailure) {VecValidValues(y,3,PETSC_FALSE);}
2484:   return(0);
2485: }

2487: /*@
2488:    MatMultHermitianTranspose - Computes matrix Hermitian transpose times a vector.

2490:    Neighbor-wise Collective on Mat

2492:    Input Parameters:
2493: +  mat - the matrix
2494: -  x   - the vector to be multilplied

2496:    Output Parameters:
2497: .  y - the result

2499:    Notes:
2500:    The vectors x and y cannot be the same.  I.e., one cannot
2501:    call MatMultHermitianTranspose(A,y,y).

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

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

2507:    Level: beginner

2509: .seealso: MatMult(), MatMultAdd(), MatMultHermitianTransposeAdd(), MatMultTranspose()
2510: @*/
2511: PetscErrorCode MatMultHermitianTranspose(Mat mat,Vec x,Vec y)
2512: {


2521:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2522:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2523:   if (x == y) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors");
2524: #if !defined(PETSC_HAVE_CONSTRAINTS)
2525:   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);
2526:   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);
2527: #endif
2528:   MatCheckPreallocated(mat,1);

2530:   PetscLogEventBegin(MAT_MultHermitianTranspose,mat,x,y,0);
2531: #if defined(PETSC_USE_COMPLEX)
2532:   if (mat->ops->multhermitiantranspose || (mat->hermitian && mat->ops->mult)) {
2533:     VecLockReadPush(x);
2534:     if (mat->ops->multhermitiantranspose) {
2535:       (*mat->ops->multhermitiantranspose)(mat,x,y);
2536:     } else {
2537:       (*mat->ops->mult)(mat,x,y);
2538:     }
2539:     VecLockReadPop(x);
2540:   } else {
2541:     Vec w;
2542:     VecDuplicate(x,&w);
2543:     VecCopy(x,w);
2544:     VecConjugate(w);
2545:     MatMultTranspose(mat,w,y);
2546:     VecDestroy(&w);
2547:     VecConjugate(y);
2548:   }
2549:   PetscObjectStateIncrease((PetscObject)y);
2550: #else
2551:   MatMultTranspose(mat,x,y);
2552: #endif
2553:   PetscLogEventEnd(MAT_MultHermitianTranspose,mat,x,y,0);
2554:   return(0);
2555: }

2557: /*@
2558:     MatMultAdd -  Computes v3 = v2 + A * v1.

2560:     Neighbor-wise Collective on Mat

2562:     Input Parameters:
2563: +   mat - the matrix
2564: -   v1, v2 - the vectors

2566:     Output Parameters:
2567: .   v3 - the result

2569:     Notes:
2570:     The vectors v1 and v3 cannot be the same.  I.e., one cannot
2571:     call MatMultAdd(A,v1,v2,v1).

2573:     Level: beginner

2575: .seealso: MatMultTranspose(), MatMult(), MatMultTransposeAdd()
2576: @*/
2577: PetscErrorCode MatMultAdd(Mat mat,Vec v1,Vec v2,Vec v3)
2578: {


2588:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2589:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2590:   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);
2591:   /* 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);
2592:      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); */
2593:   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);
2594:   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);
2595:   if (v1 == v3) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"v1 and v3 must be different vectors");
2596:   MatCheckPreallocated(mat,1);

2598:   if (!mat->ops->multadd) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"No MatMultAdd() for matrix type %s",((PetscObject)mat)->type_name);
2599:   PetscLogEventBegin(MAT_MultAdd,mat,v1,v2,v3);
2600:   VecLockReadPush(v1);
2601:   (*mat->ops->multadd)(mat,v1,v2,v3);
2602:   VecLockReadPop(v1);
2603:   PetscLogEventEnd(MAT_MultAdd,mat,v1,v2,v3);
2604:   PetscObjectStateIncrease((PetscObject)v3);
2605:   return(0);
2606: }

2608: /*@
2609:    MatMultTransposeAdd - Computes v3 = v2 + A' * v1.

2611:    Neighbor-wise Collective on Mat

2613:    Input Parameters:
2614: +  mat - the matrix
2615: -  v1, v2 - the vectors

2617:    Output Parameters:
2618: .  v3 - the result

2620:    Notes:
2621:    The vectors v1 and v3 cannot be the same.  I.e., one cannot
2622:    call MatMultTransposeAdd(A,v1,v2,v1).

2624:    Level: beginner

2626: .seealso: MatMultTranspose(), MatMultAdd(), MatMult()
2627: @*/
2628: PetscErrorCode MatMultTransposeAdd(Mat mat,Vec v1,Vec v2,Vec v3)
2629: {


2639:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2640:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2641:   if (!mat->ops->multtransposeadd) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
2642:   if (v1 == v3) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"v1 and v3 must be different vectors");
2643:   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);
2644:   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);
2645:   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);
2646:   MatCheckPreallocated(mat,1);

2648:   PetscLogEventBegin(MAT_MultTransposeAdd,mat,v1,v2,v3);
2649:   VecLockReadPush(v1);
2650:   (*mat->ops->multtransposeadd)(mat,v1,v2,v3);
2651:   VecLockReadPop(v1);
2652:   PetscLogEventEnd(MAT_MultTransposeAdd,mat,v1,v2,v3);
2653:   PetscObjectStateIncrease((PetscObject)v3);
2654:   return(0);
2655: }

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

2660:    Neighbor-wise Collective on Mat

2662:    Input Parameters:
2663: +  mat - the matrix
2664: -  v1, v2 - the vectors

2666:    Output Parameters:
2667: .  v3 - the result

2669:    Notes:
2670:    The vectors v1 and v3 cannot be the same.  I.e., one cannot
2671:    call MatMultHermitianTransposeAdd(A,v1,v2,v1).

2673:    Level: beginner

2675: .seealso: MatMultHermitianTranspose(), MatMultTranspose(), MatMultAdd(), MatMult()
2676: @*/
2677: PetscErrorCode MatMultHermitianTransposeAdd(Mat mat,Vec v1,Vec v2,Vec v3)
2678: {


2688:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2689:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2690:   if (v1 == v3) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"v1 and v3 must be different vectors");
2691:   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);
2692:   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);
2693:   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);
2694:   MatCheckPreallocated(mat,1);

2696:   PetscLogEventBegin(MAT_MultHermitianTransposeAdd,mat,v1,v2,v3);
2697:   VecLockReadPush(v1);
2698:   if (mat->ops->multhermitiantransposeadd) {
2699:     (*mat->ops->multhermitiantransposeadd)(mat,v1,v2,v3);
2700:   } else {
2701:     Vec w,z;
2702:     VecDuplicate(v1,&w);
2703:     VecCopy(v1,w);
2704:     VecConjugate(w);
2705:     VecDuplicate(v3,&z);
2706:     MatMultTranspose(mat,w,z);
2707:     VecDestroy(&w);
2708:     VecConjugate(z);
2709:     if (v2 != v3) {
2710:       VecWAXPY(v3,1.0,v2,z);
2711:     } else {
2712:       VecAXPY(v3,1.0,z);
2713:     }
2714:     VecDestroy(&z);
2715:   }
2716:   VecLockReadPop(v1);
2717:   PetscLogEventEnd(MAT_MultHermitianTransposeAdd,mat,v1,v2,v3);
2718:   PetscObjectStateIncrease((PetscObject)v3);
2719:   return(0);
2720: }

2722: /*@
2723:    MatMultConstrained - The inner multiplication routine for a
2724:    constrained matrix P^T A P.

2726:    Neighbor-wise Collective on Mat

2728:    Input Parameters:
2729: +  mat - the matrix
2730: -  x   - the vector to be multilplied

2732:    Output Parameters:
2733: .  y - the result

2735:    Notes:
2736:    The vectors x and y cannot be the same.  I.e., one cannot
2737:    call MatMult(A,y,y).

2739:    Level: beginner

2741: .seealso: MatMult(), MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd()
2742: @*/
2743: PetscErrorCode MatMultConstrained(Mat mat,Vec x,Vec y)
2744: {

2751:   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2752:   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2753:   if (x == y) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors");
2754:   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);
2755:   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);
2756:   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);

2758:   PetscLogEventBegin(MAT_MultConstrained,mat,x,y,0);
2759:   VecLockReadPush(x);
2760:   (*mat->ops->multconstrained)(mat,x,y);
2761:   VecLockReadPop(x);
2762:   PetscLogEventEnd(MAT_MultConstrained,mat,x,y,0);
2763:   PetscObjectStateIncrease((PetscObject)y);
2764:   return(0);
2765: }

2767: /*@
2768:    MatMultTransposeConstrained - The inner multiplication routine for a
2769:    constrained matrix P^T A^T P.

2771:    Neighbor-wise Collective on Mat

2773:    Input Parameters:
2774: +  mat - the matrix
2775: -  x   - the vector to be multilplied

2777:    Output Parameters:
2778: .  y - the result

2780:    Notes:
2781:    The vectors x and y cannot be the same.  I.e., one cannot
2782:    call MatMult(A,y,y).

2784:    Level: beginner

2786: .seealso: MatMult(), MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd()
2787: @*/
2788: PetscErrorCode MatMultTransposeConstrained(Mat mat,Vec x,Vec y)
2789: {

2796:   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2797:   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2798:   if (x == y) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors");
2799:   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);
2800:   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);

2802:   PetscLogEventBegin(MAT_MultConstrained,mat,x,y,0);
2803:   (*mat->ops->multtransposeconstrained)(mat,x,y);
2804:   PetscLogEventEnd(MAT_MultConstrained,mat,x,y,0);
2805:   PetscObjectStateIncrease((PetscObject)y);
2806:   return(0);
2807: }

2809: /*@C
2810:    MatGetFactorType - gets the type of factorization it is

2812:    Not Collective

2814:    Input Parameters:
2815: .  mat - the matrix

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

2820:    Level: intermediate

2822: .seealso: MatFactorType, MatGetFactor(), MatSetFactorType()
2823: @*/
2824: PetscErrorCode MatGetFactorType(Mat mat,MatFactorType *t)
2825: {
2830:   *t = mat->factortype;
2831:   return(0);
2832: }

2834: /*@C
2835:    MatSetFactorType - sets the type of factorization it is

2837:    Logically Collective on Mat

2839:    Input Parameters:
2840: +  mat - the matrix
2841: -  t - the type, one of MAT_FACTOR_NONE, MAT_FACTOR_LU, MAT_FACTOR_CHOLESKY, MAT_FACTOR_ILU, MAT_FACTOR_ICC,MAT_FACTOR_ILUDT

2843:    Level: intermediate

2845: .seealso: MatFactorType, MatGetFactor(), MatGetFactorType()
2846: @*/
2847: PetscErrorCode MatSetFactorType(Mat mat, MatFactorType t)
2848: {
2852:   mat->factortype = t;
2853:   return(0);
2854: }

2856: /* ------------------------------------------------------------*/
2857: /*@C
2858:    MatGetInfo - Returns information about matrix storage (number of
2859:    nonzeros, memory, etc.).

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

2863:    Input Parameters:
2864: .  mat - the matrix

2866:    Output Parameters:
2867: +  flag - flag indicating the type of parameters to be returned
2868:    (MAT_LOCAL - local matrix, MAT_GLOBAL_MAX - maximum over all processors,
2869:    MAT_GLOBAL_SUM - sum over all processors)
2870: -  info - matrix information context

2872:    Notes:
2873:    The MatInfo context contains a variety of matrix data, including
2874:    number of nonzeros allocated and used, number of mallocs during
2875:    matrix assembly, etc.  Additional information for factored matrices
2876:    is provided (such as the fill ratio, number of mallocs during
2877:    factorization, etc.).  Much of this info is printed to PETSC_STDOUT
2878:    when using the runtime options
2879: $       -info -mat_view ::ascii_info

2881:    Example for C/C++ Users:
2882:    See the file ${PETSC_DIR}/include/petscmat.h for a complete list of
2883:    data within the MatInfo context.  For example,
2884: .vb
2885:       MatInfo info;
2886:       Mat     A;
2887:       double  mal, nz_a, nz_u;

2889:       MatGetInfo(A,MAT_LOCAL,&info);
2890:       mal  = info.mallocs;
2891:       nz_a = info.nz_allocated;
2892: .ve

2894:    Example for Fortran Users:
2895:    Fortran users should declare info as a double precision
2896:    array of dimension MAT_INFO_SIZE, and then extract the parameters
2897:    of interest.  See the file ${PETSC_DIR}/include/petsc/finclude/petscmat.h
2898:    a complete list of parameter names.
2899: .vb
2900:       double  precision info(MAT_INFO_SIZE)
2901:       double  precision mal, nz_a
2902:       Mat     A
2903:       integer ierr

2905:       call MatGetInfo(A,MAT_LOCAL,info,ierr)
2906:       mal = info(MAT_INFO_MALLOCS)
2907:       nz_a = info(MAT_INFO_NZ_ALLOCATED)
2908: .ve

2910:     Level: intermediate

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

2915: .seealso: MatStashGetInfo()

2917: @*/
2918: PetscErrorCode MatGetInfo(Mat mat,MatInfoType flag,MatInfo *info)
2919: {

2926:   if (!mat->ops->getinfo) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
2927:   MatCheckPreallocated(mat,1);
2928:   (*mat->ops->getinfo)(mat,flag,info);
2929:   return(0);
2930: }

2932: /*
2933:    This is used by external packages where it is not easy to get the info from the actual
2934:    matrix factorization.
2935: */
2936: PetscErrorCode MatGetInfo_External(Mat A,MatInfoType flag,MatInfo *info)
2937: {

2941:   PetscMemzero(info,sizeof(MatInfo));
2942:   return(0);
2943: }

2945: /* ----------------------------------------------------------*/

2947: /*@C
2948:    MatLUFactor - Performs in-place LU factorization of matrix.

2950:    Collective on Mat

2952:    Input Parameters:
2953: +  mat - the matrix
2954: .  row - row permutation
2955: .  col - column permutation
2956: -  info - options for factorization, includes
2957: $          fill - expected fill as ratio of original fill.
2958: $          dtcol - pivot tolerance (0 no pivot, 1 full column pivoting)
2959: $                   Run with the option -info to determine an optimal value to use

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

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

2969:    Level: developer

2971: .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor(),
2972:           MatGetOrdering(), MatSetUnfactored(), MatFactorInfo, MatGetFactor()

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

2977: @*/
2978: PetscErrorCode MatLUFactor(Mat mat,IS row,IS col,const MatFactorInfo *info)
2979: {
2981:   MatFactorInfo  tinfo;

2989:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2990:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2991:   if (!mat->ops->lufactor) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
2992:   MatCheckPreallocated(mat,1);
2993:   if (!info) {
2994:     MatFactorInfoInitialize(&tinfo);
2995:     info = &tinfo;
2996:   }

2998:   PetscLogEventBegin(MAT_LUFactor,mat,row,col,0);
2999:   (*mat->ops->lufactor)(mat,row,col,info);
3000:   PetscLogEventEnd(MAT_LUFactor,mat,row,col,0);
3001:   PetscObjectStateIncrease((PetscObject)mat);
3002:   return(0);
3003: }

3005: /*@C
3006:    MatILUFactor - Performs in-place ILU factorization of matrix.

3008:    Collective on Mat

3010:    Input Parameters:
3011: +  mat - the matrix
3012: .  row - row permutation
3013: .  col - column permutation
3014: -  info - structure containing
3015: $      levels - number of levels of fill.
3016: $      expected fill - as ratio of original fill.
3017: $      1 or 0 - indicating force fill on diagonal (improves robustness for matrices
3018:                 missing diagonal entries)

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

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

3028:    Level: developer

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

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

3035: @*/
3036: PetscErrorCode MatILUFactor(Mat mat,IS row,IS col,const MatFactorInfo *info)
3037: {

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

3052:   PetscLogEventBegin(MAT_ILUFactor,mat,row,col,0);
3053:   (*mat->ops->ilufactor)(mat,row,col,info);
3054:   PetscLogEventEnd(MAT_ILUFactor,mat,row,col,0);
3055:   PetscObjectStateIncrease((PetscObject)mat);
3056:   return(0);
3057: }

3059: /*@C
3060:    MatLUFactorSymbolic - Performs symbolic LU factorization of matrix.
3061:    Call this routine before calling MatLUFactorNumeric().

3063:    Collective on Mat

3065:    Input Parameters:
3066: +  fact - the factor matrix obtained with MatGetFactor()
3067: .  mat - the matrix
3068: .  row, col - row and column permutations
3069: -  info - options for factorization, includes
3070: $          fill - expected fill as ratio of original fill.
3071: $          dtcol - pivot tolerance (0 no pivot, 1 full column pivoting)
3072: $                   Run with the option -info to determine an optimal value to use


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

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

3082:    Level: developer

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

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

3089: @*/
3090: PetscErrorCode MatLUFactorSymbolic(Mat fact,Mat mat,IS row,IS col,const MatFactorInfo *info)
3091: {
3093:   MatFactorInfo  tinfo;

3102:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3103:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3104:   if (!(fact)->ops->lufactorsymbolic) {
3105:     MatSolverType stype;
3106:     MatFactorGetSolverType(fact,&stype);
3107:     SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Matrix type %s symbolic LU using solver package %s",((PetscObject)mat)->type_name,stype);
3108:   }
3109:   MatCheckPreallocated(mat,2);
3110:   if (!info) {
3111:     MatFactorInfoInitialize(&tinfo);
3112:     info = &tinfo;
3113:   }

3115:   PetscLogEventBegin(MAT_LUFactorSymbolic,mat,row,col,0);
3116:   (fact->ops->lufactorsymbolic)(fact,mat,row,col,info);
3117:   PetscLogEventEnd(MAT_LUFactorSymbolic,mat,row,col,0);
3118:   PetscObjectStateIncrease((PetscObject)fact);
3119:   return(0);
3120: }

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

3126:    Collective on Mat

3128:    Input Parameters:
3129: +  fact - the factor matrix obtained with MatGetFactor()
3130: .  mat - the matrix
3131: -  info - options for factorization

3133:    Notes:
3134:    See MatLUFactor() for in-place factorization.  See
3135:    MatCholeskyFactorNumeric() for the symmetric, positive definite case.

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

3141:    Level: developer

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

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

3148: @*/
3149: PetscErrorCode MatLUFactorNumeric(Mat fact,Mat mat,const MatFactorInfo *info)
3150: {
3151:   MatFactorInfo  tinfo;

3159:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3160:   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);

3162:   if (!(fact)->ops->lufactornumeric) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s numeric LU",((PetscObject)mat)->type_name);
3163:   MatCheckPreallocated(mat,2);
3164:   if (!info) {
3165:     MatFactorInfoInitialize(&tinfo);
3166:     info = &tinfo;
3167:   }

3169:   PetscLogEventBegin(MAT_LUFactorNumeric,mat,fact,0,0);
3170:   (fact->ops->lufactornumeric)(fact,mat,info);
3171:   PetscLogEventEnd(MAT_LUFactorNumeric,mat,fact,0,0);
3172:   MatViewFromOptions(fact,NULL,"-mat_factor_view");
3173:   PetscObjectStateIncrease((PetscObject)fact);
3174:   return(0);
3175: }

3177: /*@C
3178:    MatCholeskyFactor - Performs in-place Cholesky factorization of a
3179:    symmetric matrix.

3181:    Collective on Mat

3183:    Input Parameters:
3184: +  mat - the matrix
3185: .  perm - row and column permutations
3186: -  f - expected fill as ratio of original fill

3188:    Notes:
3189:    See MatLUFactor() for the nonsymmetric case.  See also
3190:    MatCholeskyFactorSymbolic(), and MatCholeskyFactorNumeric().

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

3196:    Level: developer

3198: .seealso: MatLUFactor(), MatCholeskyFactorSymbolic(), MatCholeskyFactorNumeric()
3199:           MatGetOrdering()

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

3204: @*/
3205: PetscErrorCode MatCholeskyFactor(Mat mat,IS perm,const MatFactorInfo *info)
3206: {
3208:   MatFactorInfo  tinfo;

3215:   if (mat->rmap->N != mat->cmap->N) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONG,"Matrix must be square");
3216:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3217:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3218:   if (!mat->ops->choleskyfactor) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"In-place factorization for Mat type %s is not supported, try out-of-place factorization. See MatCholeskyFactorSymbolic/Numeric",((PetscObject)mat)->type_name);
3219:   MatCheckPreallocated(mat,1);
3220:   if (!info) {
3221:     MatFactorInfoInitialize(&tinfo);
3222:     info = &tinfo;
3223:   }

3225:   PetscLogEventBegin(MAT_CholeskyFactor,mat,perm,0,0);
3226:   (*mat->ops->choleskyfactor)(mat,perm,info);
3227:   PetscLogEventEnd(MAT_CholeskyFactor,mat,perm,0,0);
3228:   PetscObjectStateIncrease((PetscObject)mat);
3229:   return(0);
3230: }

3232: /*@C
3233:    MatCholeskyFactorSymbolic - Performs symbolic Cholesky factorization
3234:    of a symmetric matrix.

3236:    Collective on Mat

3238:    Input Parameters:
3239: +  fact - the factor matrix obtained with MatGetFactor()
3240: .  mat - the matrix
3241: .  perm - row and column permutations
3242: -  info - options for factorization, includes
3243: $          fill - expected fill as ratio of original fill.
3244: $          dtcol - pivot tolerance (0 no pivot, 1 full column pivoting)
3245: $                   Run with the option -info to determine an optimal value to use

3247:    Notes:
3248:    See MatLUFactorSymbolic() for the nonsymmetric case.  See also
3249:    MatCholeskyFactor() and MatCholeskyFactorNumeric().

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

3255:    Level: developer

3257: .seealso: MatLUFactorSymbolic(), MatCholeskyFactor(), MatCholeskyFactorNumeric()
3258:           MatGetOrdering()

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

3263: @*/
3264: PetscErrorCode MatCholeskyFactorSymbolic(Mat fact,Mat mat,IS perm,const MatFactorInfo *info)
3265: {
3267:   MatFactorInfo  tinfo;

3275:   if (mat->rmap->N != mat->cmap->N) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONG,"Matrix must be square");
3276:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3277:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3278:   if (!(fact)->ops->choleskyfactorsymbolic) {
3279:     MatSolverType stype;
3280:     MatFactorGetSolverType(fact,&stype);
3281:     SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s symbolic factor Cholesky using solver package %s",((PetscObject)mat)->type_name,stype);
3282:   }
3283:   MatCheckPreallocated(mat,2);
3284:   if (!info) {
3285:     MatFactorInfoInitialize(&tinfo);
3286:     info = &tinfo;
3287:   }

3289:   PetscLogEventBegin(MAT_CholeskyFactorSymbolic,mat,perm,0,0);
3290:   (fact->ops->choleskyfactorsymbolic)(fact,mat,perm,info);
3291:   PetscLogEventEnd(MAT_CholeskyFactorSymbolic,mat,perm,0,0);
3292:   PetscObjectStateIncrease((PetscObject)fact);
3293:   return(0);
3294: }

3296: /*@C
3297:    MatCholeskyFactorNumeric - Performs numeric Cholesky factorization
3298:    of a symmetric matrix. Call this routine after first calling
3299:    MatCholeskyFactorSymbolic().

3301:    Collective on Mat

3303:    Input Parameters:
3304: +  fact - the factor matrix obtained with MatGetFactor()
3305: .  mat - the initial matrix
3306: .  info - options for factorization
3307: -  fact - the symbolic factor of mat


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

3315:    Level: developer

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

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

3322: @*/
3323: PetscErrorCode MatCholeskyFactorNumeric(Mat fact,Mat mat,const MatFactorInfo *info)
3324: {
3325:   MatFactorInfo  tinfo;

3333:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3334:   if (!(fact)->ops->choleskyfactornumeric) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s numeric factor Cholesky",((PetscObject)mat)->type_name);
3335:   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);
3336:   MatCheckPreallocated(mat,2);
3337:   if (!info) {
3338:     MatFactorInfoInitialize(&tinfo);
3339:     info = &tinfo;
3340:   }

3342:   PetscLogEventBegin(MAT_CholeskyFactorNumeric,mat,fact,0,0);
3343:   (fact->ops->choleskyfactornumeric)(fact,mat,info);
3344:   PetscLogEventEnd(MAT_CholeskyFactorNumeric,mat,fact,0,0);
3345:   MatViewFromOptions(fact,NULL,"-mat_factor_view");
3346:   PetscObjectStateIncrease((PetscObject)fact);
3347:   return(0);
3348: }

3350: /* ----------------------------------------------------------------*/
3351: /*@
3352:    MatSolve - Solves A x = b, given a factored matrix.

3354:    Neighbor-wise Collective on Mat

3356:    Input Parameters:
3357: +  mat - the factored matrix
3358: -  b - the right-hand-side vector

3360:    Output Parameter:
3361: .  x - the result vector

3363:    Notes:
3364:    The vectors b and x cannot be the same.  I.e., one cannot
3365:    call MatSolve(A,x,x).

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

3372:    Level: developer

3374: .seealso: MatSolveAdd(), MatSolveTranspose(), MatSolveTransposeAdd()
3375: @*/
3376: PetscErrorCode MatSolve(Mat mat,Vec b,Vec x)
3377: {

3387:   if (x == b) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"x and b must be different vectors");
3388:   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);
3389:   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);
3390:   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);
3391:   if (!mat->rmap->N && !mat->cmap->N) return(0);
3392:   MatCheckPreallocated(mat,1);

3394:   PetscLogEventBegin(MAT_Solve,mat,b,x,0);
3395:   if (mat->factorerrortype) {
3396:     PetscInfo1(mat,"MatFactorError %D\n",mat->factorerrortype);
3397:     VecSetInf(x);
3398:   } else {
3399:     if (!mat->ops->solve) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
3400:     (*mat->ops->solve)(mat,b,x);
3401:   }
3402:   PetscLogEventEnd(MAT_Solve,mat,b,x,0);
3403:   PetscObjectStateIncrease((PetscObject)x);
3404:   return(0);
3405: }

3407: static PetscErrorCode MatMatSolve_Basic(Mat A,Mat B,Mat X,PetscBool trans)
3408: {
3410:   Vec            b,x;
3411:   PetscInt       m,N,i;
3412:   PetscScalar    *bb,*xx;

3415:   MatDenseGetArrayRead(B,(const PetscScalar**)&bb);
3416:   MatDenseGetArray(X,&xx);
3417:   MatGetLocalSize(B,&m,NULL);  /* number local rows */
3418:   MatGetSize(B,NULL,&N);       /* total columns in dense matrix */
3419:   MatCreateVecs(A,&x,&b);
3420:   for (i=0; i<N; i++) {
3421:     VecPlaceArray(b,bb + i*m);
3422:     VecPlaceArray(x,xx + i*m);
3423:     if (trans) {
3424:       MatSolveTranspose(A,b,x);
3425:     } else {
3426:       MatSolve(A,b,x);
3427:     }
3428:     VecResetArray(x);
3429:     VecResetArray(b);
3430:   }
3431:   VecDestroy(&b);
3432:   VecDestroy(&x);
3433:   MatDenseRestoreArrayRead(B,(const PetscScalar**)&bb);
3434:   MatDenseRestoreArray(X,&xx);
3435:   return(0);
3436: }

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

3441:    Neighbor-wise Collective on Mat

3443:    Input Parameters:
3444: +  A - the factored matrix
3445: -  B - the right-hand-side matrix MATDENSE (or sparse -- when using MUMPS)

3447:    Output Parameter:
3448: .  X - the result matrix (dense matrix)

3450:    Notes:
3451:    If B is a MATDENSE matrix then one can call MatMatSolve(A,B,B) except with MKL_CPARDISO;
3452:    otherwise, B and X cannot be the same.

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

3460:    Level: developer

3462: .seealso: MatMatSolveTranspose(), MatLUFactor(), MatCholeskyFactor()
3463: @*/
3464: PetscErrorCode MatMatSolve(Mat A,Mat B,Mat X)
3465: {

3475:   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);
3476:   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);
3477:   if (X->cmap->N != B->cmap->N) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Solution matrix must have same number of columns as rhs matrix");
3478:   if (!A->rmap->N && !A->cmap->N) return(0);
3479:   if (!A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
3480:   MatCheckPreallocated(A,1);

3482:   PetscLogEventBegin(MAT_MatSolve,A,B,X,0);
3483:   if (!A->ops->matsolve) {
3484:     PetscInfo1(A,"Mat type %s using basic MatMatSolve\n",((PetscObject)A)->type_name);
3485:     MatMatSolve_Basic(A,B,X,PETSC_FALSE);
3486:   } else {
3487:     (*A->ops->matsolve)(A,B,X);
3488:   }
3489:   PetscLogEventEnd(MAT_MatSolve,A,B,X,0);
3490:   PetscObjectStateIncrease((PetscObject)X);
3491:   return(0);
3492: }

3494: /*@
3495:    MatMatSolveTranspose - Solves A^T X = B, given a factored matrix.

3497:    Neighbor-wise Collective on Mat

3499:    Input Parameters:
3500: +  A - the factored matrix
3501: -  B - the right-hand-side matrix  (dense matrix)

3503:    Output Parameter:
3504: .  X - the result matrix (dense matrix)

3506:    Notes:
3507:    The matrices B and X cannot be the same.  I.e., one cannot
3508:    call MatMatSolveTranspose(A,X,X).

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

3516:    When using SuperLU_Dist or MUMPS as a parallel solver, PETSc will use their functionality to solve multiple right hand sides simultaneously.

3518:    Level: developer

3520: .seealso: MatMatSolve(), MatLUFactor(), MatCholeskyFactor()
3521: @*/
3522: PetscErrorCode MatMatSolveTranspose(Mat A,Mat B,Mat X)
3523: {

3533:   if (X == B) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_IDN,"X and B must be different matrices");
3534:   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);
3535:   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);
3536:   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);
3537:   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");
3538:   if (!A->rmap->N && !A->cmap->N) return(0);
3539:   if (!A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
3540:   MatCheckPreallocated(A,1);

3542:   PetscLogEventBegin(MAT_MatSolve,A,B,X,0);
3543:   if (!A->ops->matsolvetranspose) {
3544:     PetscInfo1(A,"Mat type %s using basic MatMatSolveTranspose\n",((PetscObject)A)->type_name);
3545:     MatMatSolve_Basic(A,B,X,PETSC_TRUE);
3546:   } else {
3547:     (*A->ops->matsolvetranspose)(A,B,X);
3548:   }
3549:   PetscLogEventEnd(MAT_MatSolve,A,B,X,0);
3550:   PetscObjectStateIncrease((PetscObject)X);
3551:   return(0);
3552: }

3554: /*@
3555:    MatMatTransposeSolve - Solves A X = B^T, given a factored matrix.

3557:    Neighbor-wise Collective on Mat

3559:    Input Parameters:
3560: +  A - the factored matrix
3561: -  Bt - the transpose of right-hand-side matrix

3563:    Output Parameter:
3564: .  X - the result matrix (dense matrix)

3566:    Notes:
3567:    Most users should usually 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(). However KSP can only solve for one vector (column of X)
3570:    at a time.

3572:    For MUMPS, it only supports centralized sparse compressed column format on the host processor for right hand side matrix. User must create B^T in sparse compressed row format on the host processor and call MatMatTransposeSolve() to implement MUMPS' MatMatSolve().

3574:    Level: developer

3576: .seealso: MatMatSolve(), MatMatSolveTranspose(), MatLUFactor(), MatCholeskyFactor()
3577: @*/
3578: PetscErrorCode MatMatTransposeSolve(Mat A,Mat Bt,Mat X)
3579: {


3590:   if (X == Bt) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_IDN,"X and B must be different matrices");
3591:   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);
3592:   if (A->rmap->N != Bt->cmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Mat A,Mat Bt: global dim %D %D",A->rmap->N,Bt->cmap->N);
3593:   if (X->cmap->N < Bt->rmap->N) SETERRQ(PetscObjectComm((PetscObject)X),PETSC_ERR_ARG_SIZ,"Solution matrix must have same number of columns as row number of the rhs matrix");
3594:   if (!A->rmap->N && !A->cmap->N) return(0);
3595:   if (!A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
3596:   MatCheckPreallocated(A,1);

3598:   if (!A->ops->mattransposesolve) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Mat type %s",((PetscObject)A)->type_name);
3599:   PetscLogEventBegin(MAT_MatTrSolve,A,Bt,X,0);
3600:   (*A->ops->mattransposesolve)(A,Bt,X);
3601:   PetscLogEventEnd(MAT_MatTrSolve,A,Bt,X,0);
3602:   PetscObjectStateIncrease((PetscObject)X);
3603:   return(0);
3604: }

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

3610:    Neighbor-wise Collective on Mat

3612:    Input Parameters:
3613: +  mat - the factored matrix
3614: -  b - the right-hand-side vector

3616:    Output Parameter:
3617: .  x - the result vector

3619:    Notes:
3620:    MatSolve() should be used for most applications, as it performs
3621:    a forward solve followed by a backward solve.

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

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

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

3636:    Level: developer

3638: .seealso: MatSolve(), MatBackwardSolve()
3639: @*/
3640: PetscErrorCode MatForwardSolve(Mat mat,Vec b,Vec x)
3641: {

3651:   if (x == b) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"x and b must be different vectors");
3652:   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);
3653:   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);
3654:   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);
3655:   if (!mat->rmap->N && !mat->cmap->N) return(0);
3656:   MatCheckPreallocated(mat,1);

3658:   if (!mat->ops->forwardsolve) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
3659:   PetscLogEventBegin(MAT_ForwardSolve,mat,b,x,0);
3660:   (*mat->ops->forwardsolve)(mat,b,x);
3661:   PetscLogEventEnd(MAT_ForwardSolve,mat,b,x,0);
3662:   PetscObjectStateIncrease((PetscObject)x);
3663:   return(0);
3664: }

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

3670:    Neighbor-wise Collective on Mat

3672:    Input Parameters:
3673: +  mat - the factored matrix
3674: -  b - the right-hand-side vector

3676:    Output Parameter:
3677: .  x - the result vector

3679:    Notes:
3680:    MatSolve() should be used for most applications, as it performs
3681:    a forward solve followed by a backward solve.

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

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

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

3696:    Level: developer

3698: .seealso: MatSolve(), MatForwardSolve()
3699: @*/
3700: PetscErrorCode MatBackwardSolve(Mat mat,Vec b,Vec x)
3701: {

3711:   if (x == b) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"x and b must be different vectors");
3712:   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);
3713:   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);
3714:   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);
3715:   if (!mat->rmap->N && !mat->cmap->N) return(0);
3716:   MatCheckPreallocated(mat,1);

3718:   if (!mat->ops->backwardsolve) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
3719:   PetscLogEventBegin(MAT_BackwardSolve,mat,b,x,0);
3720:   (*mat->ops->backwardsolve)(mat,b,x);
3721:   PetscLogEventEnd(MAT_BackwardSolve,mat,b,x,0);
3722:   PetscObjectStateIncrease((PetscObject)x);
3723:   return(0);
3724: }

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

3729:    Neighbor-wise Collective on Mat

3731:    Input Parameters:
3732: +  mat - the factored matrix
3733: .  b - the right-hand-side vector
3734: -  y - the vector to be added to

3736:    Output Parameter:
3737: .  x - the result vector

3739:    Notes:
3740:    The vectors b and x cannot be the same.  I.e., one cannot
3741:    call MatSolveAdd(A,x,y,x).

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

3747:    Level: developer

3749: .seealso: MatSolve(), MatSolveTranspose(), MatSolveTransposeAdd()
3750: @*/
3751: PetscErrorCode MatSolveAdd(Mat mat,Vec b,Vec y,Vec x)
3752: {
3753:   PetscScalar    one = 1.0;
3754:   Vec            tmp;

3766:   if (x == b) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"x and b must be different vectors");
3767:   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);
3768:   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);
3769:   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);
3770:   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);
3771:   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);
3772:   if (!mat->rmap->N && !mat->cmap->N) return(0);
3773:    MatCheckPreallocated(mat,1);

3775:   PetscLogEventBegin(MAT_SolveAdd,mat,b,x,y);
3776:   if (mat->factorerrortype) {
3777:     PetscInfo1(mat,"MatFactorError %D\n",mat->factorerrortype);
3778:     VecSetInf(x);
3779:   } else if (mat->ops->solveadd) {
3780:     (*mat->ops->solveadd)(mat,b,y,x);
3781:   } else {
3782:     /* do the solve then the add manually */
3783:     if (x != y) {
3784:       MatSolve(mat,b,x);
3785:       VecAXPY(x,one,y);
3786:     } else {
3787:       VecDuplicate(x,&tmp);
3788:       PetscLogObjectParent((PetscObject)mat,(PetscObject)tmp);
3789:       VecCopy(x,tmp);
3790:       MatSolve(mat,b,x);
3791:       VecAXPY(x,one,tmp);
3792:       VecDestroy(&tmp);
3793:     }
3794:   }
3795:   PetscLogEventEnd(MAT_SolveAdd,mat,b,x,y);
3796:   PetscObjectStateIncrease((PetscObject)x);
3797:   return(0);
3798: }

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

3803:    Neighbor-wise Collective on Mat

3805:    Input Parameters:
3806: +  mat - the factored matrix
3807: -  b - the right-hand-side vector

3809:    Output Parameter:
3810: .  x - the result vector

3812:    Notes:
3813:    The vectors b and x cannot be the same.  I.e., one cannot
3814:    call MatSolveTranspose(A,x,x).

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

3820:    Level: developer

3822: .seealso: MatSolve(), MatSolveAdd(), MatSolveTransposeAdd()
3823: @*/
3824: PetscErrorCode MatSolveTranspose(Mat mat,Vec b,Vec x)
3825: {

3835:   if (x == b) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"x and b must be different vectors");
3836:   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);
3837:   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);
3838:   if (!mat->rmap->N && !mat->cmap->N) return(0);
3839:   MatCheckPreallocated(mat,1);
3840:   PetscLogEventBegin(MAT_SolveTranspose,mat,b,x,0);
3841:   if (mat->factorerrortype) {
3842:     PetscInfo1(mat,"MatFactorError %D\n",mat->factorerrortype);
3843:     VecSetInf(x);
3844:   } else {
3845:     if (!mat->ops->solvetranspose) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Matrix type %s",((PetscObject)mat)->type_name);
3846:     (*mat->ops->solvetranspose)(mat,b,x);
3847:   }
3848:   PetscLogEventEnd(MAT_SolveTranspose,mat,b,x,0);
3849:   PetscObjectStateIncrease((PetscObject)x);
3850:   return(0);
3851: }

3853: /*@
3854:    MatSolveTransposeAdd - Computes x = y + inv(Transpose(A)) b, given a
3855:                       factored matrix.

3857:    Neighbor-wise Collective on Mat

3859:    Input Parameters:
3860: +  mat - the factored matrix
3861: .  b - the right-hand-side vector
3862: -  y - the vector to be added to

3864:    Output Parameter:
3865: .  x - the result vector

3867:    Notes:
3868:    The vectors b and x cannot be the same.  I.e., one cannot
3869:    call MatSolveTransposeAdd(A,x,y,x).

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

3875:    Level: developer

3877: .seealso: MatSolve(), MatSolveAdd(), MatSolveTranspose()
3878: @*/
3879: PetscErrorCode MatSolveTransposeAdd(Mat mat,Vec b,Vec y,Vec x)
3880: {
3881:   PetscScalar    one = 1.0;
3883:   Vec            tmp;

3894:   if (x == b) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"x and b must be different vectors");
3895:   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);
3896:   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);
3897:   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);
3898:   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);
3899:   if (!mat->rmap->N && !mat->cmap->N) return(0);
3900:    MatCheckPreallocated(mat,1);

3902:   PetscLogEventBegin(MAT_SolveTransposeAdd,mat,b,x,y);
3903:   if (mat->factorerrortype) {
3904:     PetscInfo1(mat,"MatFactorError %D\n",mat->factorerrortype);
3905:     VecSetInf(x);
3906:   } else if (mat->ops->solvetransposeadd){
3907:     (*mat->ops->solvetransposeadd)(mat,b,y,x);
3908:   } else {
3909:     /* do the solve then the add manually */
3910:     if (x != y) {
3911:       MatSolveTranspose(mat,b,x);
3912:       VecAXPY(x,one,y);
3913:     } else {
3914:       VecDuplicate(x,&tmp);
3915:       PetscLogObjectParent((PetscObject)mat,(PetscObject)tmp);
3916:       VecCopy(x,tmp);
3917:       MatSolveTranspose(mat,b,x);
3918:       VecAXPY(x,one,tmp);
3919:       VecDestroy(&tmp);
3920:     }
3921:   }
3922:   PetscLogEventEnd(MAT_SolveTransposeAdd,mat,b,x,y);
3923:   PetscObjectStateIncrease((PetscObject)x);
3924:   return(0);
3925: }
3926: /* ----------------------------------------------------------------*/

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

3931:    Neighbor-wise Collective on Mat

3933:    Input Parameters:
3934: +  mat - the matrix
3935: .  b - the right hand side
3936: .  omega - the relaxation factor
3937: .  flag - flag indicating the type of SOR (see below)
3938: .  shift -  diagonal shift
3939: .  its - the number of iterations
3940: -  lits - the number of local iterations

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

3945:    SOR Flags:
3946: +     SOR_FORWARD_SWEEP - forward SOR
3947: .     SOR_BACKWARD_SWEEP - backward SOR
3948: .     SOR_SYMMETRIC_SWEEP - SSOR (symmetric SOR)
3949: .     SOR_LOCAL_FORWARD_SWEEP - local forward SOR
3950: .     SOR_LOCAL_BACKWARD_SWEEP - local forward SOR
3951: .     SOR_LOCAL_SYMMETRIC_SWEEP - local SSOR
3952: .     SOR_APPLY_UPPER, SOR_APPLY_LOWER - applies
3953:          upper/lower triangular part of matrix to
3954:          vector (with omega)
3955: -     SOR_ZERO_INITIAL_GUESS - zero initial guess

3957:    Notes:
3958:    SOR_LOCAL_FORWARD_SWEEP, SOR_LOCAL_BACKWARD_SWEEP, and
3959:    SOR_LOCAL_SYMMETRIC_SWEEP perform separate independent smoothings
3960:    on each processor.

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

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

3968:    Notes for Advanced Users:
3969:    The flags are implemented as bitwise inclusive or operations.
3970:    For example, use (SOR_ZERO_INITIAL_GUESS | SOR_SYMMETRIC_SWEEP)
3971:    to specify a zero initial guess for SSOR.

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

3977:    Vectors x and b CANNOT be the same

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

3981:    Level: developer

3983: @*/
3984: PetscErrorCode MatSOR(Mat mat,Vec b,PetscReal omega,MatSORType flag,PetscReal shift,PetscInt its,PetscInt lits,Vec x)
3985: {

3995:   if (!mat->ops->sor) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
3996:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3997:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3998:   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);
3999:   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);
4000:   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);
4001:   if (its <= 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Relaxation requires global its %D positive",its);
4002:   if (lits <= 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Relaxation requires local its %D positive",lits);
4003:   if (b == x) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"b and x vector cannot be the same");

4005:   MatCheckPreallocated(mat,1);
4006:   PetscLogEventBegin(MAT_SOR,mat,b,x,0);
4007:   ierr =(*mat->ops->sor)(mat,b,omega,flag,shift,its,lits,x);
4008:   PetscLogEventEnd(MAT_SOR,mat,b,x,0);
4009:   PetscObjectStateIncrease((PetscObject)x);
4010:   return(0);
4011: }

4013: /*
4014:       Default matrix copy routine.
4015: */
4016: PetscErrorCode MatCopy_Basic(Mat A,Mat B,MatStructure str)
4017: {
4018:   PetscErrorCode    ierr;
4019:   PetscInt          i,rstart = 0,rend = 0,nz;
4020:   const PetscInt    *cwork;
4021:   const PetscScalar *vwork;

4024:   if (B->assembled) {
4025:     MatZeroEntries(B);
4026:   }
4027:   if (str == SAME_NONZERO_PATTERN) {
4028:     MatGetOwnershipRange(A,&rstart,&rend);
4029:     for (i=rstart; i<rend; i++) {
4030:       MatGetRow(A,i,&nz,&cwork,&vwork);
4031:       MatSetValues(B,1,&i,nz,cwork,vwork,INSERT_VALUES);
4032:       MatRestoreRow(A,i,&nz,&cwork,&vwork);
4033:     }
4034:   } else {
4035:     MatAYPX(B,0.0,A,str);
4036:   }
4037:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
4038:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
4039:   return(0);
4040: }

4042: /*@
4043:    MatCopy - Copies a matrix to another matrix.

4045:    Collective on Mat

4047:    Input Parameters:
4048: +  A - the matrix
4049: -  str - SAME_NONZERO_PATTERN or DIFFERENT_NONZERO_PATTERN

4051:    Output Parameter:
4052: .  B - where the copy is put

4054:    Notes:
4055:    If you use SAME_NONZERO_PATTERN then the two matrices had better have the
4056:    same nonzero pattern or the routine will crash.

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

4062:    Level: intermediate

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

4066: @*/
4067: PetscErrorCode MatCopy(Mat A,Mat B,MatStructure str)
4068: {
4070:   PetscInt       i;

4078:   MatCheckPreallocated(B,2);
4079:   if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
4080:   if (A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
4081:   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);
4082:   MatCheckPreallocated(A,1);
4083:   if (A == B) return(0);

4085:   PetscLogEventBegin(MAT_Copy,A,B,0,0);
4086:   if (A->ops->copy) {
4087:     (*A->ops->copy)(A,B,str);
4088:   } else { /* generic conversion */
4089:     MatCopy_Basic(A,B,str);
4090:   }

4092:   B->stencil.dim = A->stencil.dim;
4093:   B->stencil.noc = A->stencil.noc;
4094:   for (i=0; i<=A->stencil.dim; i++) {
4095:     B->stencil.dims[i]   = A->stencil.dims[i];
4096:     B->stencil.starts[i] = A->stencil.starts[i];
4097:   }

4099:   PetscLogEventEnd(MAT_Copy,A,B,0,0);
4100:   PetscObjectStateIncrease((PetscObject)B);
4101:   return(0);
4102: }

4104: /*@C
4105:    MatConvert - Converts a matrix to another matrix, either of the same
4106:    or different type.

4108:    Collective on Mat

4110:    Input Parameters:
4111: +  mat - the matrix
4112: .  newtype - new matrix type.  Use MATSAME to create a new matrix of the
4113:    same type as the original matrix.
4114: -  reuse - denotes if the destination matrix is to be created or reused.
4115:    Use MAT_INPLACE_MATRIX for inplace conversion (that is when you want the input mat to be changed to contain the matrix in the new format), otherwise use
4116:    MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX (can only be used after the first call was made with MAT_INITIAL_MATRIX, causes the matrix space in M to be reused).

4118:    Output Parameter:
4119: .  M - pointer to place new matrix

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

4126:    Cannot be used to convert a sequential matrix to parallel or parallel to sequential,
4127:    the MPI communicator of the generated matrix is always the same as the communicator
4128:    of the input matrix.

4130:    Level: intermediate

4132: .seealso: MatCopy(), MatDuplicate()
4133: @*/
4134: PetscErrorCode MatConvert(Mat mat, MatType newtype,MatReuse reuse,Mat *M)
4135: {
4137:   PetscBool      sametype,issame,flg,issymmetric,ishermitian;
4138:   char           convname[256],mtype[256];
4139:   Mat            B;

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

4149:   PetscOptionsGetString(((PetscObject)mat)->options,((PetscObject)mat)->prefix,"-matconvert_type",mtype,sizeof(mtype),&flg);
4150:   if (flg) newtype = mtype;

4152:   PetscObjectTypeCompare((PetscObject)mat,newtype,&sametype);
4153:   PetscStrcmp(newtype,"same",&issame);
4154:   if ((reuse == MAT_INPLACE_MATRIX) && (mat != *M)) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"MAT_INPLACE_MATRIX requires same input and output matrix");
4155:   if ((reuse == MAT_REUSE_MATRIX) && (mat == *M)) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"MAT_REUSE_MATRIX means reuse matrix in final argument, perhaps you mean MAT_INPLACE_MATRIX");

4157:   if ((reuse == MAT_INPLACE_MATRIX) && (issame || sametype)) {
4158:     PetscInfo3(mat,"Early return for inplace %s %d %d\n",((PetscObject)mat)->type_name,sametype,issame);
4159:     return(0);
4160:   }

4162:   /* Cache Mat options because some converter use MatHeaderReplace  */
4163:   issymmetric = mat->symmetric;
4164:   ishermitian = mat->hermitian;

4166:   if ((sametype || issame) && (reuse==MAT_INITIAL_MATRIX) && mat->ops->duplicate) {
4167:     PetscInfo3(mat,"Calling duplicate for initial matrix %s %d %d\n",((PetscObject)mat)->type_name,sametype,issame);
4168:     (*mat->ops->duplicate)(mat,MAT_COPY_VALUES,M);
4169:   } else {
4170:     PetscErrorCode (*conv)(Mat, MatType,MatReuse,Mat*)=NULL;
4171:     const char     *prefix[3] = {"seq","mpi",""};
4172:     PetscInt       i;
4173:     /*
4174:        Order of precedence:
4175:        0) See if newtype is a superclass of the current matrix.
4176:        1) See if a specialized converter is known to the current matrix.
4177:        2) See if a specialized converter is known to the desired matrix class.
4178:        3) See if a good general converter is registered for the desired class
4179:           (as of 6/27/03 only MATMPIADJ falls into this category).
4180:        4) See if a good general converter is known for the current matrix.
4181:        5) Use a really basic converter.
4182:     */

4184:     /* 0) See if newtype is a superclass of the current matrix.
4185:           i.e mat is mpiaij and newtype is aij */
4186:     for (i=0; i<2; i++) {
4187:       PetscStrncpy(convname,prefix[i],sizeof(convname));
4188:       PetscStrlcat(convname,newtype,sizeof(convname));
4189:       PetscStrcmp(convname,((PetscObject)mat)->type_name,&flg);
4190:       PetscInfo3(mat,"Check superclass %s %s -> %d\n",convname,((PetscObject)mat)->type_name,flg);
4191:       if (flg) {
4192:         if (reuse == MAT_INPLACE_MATRIX) {
4193:           PetscInfo(mat,"Early return\n");
4194:           return(0);
4195:         } else if (reuse == MAT_INITIAL_MATRIX && mat->ops->duplicate) {
4196:           PetscInfo(mat,"Calling MatDuplicate\n");
4197:           (*mat->ops->duplicate)(mat,MAT_COPY_VALUES,M);
4198:           return(0);
4199:         } else if (reuse == MAT_REUSE_MATRIX && mat->ops->copy) {
4200:           PetscInfo(mat,"Calling MatCopy\n");
4201:           MatCopy(mat,*M,SAME_NONZERO_PATTERN);
4202:           return(0);
4203:         }
4204:       }
4205:     }
4206:     /* 1) See if a specialized converter is known to the current matrix and the desired class */
4207:     for (i=0; i<3; i++) {
4208:       PetscStrncpy(convname,"MatConvert_",sizeof(convname));
4209:       PetscStrlcat(convname,((PetscObject)mat)->type_name,sizeof(convname));
4210:       PetscStrlcat(convname,"_",sizeof(convname));
4211:       PetscStrlcat(convname,prefix[i],sizeof(convname));
4212:       PetscStrlcat(convname,issame ? ((PetscObject)mat)->type_name : newtype,sizeof(convname));
4213:       PetscStrlcat(convname,"_C",sizeof(convname));
4214:       PetscObjectQueryFunction((PetscObject)mat,convname,&conv);
4215:       PetscInfo3(mat,"Check specialized (1) %s (%s) -> %d\n",convname,((PetscObject)mat)->type_name,!!conv);
4216:       if (conv) goto foundconv;
4217:     }

4219:     /* 2)  See if a specialized converter is known to the desired matrix class. */
4220:     MatCreate(PetscObjectComm((PetscObject)mat),&B);
4221:     MatSetSizes(B,mat->rmap->n,mat->cmap->n,mat->rmap->N,mat->cmap->N);
4222:     MatSetType(B,newtype);
4223:     for (i=0; i<3; i++) {
4224:       PetscStrncpy(convname,"MatConvert_",sizeof(convname));
4225:       PetscStrlcat(convname,((PetscObject)mat)->type_name,sizeof(convname));
4226:       PetscStrlcat(convname,"_",sizeof(convname));
4227:       PetscStrlcat(convname,prefix[i],sizeof(convname));
4228:       PetscStrlcat(convname,newtype,sizeof(convname));
4229:       PetscStrlcat(convname,"_C",sizeof(convname));
4230:       PetscObjectQueryFunction((PetscObject)B,convname,&conv);
4231:       PetscInfo3(mat,"Check specialized (2) %s (%s) -> %d\n",convname,((PetscObject)B)->type_name,!!conv);
4232:       if (conv) {
4233:         MatDestroy(&B);
4234:         goto foundconv;
4235:       }
4236:     }

4238:     /* 3) See if a good general converter is registered for the desired class */
4239:     conv = B->ops->convertfrom;
4240:     PetscInfo2(mat,"Check convertfrom (%s) -> %d\n",((PetscObject)B)->type_name,!!conv);
4241:     MatDestroy(&B);
4242:     if (conv) goto foundconv;

4244:     /* 4) See if a good general converter is known for the current matrix */
4245:     if (mat->ops->convert) conv = mat->ops->convert;

4247:     PetscInfo2(mat,"Check general convert (%s) -> %d\n",((PetscObject)mat)->type_name,!!conv);
4248:     if (conv) goto foundconv;

4250:     /* 5) Use a really basic converter. */
4251:     PetscInfo(mat,"Using MatConvert_Basic\n");
4252:     conv = MatConvert_Basic;

4254: foundconv:
4255:     PetscLogEventBegin(MAT_Convert,mat,0,0,0);
4256:     (*conv)(mat,newtype,reuse,M);
4257:     if (mat->rmap->mapping && mat->cmap->mapping && !(*M)->rmap->mapping && !(*M)->cmap->mapping) {
4258:       /* the block sizes must be same if the mappings are copied over */
4259:       (*M)->rmap->bs = mat->rmap->bs;
4260:       (*M)->cmap->bs = mat->cmap->bs;
4261:       PetscObjectReference((PetscObject)mat->rmap->mapping);
4262:       PetscObjectReference((PetscObject)mat->cmap->mapping);
4263:       (*M)->rmap->mapping = mat->rmap->mapping;
4264:       (*M)->cmap->mapping = mat->cmap->mapping;
4265:     }
4266:     (*M)->stencil.dim = mat->stencil.dim;
4267:     (*M)->stencil.noc = mat->stencil.noc;
4268:     for (i=0; i<=mat->stencil.dim; i++) {
4269:       (*M)->stencil.dims[i]   = mat->stencil.dims[i];
4270:       (*M)->stencil.starts[i] = mat->stencil.starts[i];
4271:     }
4272:     PetscLogEventEnd(MAT_Convert,mat,0,0,0);
4273:   }
4274:   PetscObjectStateIncrease((PetscObject)*M);

4276:   /* Copy Mat options */
4277:   if (issymmetric) {
4278:     MatSetOption(*M,MAT_SYMMETRIC,PETSC_TRUE);
4279:   }
4280:   if (ishermitian) {
4281:     MatSetOption(*M,MAT_HERMITIAN,PETSC_TRUE);
4282:   }
4283:   return(0);
4284: }

4286: /*@C
4287:    MatFactorGetSolverType - Returns name of the package providing the factorization routines

4289:    Not Collective

4291:    Input Parameter:
4292: .  mat - the matrix, must be a factored matrix

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

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

4301:    Level: intermediate

4303: .seealso: MatCopy(), MatDuplicate(), MatGetFactorAvailable(), MatGetFactor()
4304: @*/
4305: PetscErrorCode MatFactorGetSolverType(Mat mat, MatSolverType *type)
4306: {
4307:   PetscErrorCode ierr, (*conv)(Mat,MatSolverType*);

4312:   if (!mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Only for factored matrix");
4313:   PetscObjectQueryFunction((PetscObject)mat,"MatFactorGetSolverType_C",&conv);
4314:   if (!conv) {
4315:     *type = MATSOLVERPETSC;
4316:   } else {
4317:     (*conv)(mat,type);
4318:   }
4319:   return(0);
4320: }

4322: typedef struct _MatSolverTypeForSpecifcType* MatSolverTypeForSpecifcType;
4323: struct _MatSolverTypeForSpecifcType {
4324:   MatType                        mtype;
4325:   PetscErrorCode                 (*createfactor[4])(Mat,MatFactorType,Mat*);
4326:   MatSolverTypeForSpecifcType next;
4327: };

4329: typedef struct _MatSolverTypeHolder* MatSolverTypeHolder;
4330: struct _MatSolverTypeHolder {
4331:   char                        *name;
4332:   MatSolverTypeForSpecifcType handlers;
4333:   MatSolverTypeHolder         next;
4334: };

4336: static MatSolverTypeHolder MatSolverTypeHolders = NULL;

4338: /*@C
4339:    MatSolveTypeRegister - Registers a MatSolverType that works for a particular matrix type

4341:    Input Parameters:
4342: +    package - name of the package, for example petsc or superlu
4343: .    mtype - the matrix type that works with this package
4344: .    ftype - the type of factorization supported by the package
4345: -    createfactor - routine that will create the factored matrix ready to be used

4347:     Level: intermediate

4349: .seealso: MatCopy(), MatDuplicate(), MatGetFactorAvailable(), MatGetFactor()
4350: @*/
4351: PetscErrorCode MatSolverTypeRegister(MatSolverType package,MatType mtype,MatFactorType ftype,PetscErrorCode (*createfactor)(Mat,MatFactorType,Mat*))
4352: {
4353:   PetscErrorCode              ierr;
4354:   MatSolverTypeHolder         next = MatSolverTypeHolders,prev = NULL;
4355:   PetscBool                   flg;
4356:   MatSolverTypeForSpecifcType inext,iprev = NULL;

4359:   MatInitializePackage();
4360:   if (!next) {
4361:     PetscNew(&MatSolverTypeHolders);
4362:     PetscStrallocpy(package,&MatSolverTypeHolders->name);
4363:     PetscNew(&MatSolverTypeHolders->handlers);
4364:     PetscStrallocpy(mtype,(char **)&MatSolverTypeHolders->handlers->mtype);
4365:     MatSolverTypeHolders->handlers->createfactor[(int)ftype-1] = createfactor;
4366:     return(0);
4367:   }
4368:   while (next) {
4369:     PetscStrcasecmp(package,next->name,&flg);
4370:     if (flg) {
4371:       if (!next->handlers) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"MatSolverTypeHolder is missing handlers");
4372:       inext = next->handlers;
4373:       while (inext) {
4374:         PetscStrcasecmp(mtype,inext->mtype,&flg);
4375:         if (flg) {
4376:           inext->createfactor[(int)ftype-1] = createfactor;
4377:           return(0);
4378:         }
4379:         iprev = inext;
4380:         inext = inext->next;
4381:       }
4382:       PetscNew(&iprev->next);
4383:       PetscStrallocpy(mtype,(char **)&iprev->next->mtype);
4384:       iprev->next->createfactor[(int)ftype-1] = createfactor;
4385:       return(0);
4386:     }
4387:     prev = next;
4388:     next = next->next;
4389:   }
4390:   PetscNew(&prev->next);
4391:   PetscStrallocpy(package,&prev->next->name);
4392:   PetscNew(&prev->next->handlers);
4393:   PetscStrallocpy(mtype,(char **)&prev->next->handlers->mtype);
4394:   prev->next->handlers->createfactor[(int)ftype-1] = createfactor;
4395:   return(0);
4396: }

4398: /*@C
4399:    MatSolveTypeGet - Gets the function that creates the factor matrix if it exist

4401:    Input Parameters:
4402: +    type - name of the package, for example petsc or superlu
4403: .    ftype - the type of factorization supported by the type
4404: -    mtype - the matrix type that works with this type

4406:    Output Parameters:
4407: +   foundtype - PETSC_TRUE if the type was registered
4408: .   foundmtype - PETSC_TRUE if the type supports the requested mtype
4409: -   createfactor - routine that will create the factored matrix ready to be used or NULL if not found

4411:     Level: intermediate

4413: .seealso: MatCopy(), MatDuplicate(), MatGetFactorAvailable(), MatSolvePackageRegister), MatGetFactor()
4414: @*/
4415: PetscErrorCode MatSolverTypeGet(MatSolverType type,MatType mtype,MatFactorType ftype,PetscBool *foundtype,PetscBool *foundmtype,PetscErrorCode (**createfactor)(Mat,MatFactorType,Mat*))
4416: {
4417:   PetscErrorCode              ierr;
4418:   MatSolverTypeHolder         next = MatSolverTypeHolders;
4419:   PetscBool                   flg;
4420:   MatSolverTypeForSpecifcType inext;

4423:   if (foundtype) *foundtype = PETSC_FALSE;
4424:   if (foundmtype)   *foundmtype   = PETSC_FALSE;
4425:   if (createfactor) *createfactor    = NULL;

4427:   if (type) {
4428:     while (next) {
4429:       PetscStrcasecmp(type,next->name,&flg);
4430:       if (flg) {
4431:         if (foundtype) *foundtype = PETSC_TRUE;
4432:         inext = next->handlers;
4433:         while (inext) {
4434:           PetscStrbeginswith(mtype,inext->mtype,&flg);
4435:           if (flg) {
4436:             if (foundmtype) *foundmtype = PETSC_TRUE;
4437:             if (createfactor)  *createfactor  = inext->createfactor[(int)ftype-1];
4438:             return(0);
4439:           }
4440:           inext = inext->next;
4441:         }
4442:       }
4443:       next = next->next;
4444:     }
4445:   } else {
4446:     while (next) {
4447:       inext = next->handlers;
4448:       while (inext) {
4449:         PetscStrbeginswith(mtype,inext->mtype,&flg);
4450:         if (flg && inext->createfactor[(int)ftype-1]) {
4451:           if (foundtype) *foundtype = PETSC_TRUE;
4452:           if (foundmtype)   *foundmtype   = PETSC_TRUE;
4453:           if (createfactor) *createfactor = inext->createfactor[(int)ftype-1];
4454:           return(0);
4455:         }
4456:         inext = inext->next;
4457:       }
4458:       next = next->next;
4459:     }
4460:   }
4461:   return(0);
4462: }

4464: PetscErrorCode MatSolverTypeDestroy(void)
4465: {
4466:   PetscErrorCode              ierr;
4467:   MatSolverTypeHolder         next = MatSolverTypeHolders,prev;
4468:   MatSolverTypeForSpecifcType inext,iprev;

4471:   while (next) {
4472:     PetscFree(next->name);
4473:     inext = next->handlers;
4474:     while (inext) {
4475:       PetscFree(inext->mtype);
4476:       iprev = inext;
4477:       inext = inext->next;
4478:       PetscFree(iprev);
4479:     }
4480:     prev = next;
4481:     next = next->next;
4482:     PetscFree(prev);
4483:   }
4484:   MatSolverTypeHolders = NULL;
4485:   return(0);
4486: }

4488: /*@C
4489:    MatFactorGetUseOrdering - Indicates if the factorization uses the ordering provided in MatLUFactorSymbolic(), MatCholeskyFactorSymbolic()

4491:    Logically Collective on Mat

4493:    Input Parameters:
4494: .  mat - the matrix

4496:    Output Parameters:
4497: .  flg - PETSC_TRUE if uses the ordering

4499:    Notes:
4500:       Most internal PETSc factorizations use the ordering past to the factorization routine but external
4501:       packages do no, thus we want to skip the ordering when it is not needed.

4503:    Level: developer

4505: .seealso: MatCopy(), MatDuplicate(), MatGetFactorAvailable(), MatGetFactor(), MatLUFactorSymbolic(), MatCholeskyFactorSymbolic()
4506: @*/
4507: PetscErrorCode MatFactorGetUseOrdering(Mat mat, PetscBool *flg)
4508: {
4510:   *flg = mat->useordering;
4511:   return(0);
4512: }

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

4517:    Collective on Mat

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

4524:    Output Parameters:
4525: .  f - the factor matrix used with MatXXFactorSymbolic() calls

4527:    Notes:
4528:       Some PETSc matrix formats have alternative solvers available that are contained in alternative packages
4529:      such as pastix, superlu, mumps etc.

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

4533:    Developer Notes:
4534:       This should actually be called MatCreateFactor() since it creates a new factor object

4536:    Level: intermediate

4538: .seealso: MatCopy(), MatDuplicate(), MatGetFactorAvailable(), MatFactorGetUseOrdering(), MatSolverTypeRegister()
4539: @*/
4540: PetscErrorCode MatGetFactor(Mat mat, MatSolverType type,MatFactorType ftype,Mat *f)
4541: {
4542:   PetscErrorCode ierr,(*conv)(Mat,MatFactorType,Mat*);
4543:   PetscBool      foundtype,foundmtype;


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

4552:   MatSolverTypeGet(type,((PetscObject)mat)->type_name,ftype,&foundtype,&foundmtype,&conv);
4553:   if (!foundtype) {
4554:     if (type) {
4555:       SETERRQ4(PetscObjectComm((PetscObject)mat),PETSC_ERR_MISSING_FACTOR,"Could not locate solver type %s for factorization type %s and matrix type %s. Perhaps you must ./configure with --download-%s",type,MatFactorTypes[ftype],((PetscObject)mat)->type_name,type);
4556:     } else {
4557:       SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_MISSING_FACTOR,"Could not locate a solver type for factorization type %s and matrix type %s.",MatFactorTypes[ftype],((PetscObject)mat)->type_name);
4558:     }
4559:   }
4560:   if (!foundmtype) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_MISSING_FACTOR,"MatSolverType %s does not support matrix type %s",type,((PetscObject)mat)->type_name);
4561:   if (!conv) SETERRQ3(PetscObjectComm((PetscObject)mat),PETSC_ERR_MISSING_FACTOR,"MatSolverType %s does not support factorization type %s for matrix type %s",type,MatFactorTypes[ftype],((PetscObject)mat)->type_name);

4563:   (*conv)(mat,ftype,f);
4564:   return(0);
4565: }

4567: /*@C
4568:    MatGetFactorAvailable - Returns a a flag if matrix supports particular type and factor type

4570:    Not Collective

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

4577:    Output Parameter:
4578: .    flg - PETSC_TRUE if the factorization is available

4580:    Notes:
4581:       Some PETSc matrix formats have alternative solvers available that are contained in alternative packages
4582:      such as pastix, superlu, mumps etc.

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

4586:    Developer Notes:
4587:       This should actually be called MatCreateFactorAvailable() since MatGetFactor() creates a new factor object

4589:    Level: intermediate

4591: .seealso: MatCopy(), MatDuplicate(), MatGetFactor(), MatSolverTypeRegister()
4592: @*/
4593: PetscErrorCode MatGetFactorAvailable(Mat mat, MatSolverType type,MatFactorType ftype,PetscBool  *flg)
4594: {
4595:   PetscErrorCode ierr, (*gconv)(Mat,MatFactorType,Mat*);


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

4604:   *flg = PETSC_FALSE;
4605:   MatSolverTypeGet(type,((PetscObject)mat)->type_name,ftype,NULL,NULL,&gconv);
4606:   if (gconv) {
4607:     *flg = PETSC_TRUE;
4608:   }
4609:   return(0);
4610: }

4612: #include <petscdmtypes.h>

4614: /*@
4615:    MatDuplicate - Duplicates a matrix including the non-zero structure.

4617:    Collective on Mat

4619:    Input Parameters:
4620: +  mat - the matrix
4621: -  op - One of MAT_DO_NOT_COPY_VALUES, MAT_COPY_VALUES, or MAT_SHARE_NONZERO_PATTERN.
4622:         See the manual page for MatDuplicateOption for an explanation of these options.

4624:    Output Parameter:
4625: .  M - pointer to place new matrix

4627:    Level: intermediate

4629:    Notes:
4630:     You cannot change the nonzero pattern for the parent or child matrix if you use MAT_SHARE_NONZERO_PATTERN.
4631:     When original mat is a product of matrix operation, e.g., an output of MatMatMult() or MatCreateSubMatrix(), only the simple matrix data structure of mat is duplicated and the internal data structures created for the reuse of previous matrix operations are not duplicated. User should not use MatDuplicate() to create new matrix M if M is intended to be reused as the product of matrix operation.

4633: .seealso: MatCopy(), MatConvert(), MatDuplicateOption
4634: @*/
4635: PetscErrorCode MatDuplicate(Mat mat,MatDuplicateOption op,Mat *M)
4636: {
4638:   Mat            B;
4639:   PetscInt       i;
4640:   DM             dm;
4641:   void           (*viewf)(void);

4647:   if (op == MAT_COPY_VALUES && !mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"MAT_COPY_VALUES not allowed for unassembled matrix");
4648:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
4649:   MatCheckPreallocated(mat,1);

4651:   *M = NULL;
4652:   if (!mat->ops->duplicate) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Not written for matrix type %s\n",((PetscObject)mat)->type_name);
4653:   PetscLogEventBegin(MAT_Convert,mat,0,0,0);
4654:   (*mat->ops->duplicate)(mat,op,M);
4655:   B    = *M;

4657:   MatGetOperation(mat,MATOP_VIEW,&viewf);
4658:   if (viewf) {
4659:     MatSetOperation(B,MATOP_VIEW,viewf);
4660:   }

4662:   B->stencil.dim = mat->stencil.dim;
4663:   B->stencil.noc = mat->stencil.noc;
4664:   for (i=0; i<=mat->stencil.dim; i++) {
4665:     B->stencil.dims[i]   = mat->stencil.dims[i];
4666:     B->stencil.starts[i] = mat->stencil.starts[i];
4667:   }

4669:   B->nooffproczerorows = mat->nooffproczerorows;
4670:   B->nooffprocentries  = mat->nooffprocentries;

4672:   PetscObjectQuery((PetscObject) mat, "__PETSc_dm", (PetscObject*) &dm);
4673:   if (dm) {
4674:     PetscObjectCompose((PetscObject) B, "__PETSc_dm", (PetscObject) dm);
4675:   }
4676:   PetscLogEventEnd(MAT_Convert,mat,0,0,0);
4677:   PetscObjectStateIncrease((PetscObject)B);
4678:   return(0);
4679: }

4681: /*@
4682:    MatGetDiagonal - Gets the diagonal of a matrix.

4684:    Logically Collective on Mat

4686:    Input Parameters:
4687: +  mat - the matrix
4688: -  v - the vector for storing the diagonal

4690:    Output Parameter:
4691: .  v - the diagonal of the matrix

4693:    Level: intermediate

4695:    Note:
4696:    Currently only correct in parallel for square matrices.

4698: .seealso: MatGetRow(), MatCreateSubMatrices(), MatCreateSubMatrix(), MatGetRowMaxAbs()
4699: @*/
4700: PetscErrorCode MatGetDiagonal(Mat mat,Vec v)
4701: {

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

4712:   (*mat->ops->getdiagonal)(mat,v);
4713:   PetscObjectStateIncrease((PetscObject)v);
4714:   return(0);
4715: }

4717: /*@C
4718:    MatGetRowMin - Gets the minimum value (of the real part) of each
4719:         row of the matrix

4721:    Logically Collective on Mat

4723:    Input Parameters:
4724: .  mat - the matrix

4726:    Output Parameter:
4727: +  v - the vector for storing the maximums
4728: -  idx - the indices of the column found for each row (optional)

4730:    Level: intermediate

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

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

4738: .seealso: MatGetDiagonal(), MatCreateSubMatrices(), MatCreateSubMatrix(), MatGetRowMaxAbs(),
4739:           MatGetRowMax()
4740: @*/
4741: PetscErrorCode MatGetRowMin(Mat mat,Vec v,PetscInt idx[])
4742: {

4749:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");

4751:   if (!mat->cmap->N) {
4752:     VecSet(v,PETSC_MAX_REAL);
4753:     if (idx) {
4754:       PetscInt i,m = mat->rmap->n;
4755:       for (i=0; i<m; i++) idx[i] = -1;
4756:     }
4757:   } else {
4758:     if (!mat->ops->getrowmin) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
4759:     MatCheckPreallocated(mat,1);
4760:   }
4761:   (*mat->ops->getrowmin)(mat,v,idx);
4762:   PetscObjectStateIncrease((PetscObject)v);
4763:   return(0);
4764: }

4766: /*@C
4767:    MatGetRowMinAbs - Gets the minimum value (in absolute value) of each
4768:         row of the matrix

4770:    Logically Collective on Mat

4772:    Input Parameters:
4773: .  mat - the matrix

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

4779:    Level: intermediate

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

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

4787: .seealso: MatGetDiagonal(), MatCreateSubMatrices(), MatCreateSubMatrix(), MatGetRowMax(), MatGetRowMaxAbs(), MatGetRowMin()
4788: @*/
4789: PetscErrorCode MatGetRowMinAbs(Mat mat,Vec v,PetscInt idx[])
4790: {

4797:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
4798:   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");

4800:   if (!mat->cmap->N) {
4801:     VecSet(v,0.0);
4802:     if (idx) {
4803:       PetscInt i,m = mat->rmap->n;
4804:       for (i=0; i<m; i++) idx[i] = -1;
4805:     }
4806:   } else {
4807:     if (!mat->ops->getrowminabs) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
4808:     MatCheckPreallocated(mat,1);
4809:     if (idx) {PetscArrayzero(idx,mat->rmap->n);}
4810:     (*mat->ops->getrowminabs)(mat,v,idx);
4811:   }
4812:   PetscObjectStateIncrease((PetscObject)v);
4813:   return(0);
4814: }

4816: /*@C
4817:    MatGetRowMax - Gets the maximum value (of the real part) of each
4818:         row of the matrix

4820:    Logically Collective on Mat

4822:    Input Parameters:
4823: .  mat - the matrix

4825:    Output Parameter:
4826: +  v - the vector for storing the maximums
4827: -  idx - the indices of the column found for each row (optional)

4829:    Level: intermediate

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

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

4837: .seealso: MatGetDiagonal(), MatCreateSubMatrices(), MatCreateSubMatrix(), MatGetRowMaxAbs(), MatGetRowMin()
4838: @*/
4839: PetscErrorCode MatGetRowMax(Mat mat,Vec v,PetscInt idx[])
4840: {

4847:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");

4849:   if (!mat->cmap->N) {
4850:     VecSet(v,PETSC_MIN_REAL);
4851:     if (idx) {
4852:       PetscInt i,m = mat->rmap->n;
4853:       for (i=0; i<m; i++) idx[i] = -1;
4854:     }
4855:   } else {
4856:     if (!mat->ops->getrowmax) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
4857:     MatCheckPreallocated(mat,1);
4858:     (*mat->ops->getrowmax)(mat,v,idx);
4859:   }
4860:   PetscObjectStateIncrease((PetscObject)v);
4861:   return(0);
4862: }

4864: /*@C
4865:    MatGetRowMaxAbs - Gets the maximum value (in absolute value) of each
4866:         row of the matrix

4868:    Logically Collective on Mat

4870:    Input Parameters:
4871: .  mat - the matrix

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

4877:    Level: intermediate

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

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

4885: .seealso: MatGetDiagonal(), MatCreateSubMatrices(), MatCreateSubMatrix(), MatGetRowMax(), MatGetRowMin()
4886: @*/
4887: PetscErrorCode MatGetRowMaxAbs(Mat mat,Vec v,PetscInt idx[])
4888: {

4895:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");

4897:   if (!mat->cmap->N) {
4898:     VecSet(v,0.0);
4899:     if (idx) {
4900:       PetscInt i,m = mat->rmap->n;
4901:       for (i=0; i<m; i++) idx[i] = -1;
4902:     }
4903:   } else {
4904:     if (!mat->ops->getrowmaxabs) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
4905:     MatCheckPreallocated(mat,1);
4906:     if (idx) {PetscArrayzero(idx,mat->rmap->n);}
4907:     (*mat->ops->getrowmaxabs)(mat,v,idx);
4908:   }
4909:   PetscObjectStateIncrease((PetscObject)v);
4910:   return(0);
4911: }

4913: /*@
4914:    MatGetRowSum - Gets the sum of each row of the matrix

4916:    Logically or Neighborhood Collective on Mat

4918:    Input Parameters:
4919: .  mat - the matrix

4921:    Output Parameter:
4922: .  v - the vector for storing the sum of rows

4924:    Level: intermediate

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

4929: .seealso: MatGetDiagonal(), MatCreateSubMatrices(), MatCreateSubMatrix(), MatGetRowMax(), MatGetRowMin()
4930: @*/
4931: PetscErrorCode MatGetRowSum(Mat mat, Vec v)
4932: {
4933:   Vec            ones;

4940:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
4941:   MatCheckPreallocated(mat,1);
4942:   MatCreateVecs(mat,&ones,NULL);
4943:   VecSet(ones,1.);
4944:   MatMult(mat,ones,v);
4945:   VecDestroy(&ones);
4946:   return(0);
4947: }

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

4952:    Collective on Mat

4954:    Input Parameter:
4955: +  mat - the matrix to transpose
4956: -  reuse - either MAT_INITIAL_MATRIX, MAT_REUSE_MATRIX, or MAT_INPLACE_MATRIX

4958:    Output Parameters:
4959: .  B - the transpose

4961:    Notes:
4962:      If you use MAT_INPLACE_MATRIX then you must pass in &mat for B

4964:      MAT_REUSE_MATRIX causes the B matrix from a previous call to this function with MAT_INITIAL_MATRIX to be used

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

4968:    Level: intermediate

4970: .seealso: MatMultTranspose(), MatMultTransposeAdd(), MatIsTranspose(), MatReuse
4971: @*/
4972: PetscErrorCode MatTranspose(Mat mat,MatReuse reuse,Mat *B)
4973: {

4979:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
4980:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
4981:   if (!mat->ops->transpose) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
4982:   if (reuse == MAT_INPLACE_MATRIX && mat != *B) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"MAT_INPLACE_MATRIX requires last matrix to match first");
4983:   if (reuse == MAT_REUSE_MATRIX && mat == *B) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Perhaps you mean MAT_INPLACE_MATRIX");
4984:   MatCheckPreallocated(mat,1);

4986:   PetscLogEventBegin(MAT_Transpose,mat,0,0,0);
4987:   (*mat->ops->transpose)(mat,reuse,B);
4988:   PetscLogEventEnd(MAT_Transpose,mat,0,0,0);
4989:   if (B) {PetscObjectStateIncrease((PetscObject)*B);}
4990:   return(0);
4991: }

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

4997:    Collective on Mat

4999:    Input Parameter:
5000: +  A - the matrix to test
5001: -  B - the matrix to test against, this can equal the first parameter

5003:    Output Parameters:
5004: .  flg - the result

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

5011:    Level: intermediate

5013: .seealso: MatTranspose(), MatIsSymmetric(), MatIsHermitian()
5014: @*/
5015: PetscErrorCode MatIsTranspose(Mat A,Mat B,PetscReal tol,PetscBool  *flg)
5016: {
5017:   PetscErrorCode ierr,(*f)(Mat,Mat,PetscReal,PetscBool*),(*g)(Mat,Mat,PetscReal,PetscBool*);

5023:   PetscObjectQueryFunction((PetscObject)A,"MatIsTranspose_C",&f);
5024:   PetscObjectQueryFunction((PetscObject)B,"MatIsTranspose_C",&g);
5025:   *flg = PETSC_FALSE;
5026:   if (f && g) {
5027:     if (f == g) {
5028:       (*f)(A,B,tol,flg);
5029:     } else SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_NOTSAMETYPE,"Matrices do not have the same comparator for symmetry test");
5030:   } else {
5031:     MatType mattype;
5032:     if (!f) {
5033:       MatGetType(A,&mattype);
5034:     } else {
5035:       MatGetType(B,&mattype);
5036:     }
5037:     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type %s does not support checking for transpose",mattype);
5038:   }
5039:   return(0);
5040: }

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

5045:    Collective on Mat

5047:    Input Parameter:
5048: +  mat - the matrix to transpose and complex conjugate
5049: -  reuse - MAT_INITIAL_MATRIX to create a new matrix, MAT_INPLACE_MATRIX to reuse the first argument to store the transpose

5051:    Output Parameters:
5052: .  B - the Hermitian

5054:    Level: intermediate

5056: .seealso: MatTranspose(), MatMultTranspose(), MatMultTransposeAdd(), MatIsTranspose(), MatReuse
5057: @*/
5058: PetscErrorCode MatHermitianTranspose(Mat mat,MatReuse reuse,Mat *B)
5059: {

5063:   MatTranspose(mat,reuse,B);
5064: #if defined(PETSC_USE_COMPLEX)
5065:   MatConjugate(*B);
5066: #endif
5067:   return(0);
5068: }

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

5073:    Collective on Mat

5075:    Input Parameter:
5076: +  A - the matrix to test
5077: -  B - the matrix to test against, this can equal the first parameter

5079:    Output Parameters:
5080: .  flg - the result

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

5087:    Level: intermediate

5089: .seealso: MatTranspose(), MatIsSymmetric(), MatIsHermitian(), MatIsTranspose()
5090: @*/
5091: PetscErrorCode MatIsHermitianTranspose(Mat A,Mat B,PetscReal tol,PetscBool  *flg)
5092: {
5093:   PetscErrorCode ierr,(*f)(Mat,Mat,PetscReal,PetscBool*),(*g)(Mat,Mat,PetscReal,PetscBool*);

5099:   PetscObjectQueryFunction((PetscObject)A,"MatIsHermitianTranspose_C",&f);
5100:   PetscObjectQueryFunction((PetscObject)B,"MatIsHermitianTranspose_C",&g);
5101:   if (f && g) {
5102:     if (f==g) {
5103:       (*f)(A,B,tol,flg);
5104:     } else SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_NOTSAMETYPE,"Matrices do not have the same comparator for Hermitian test");
5105:   }
5106:   return(0);
5107: }

5109: /*@
5110:    MatPermute - Creates a new matrix with rows and columns permuted from the
5111:    original.

5113:    Collective on Mat

5115:    Input Parameters:
5116: +  mat - the matrix to permute
5117: .  row - row permutation, each processor supplies only the permutation for its rows
5118: -  col - column permutation, each processor supplies only the permutation for its columns

5120:    Output Parameters:
5121: .  B - the permuted matrix

5123:    Level: advanced

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

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

5131: @*/
5132: PetscErrorCode MatPermute(Mat mat,IS row,IS col,Mat *B)
5133: {

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

5147:   (*mat->ops->permute)(mat,row,col,B);
5148:   PetscObjectStateIncrease((PetscObject)*B);
5149:   return(0);
5150: }

5152: /*@
5153:    MatEqual - Compares two matrices.

5155:    Collective on Mat

5157:    Input Parameters:
5158: +  A - the first matrix
5159: -  B - the second matrix

5161:    Output Parameter:
5162: .  flg - PETSC_TRUE if the matrices are equal; PETSC_FALSE otherwise.

5164:    Level: intermediate

5166: @*/
5167: PetscErrorCode MatEqual(Mat A,Mat B,PetscBool  *flg)
5168: {

5178:   MatCheckPreallocated(B,2);
5179:   if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
5180:   if (!B->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
5181:   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);
5182:   if (!A->ops->equal) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Mat type %s",((PetscObject)A)->type_name);
5183:   if (!B->ops->equal) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Mat type %s",((PetscObject)B)->type_name);
5184:   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);
5185:   MatCheckPreallocated(A,1);

5187:   (*A->ops->equal)(A,B,flg);
5188:   return(0);
5189: }

5191: /*@
5192:    MatDiagonalScale - Scales a matrix on the left and right by diagonal
5193:    matrices that are stored as vectors.  Either of the two scaling
5194:    matrices can be NULL.

5196:    Collective on Mat

5198:    Input Parameters:
5199: +  mat - the matrix to be scaled
5200: .  l - the left scaling vector (or NULL)
5201: -  r - the right scaling vector (or NULL)

5203:    Notes:
5204:    MatDiagonalScale() computes A = LAR, where
5205:    L = a diagonal matrix (stored as a vector), R = a diagonal matrix (stored as a vector)
5206:    The L scales the rows of the matrix, the R scales the columns of the matrix.

5208:    Level: intermediate


5211: .seealso: MatScale(), MatShift(), MatDiagonalSet()
5212: @*/
5213: PetscErrorCode MatDiagonalScale(Mat mat,Vec l,Vec r)
5214: {

5222:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
5223:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
5224:   MatCheckPreallocated(mat,1);
5225:   if (!l && !r) return(0);

5227:   if (!mat->ops->diagonalscale) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
5228:   PetscLogEventBegin(MAT_Scale,mat,0,0,0);
5229:   (*mat->ops->diagonalscale)(mat,l,r);
5230:   PetscLogEventEnd(MAT_Scale,mat,0,0,0);
5231:   PetscObjectStateIncrease((PetscObject)mat);
5232:   return(0);
5233: }

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

5238:     Logically Collective on Mat

5240:     Input Parameters:
5241: +   mat - the matrix to be scaled
5242: -   a  - the scaling value

5244:     Output Parameter:
5245: .   mat - the scaled matrix

5247:     Level: intermediate

5249: .seealso: MatDiagonalScale()
5250: @*/
5251: PetscErrorCode MatScale(Mat mat,PetscScalar a)
5252: {

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

5264:   PetscLogEventBegin(MAT_Scale,mat,0,0,0);
5265:   if (a != (PetscScalar)1.0) {
5266:     (*mat->ops->scale)(mat,a);
5267:     PetscObjectStateIncrease((PetscObject)mat);
5268:   }
5269:   PetscLogEventEnd(MAT_Scale,mat,0,0,0);
5270:   return(0);
5271: }

5273: /*@
5274:    MatNorm - Calculates various norms of a matrix.

5276:    Collective on Mat

5278:    Input Parameters:
5279: +  mat - the matrix
5280: -  type - the type of norm, NORM_1, NORM_FROBENIUS, NORM_INFINITY

5282:    Output Parameters:
5283: .  nrm - the resulting norm

5285:    Level: intermediate

5287: @*/
5288: PetscErrorCode MatNorm(Mat mat,NormType type,PetscReal *nrm)
5289: {


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

5302:   (*mat->ops->norm)(mat,type,nrm);
5303:   return(0);
5304: }

5306: /*
5307:      This variable is used to prevent counting of MatAssemblyBegin() that
5308:    are called from within a MatAssemblyEnd().
5309: */
5310: static PetscInt MatAssemblyEnd_InUse = 0;
5311: /*@
5312:    MatAssemblyBegin - Begins assembling the matrix.  This routine should
5313:    be called after completing all calls to MatSetValues().

5315:    Collective on Mat

5317:    Input Parameters:
5318: +  mat - the matrix
5319: -  type - type of assembly, either MAT_FLUSH_ASSEMBLY or MAT_FINAL_ASSEMBLY

5321:    Notes:
5322:    MatSetValues() generally caches the values.  The matrix is ready to
5323:    use only after MatAssemblyBegin() and MatAssemblyEnd() have been called.
5324:    Use MAT_FLUSH_ASSEMBLY when switching between ADD_VALUES and INSERT_VALUES
5325:    in MatSetValues(); use MAT_FINAL_ASSEMBLY for the final assembly before
5326:    using the matrix.

5328:    ALL processes that share a matrix MUST call MatAssemblyBegin() and MatAssemblyEnd() the SAME NUMBER of times, and each time with the
5329:    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
5330:    a global collective operation requring all processes that share the matrix.

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

5336:    Level: beginner

5338: .seealso: MatAssemblyEnd(), MatSetValues(), MatAssembled()
5339: @*/
5340: PetscErrorCode MatAssemblyBegin(Mat mat,MatAssemblyType type)
5341: {

5347:   MatCheckPreallocated(mat,1);
5348:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix.\nDid you forget to call MatSetUnfactored()?");
5349:   if (mat->assembled) {
5350:     mat->was_assembled = PETSC_TRUE;
5351:     mat->assembled     = PETSC_FALSE;
5352:   }

5354:   if (!MatAssemblyEnd_InUse) {
5355:     PetscLogEventBegin(MAT_AssemblyBegin,mat,0,0,0);
5356:     if (mat->ops->assemblybegin) {(*mat->ops->assemblybegin)(mat,type);}
5357:     PetscLogEventEnd(MAT_AssemblyBegin,mat,0,0,0);
5358:   } else if (mat->ops->assemblybegin) {
5359:     (*mat->ops->assemblybegin)(mat,type);
5360:   }
5361:   return(0);
5362: }

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

5368:    Not Collective

5370:    Input Parameter:
5371: .  mat - the matrix

5373:    Output Parameter:
5374: .  assembled - PETSC_TRUE or PETSC_FALSE

5376:    Level: advanced

5378: .seealso: MatAssemblyEnd(), MatSetValues(), MatAssemblyBegin()
5379: @*/
5380: PetscErrorCode MatAssembled(Mat mat,PetscBool  *assembled)
5381: {
5385:   *assembled = mat->assembled;
5386:   return(0);
5387: }

5389: /*@
5390:    MatAssemblyEnd - Completes assembling the matrix.  This routine should
5391:    be called after MatAssemblyBegin().

5393:    Collective on Mat

5395:    Input Parameters:
5396: +  mat - the matrix
5397: -  type - type of assembly, either MAT_FLUSH_ASSEMBLY or MAT_FINAL_ASSEMBLY

5399:    Options Database Keys:
5400: +  -mat_view ::ascii_info - Prints info on matrix at conclusion of MatEndAssembly()
5401: .  -mat_view ::ascii_info_detail - Prints more detailed info
5402: .  -mat_view - Prints matrix in ASCII format
5403: .  -mat_view ::ascii_matlab - Prints matrix in Matlab format
5404: .  -mat_view draw - PetscDraws nonzero structure of matrix, using MatView() and PetscDrawOpenX().
5405: .  -display <name> - Sets display name (default is host)
5406: .  -draw_pause <sec> - Sets number of seconds to pause after display
5407: .  -mat_view socket - Sends matrix to socket, can be accessed from Matlab (See Users-Manual: ch_matlab)
5408: .  -viewer_socket_machine <machine> - Machine to use for socket
5409: .  -viewer_socket_port <port> - Port number to use for socket
5410: -  -mat_view binary:filename[:append] - Save matrix to file in binary format

5412:    Notes:
5413:    MatSetValues() generally caches the values.  The matrix is ready to
5414:    use only after MatAssemblyBegin() and MatAssemblyEnd() have been called.
5415:    Use MAT_FLUSH_ASSEMBLY when switching between ADD_VALUES and INSERT_VALUES
5416:    in MatSetValues(); use MAT_FINAL_ASSEMBLY for the final assembly before
5417:    using the matrix.

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

5423:    Level: beginner

5425: .seealso: MatAssemblyBegin(), MatSetValues(), PetscDrawOpenX(), PetscDrawCreate(), MatView(), MatAssembled(), PetscViewerSocketOpen()
5426: @*/
5427: PetscErrorCode MatAssemblyEnd(Mat mat,MatAssemblyType type)
5428: {
5429:   PetscErrorCode  ierr;
5430:   static PetscInt inassm = 0;
5431:   PetscBool       flg    = PETSC_FALSE;


5437:   inassm++;
5438:   MatAssemblyEnd_InUse++;
5439:   if (MatAssemblyEnd_InUse == 1) { /* Do the logging only the first time through */
5440:     PetscLogEventBegin(MAT_AssemblyEnd,mat,0,0,0);
5441:     if (mat->ops->assemblyend) {
5442:       (*mat->ops->assemblyend)(mat,type);
5443:     }
5444:     PetscLogEventEnd(MAT_AssemblyEnd,mat,0,0,0);
5445:   } else if (mat->ops->assemblyend) {
5446:     (*mat->ops->assemblyend)(mat,type);
5447:   }

5449:   /* Flush assembly is not a true assembly */
5450:   if (type != MAT_FLUSH_ASSEMBLY) {
5451:     mat->num_ass++;
5452:     mat->assembled        = PETSC_TRUE;
5453:     mat->ass_nonzerostate = mat->nonzerostate;
5454:   }

5456:   mat->insertmode = NOT_SET_VALUES;
5457:   MatAssemblyEnd_InUse--;
5458:   PetscObjectStateIncrease((PetscObject)mat);
5459:   if (!mat->symmetric_eternal) {
5460:     mat->symmetric_set              = PETSC_FALSE;
5461:     mat->hermitian_set              = PETSC_FALSE;
5462:     mat->structurally_symmetric_set = PETSC_FALSE;
5463:   }
5464:   if (inassm == 1 && type != MAT_FLUSH_ASSEMBLY) {
5465:     MatViewFromOptions(mat,NULL,"-mat_view");

5467:     if (mat->checksymmetryonassembly) {
5468:       MatIsSymmetric(mat,mat->checksymmetrytol,&flg);
5469:       if (flg) {
5470:         PetscPrintf(PetscObjectComm((PetscObject)mat),"Matrix is symmetric (tolerance %g)\n",(double)mat->checksymmetrytol);
5471:       } else {
5472:         PetscPrintf(PetscObjectComm((PetscObject)mat),"Matrix is not symmetric (tolerance %g)\n",(double)mat->checksymmetrytol);
5473:       }
5474:     }
5475:     if (mat->nullsp && mat->checknullspaceonassembly) {
5476:       MatNullSpaceTest(mat->nullsp,mat,NULL);
5477:     }
5478:   }
5479:   inassm--;
5480:   return(0);
5481: }

5483: /*@
5484:    MatSetOption - Sets a parameter option for a matrix. Some options
5485:    may be specific to certain storage formats.  Some options
5486:    determine how values will be inserted (or added). Sorted,
5487:    row-oriented input will generally assemble the fastest. The default
5488:    is row-oriented.

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

5492:    Input Parameters:
5493: +  mat - the matrix
5494: .  option - the option, one of those listed below (and possibly others),
5495: -  flg - turn the option on (PETSC_TRUE) or off (PETSC_FALSE)

5497:   Options Describing Matrix Structure:
5498: +    MAT_SPD - symmetric positive definite
5499: .    MAT_SYMMETRIC - symmetric in terms of both structure and value
5500: .    MAT_HERMITIAN - transpose is the complex conjugation
5501: .    MAT_STRUCTURALLY_SYMMETRIC - symmetric nonzero structure
5502: -    MAT_SYMMETRY_ETERNAL - if you would like the symmetry/Hermitian flag
5503:                             you set to be kept with all future use of the matrix
5504:                             including after MatAssemblyBegin/End() which could
5505:                             potentially change the symmetry structure, i.e. you
5506:                             KNOW the matrix will ALWAYS have the property you set.
5507:                             Note that setting this flag alone implies nothing about whether the matrix is symmetric/Hermitian;
5508:                             the relevant flags must be set independently.


5511:    Options For Use with MatSetValues():
5512:    Insert a logically dense subblock, which can be
5513: .    MAT_ROW_ORIENTED - row-oriented (default)

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

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

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

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

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

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

5551:    MAT_NEW_NONZERO_LOCATION_ERR set to PETSC_TRUE indicates that any add or insertion
5552:    that would generate a new entry in the nonzero structure instead produces
5553:    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

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

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

5567:    MAT_USE_HASH_TABLE indicates that a hash table be used to improve the
5568:    searches during matrix assembly. When this flag is set, the hash table
5569:    is created during the first Matrix Assembly. This hash table is
5570:    used the next time through, during MatSetVaules()/MatSetVaulesBlocked()
5571:    to improve the searching of indices. MAT_NEW_NONZERO_LOCATIONS flag
5572:    should be used with MAT_USE_HASH_TABLE flag. This option is currently
5573:    supported by MATMPIBAIJ format only.

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

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

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

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

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

5589:    MAT_SORTED_FULL - each process provides exactly its local rows; all column indices for a given row are passed in a
5590:                      single call to MatSetValues(), preallocation is perfect, row oriented, INSERT_VALUES is used. Common
5591:                      with finite difference schemes with non-periodic boundary conditions.
5592:    Notes:
5593:     Can only be called after MatSetSizes() and MatSetType() have been set.

5595:    Level: intermediate

5597: .seealso:  MatOption, Mat

5599: @*/
5600: PetscErrorCode MatSetOption(Mat mat,MatOption op,PetscBool flg)
5601: {

5607:   if (op > 0) {
5610:   }

5612:   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);
5613:   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()");

5615:   switch (op) {
5616:   case MAT_NO_OFF_PROC_ENTRIES:
5617:     mat->nooffprocentries = flg;
5618:     return(0);
5619:   case MAT_SUBSET_OFF_PROC_ENTRIES:
5620:     mat->assembly_subset = flg;
5621:     if (!mat->assembly_subset) { /* See the same logic in VecAssembly wrt VEC_SUBSET_OFF_PROC_ENTRIES */
5622: #if !defined(PETSC_HAVE_MPIUNI)
5623:       MatStashScatterDestroy_BTS(&mat->stash);
5624: #endif
5625:       mat->stash.first_assembly_done = PETSC_FALSE;
5626:     }
5627:     return(0);
5628:   case MAT_NO_OFF_PROC_ZERO_ROWS:
5629:     mat->nooffproczerorows = flg;
5630:     return(0);
5631:   case MAT_SPD:
5632:     mat->spd_set = PETSC_TRUE;
5633:     mat->spd     = flg;
5634:     if (flg) {
5635:       mat->symmetric                  = PETSC_TRUE;
5636:       mat->structurally_symmetric     = PETSC_TRUE;
5637:       mat->symmetric_set              = PETSC_TRUE;
5638:       mat->structurally_symmetric_set = PETSC_TRUE;
5639:     }
5640:     break;
5641:   case MAT_SYMMETRIC:
5642:     mat->symmetric = flg;
5643:     if (flg) mat->structurally_symmetric = PETSC_TRUE;
5644:     mat->symmetric_set              = PETSC_TRUE;
5645:     mat->structurally_symmetric_set = flg;
5646: #if !defined(PETSC_USE_COMPLEX)
5647:     mat->hermitian     = flg;
5648:     mat->hermitian_set = PETSC_TRUE;
5649: #endif
5650:     break;
5651:   case MAT_HERMITIAN:
5652:     mat->hermitian = flg;
5653:     if (flg) mat->structurally_symmetric = PETSC_TRUE;
5654:     mat->hermitian_set              = PETSC_TRUE;
5655:     mat->structurally_symmetric_set = flg;
5656: #if !defined(PETSC_USE_COMPLEX)
5657:     mat->symmetric     = flg;
5658:     mat->symmetric_set = PETSC_TRUE;
5659: #endif
5660:     break;
5661:   case MAT_STRUCTURALLY_SYMMETRIC:
5662:     mat->structurally_symmetric     = flg;
5663:     mat->structurally_symmetric_set = PETSC_TRUE;
5664:     break;
5665:   case MAT_SYMMETRY_ETERNAL:
5666:     mat->symmetric_eternal = flg;
5667:     break;
5668:   case MAT_STRUCTURE_ONLY:
5669:     mat->structure_only = flg;
5670:     break;
5671:   case MAT_SORTED_FULL:
5672:     mat->sortedfull = flg;
5673:     break;
5674:   default:
5675:     break;
5676:   }
5677:   if (mat->ops->setoption) {
5678:     (*mat->ops->setoption)(mat,op,flg);
5679:   }
5680:   return(0);
5681: }

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

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

5688:    Input Parameters:
5689: +  mat - the matrix
5690: -  option - the option, this only responds to certain options, check the code for which ones

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

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

5698:    Level: intermediate

5700: .seealso:  MatOption, MatSetOption()

5702: @*/
5703: PetscErrorCode MatGetOption(Mat mat,MatOption op,PetscBool *flg)
5704: {

5709:   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);
5710:   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()");

5712:   switch (op) {
5713:   case MAT_NO_OFF_PROC_ENTRIES:
5714:     *flg = mat->nooffprocentries;
5715:     break;
5716:   case MAT_NO_OFF_PROC_ZERO_ROWS:
5717:     *flg = mat->nooffproczerorows;
5718:     break;
5719:   case MAT_SYMMETRIC:
5720:     *flg = mat->symmetric;
5721:     break;
5722:   case MAT_HERMITIAN:
5723:     *flg = mat->hermitian;
5724:     break;
5725:   case MAT_STRUCTURALLY_SYMMETRIC:
5726:     *flg = mat->structurally_symmetric;
5727:     break;
5728:   case MAT_SYMMETRY_ETERNAL:
5729:     *flg = mat->symmetric_eternal;
5730:     break;
5731:   case MAT_SPD:
5732:     *flg = mat->spd;
5733:     break;
5734:   default:
5735:     break;
5736:   }
5737:   return(0);
5738: }

5740: /*@
5741:    MatZeroEntries - Zeros all entries of a matrix.  For sparse matrices
5742:    this routine retains the old nonzero structure.

5744:    Logically Collective on Mat

5746:    Input Parameters:
5747: .  mat - the matrix

5749:    Level: intermediate

5751:    Notes:
5752:     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.
5753:    See the Performance chapter of the users manual for information on preallocating matrices.

5755: .seealso: MatZeroRows()
5756: @*/
5757: PetscErrorCode MatZeroEntries(Mat mat)
5758: {

5764:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
5765:   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");
5766:   if (!mat->ops->zeroentries) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
5767:   MatCheckPreallocated(mat,1);

5769:   PetscLogEventBegin(MAT_ZeroEntries,mat,0,0,0);
5770:   (*mat->ops->zeroentries)(mat);
5771:   PetscLogEventEnd(MAT_ZeroEntries,mat,0,0,0);
5772:   PetscObjectStateIncrease((PetscObject)mat);
5773:   return(0);
5774: }

5776: /*@
5777:    MatZeroRowsColumns - Zeros all entries (except possibly the main diagonal)
5778:    of a set of rows and columns of a matrix.

5780:    Collective on Mat

5782:    Input Parameters:
5783: +  mat - the matrix
5784: .  numRows - the number of rows to remove
5785: .  rows - the global row indices
5786: .  diag - value put in all diagonals of eliminated rows (0.0 will even eliminate diagonal entry)
5787: .  x - optional vector of solutions for zeroed rows (other entries in vector are not used)
5788: -  b - optional vector of right hand side, that will be adjusted by provided solution

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

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

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

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

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

5807:    Level: intermediate

5809: .seealso: MatZeroRowsIS(), MatZeroRows(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(),
5810:           MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil()
5811: @*/
5812: PetscErrorCode MatZeroRowsColumns(Mat mat,PetscInt numRows,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
5813: {

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

5825:   (*mat->ops->zerorowscolumns)(mat,numRows,rows,diag,x,b);
5826:   MatViewFromOptions(mat,NULL,"-mat_view");
5827:   PetscObjectStateIncrease((PetscObject)mat);
5828:   return(0);
5829: }

5831: /*@
5832:    MatZeroRowsColumnsIS - Zeros all entries (except possibly the main diagonal)
5833:    of a set of rows and columns of a matrix.

5835:    Collective on Mat

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

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

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

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

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

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

5861:    Level: intermediate

5863: .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(),
5864:           MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRows(), MatZeroRowsColumnsStencil()
5865: @*/
5866: PetscErrorCode MatZeroRowsColumnsIS(Mat mat,IS is,PetscScalar diag,Vec x,Vec b)
5867: {
5869:   PetscInt       numRows;
5870:   const PetscInt *rows;

5877:   ISGetLocalSize(is,&numRows);
5878:   ISGetIndices(is,&rows);
5879:   MatZeroRowsColumns(mat,numRows,rows,diag,x,b);
5880:   ISRestoreIndices(is,&rows);
5881:   return(0);
5882: }

5884: /*@
5885:    MatZeroRows - Zeros all entries (except possibly the main diagonal)
5886:    of a set of rows of a matrix.

5888:    Collective on Mat

5890:    Input Parameters:
5891: +  mat - the matrix
5892: .  numRows - the number of rows to remove
5893: .  rows - the global row indices
5894: .  diag - value put in all diagonals of eliminated rows (0.0 will even eliminate diagonal entry)
5895: .  x - optional vector of solutions for zeroed rows (other entries in vector are not used)
5896: -  b - optional vector of right hand side, that will be adjusted by provided solution

5898:    Notes:
5899:    For the AIJ and BAIJ matrix formats this removes the old nonzero structure,
5900:    but does not release memory.  For the dense and block diagonal
5901:    formats this does not alter the nonzero structure.

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

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

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

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

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

5922:    Level: intermediate

5924: .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(),
5925:           MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil()
5926: @*/
5927: PetscErrorCode MatZeroRows(Mat mat,PetscInt numRows,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
5928: {

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

5940:   (*mat->ops->zerorows)(mat,numRows,rows,diag,x,b);
5941:   MatViewFromOptions(mat,NULL,"-mat_view");
5942:   PetscObjectStateIncrease((PetscObject)mat);
5943:   return(0);
5944: }

5946: /*@
5947:    MatZeroRowsIS - Zeros all entries (except possibly the main diagonal)
5948:    of a set of rows of a matrix.

5950:    Collective on Mat

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

5959:    Notes:
5960:    For the AIJ and BAIJ matrix formats this removes the old nonzero structure,
5961:    but does not release memory.  For the dense and block diagonal
5962:    formats this does not alter the nonzero structure.

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

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

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

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

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

5983:    Level: intermediate

5985: .seealso: MatZeroRows(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(),
5986:           MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil()
5987: @*/
5988: PetscErrorCode MatZeroRowsIS(Mat mat,IS is,PetscScalar diag,Vec x,Vec b)
5989: {
5990:   PetscInt       numRows;
5991:   const PetscInt *rows;

5998:   ISGetLocalSize(is,&numRows);
5999:   ISGetIndices(is,&rows);
6000:   MatZeroRows(mat,numRows,rows,diag,x,b);
6001:   ISRestoreIndices(is,&rows);
6002:   return(0);
6003: }

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

6009:    Collective on Mat

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

6019:    Notes:
6020:    For the AIJ and BAIJ matrix formats this removes the old nonzero structure,
6021:    but does not release memory.  For the dense and block diagonal
6022:    formats this does not alter the nonzero structure.

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

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

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

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

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

6042:    In Fortran idxm and idxn should be declared as
6043: $     MatStencil idxm(4,m)
6044:    and the values inserted using
6045: $    idxm(MatStencil_i,1) = i
6046: $    idxm(MatStencil_j,1) = j
6047: $    idxm(MatStencil_k,1) = k
6048: $    idxm(MatStencil_c,1) = c
6049:    etc

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

6056:    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
6057:    a single value per point) you can skip filling those indices.

6059:    Level: intermediate

6061: .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsl(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(),
6062:           MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil()
6063: @*/
6064: PetscErrorCode MatZeroRowsStencil(Mat mat,PetscInt numRows,const MatStencil rows[],PetscScalar diag,Vec x,Vec b)
6065: {
6066:   PetscInt       dim     = mat->stencil.dim;
6067:   PetscInt       sdim    = dim - (1 - (PetscInt) mat->stencil.noc);
6068:   PetscInt       *dims   = mat->stencil.dims+1;
6069:   PetscInt       *starts = mat->stencil.starts;
6070:   PetscInt       *dxm    = (PetscInt*) rows;
6071:   PetscInt       *jdxm, i, j, tmp, numNewRows = 0;


6079:   PetscMalloc1(numRows, &jdxm);
6080:   for (i = 0; i < numRows; ++i) {
6081:     /* Skip unused dimensions (they are ordered k, j, i, c) */
6082:     for (j = 0; j < 3-sdim; ++j) dxm++;
6083:     /* Local index in X dir */
6084:     tmp = *dxm++ - starts[0];
6085:     /* Loop over remaining dimensions */
6086:     for (j = 0; j < dim-1; ++j) {
6087:       /* If nonlocal, set index to be negative */
6088:       if ((*dxm++ - starts[j+1]) < 0 || tmp < 0) tmp = PETSC_MIN_INT;
6089:       /* Update local index */
6090:       else tmp = tmp*dims[j] + *(dxm-1) - starts[j+1];
6091:     }
6092:     /* Skip component slot if necessary */
6093:     if (mat->stencil.noc) dxm++;
6094:     /* Local row number */
6095:     if (tmp >= 0) {
6096:       jdxm[numNewRows++] = tmp;
6097:     }
6098:   }
6099:   MatZeroRowsLocal(mat,numNewRows,jdxm,diag,x,b);
6100:   PetscFree(jdxm);
6101:   return(0);
6102: }

6104: /*@
6105:    MatZeroRowsColumnsStencil - Zeros all row and column entries (except possibly the main diagonal)
6106:    of a set of rows and columns of a matrix.

6108:    Collective on Mat

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

6118:    Notes:
6119:    For the AIJ and BAIJ matrix formats this removes the old nonzero structure,
6120:    but does not release memory.  For the dense and block diagonal
6121:    formats this does not alter the nonzero structure.

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

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

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

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

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

6141:    In Fortran idxm and idxn should be declared as
6142: $     MatStencil idxm(4,m)
6143:    and the values inserted using
6144: $    idxm(MatStencil_i,1) = i
6145: $    idxm(MatStencil_j,1) = j
6146: $    idxm(MatStencil_k,1) = k
6147: $    idxm(MatStencil_c,1) = c
6148:    etc

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

6155:    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
6156:    a single value per point) you can skip filling those indices.

6158:    Level: intermediate

6160: .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(),
6161:           MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRows()
6162: @*/
6163: PetscErrorCode MatZeroRowsColumnsStencil(Mat mat,PetscInt numRows,const MatStencil rows[],PetscScalar diag,Vec x,Vec b)
6164: {
6165:   PetscInt       dim     = mat->stencil.dim;
6166:   PetscInt       sdim    = dim - (1 - (PetscInt) mat->stencil.noc);
6167:   PetscInt       *dims   = mat->stencil.dims+1;
6168:   PetscInt       *starts = mat->stencil.starts;
6169:   PetscInt       *dxm    = (PetscInt*) rows;
6170:   PetscInt       *jdxm, i, j, tmp, numNewRows = 0;


6178:   PetscMalloc1(numRows, &jdxm);
6179:   for (i = 0; i < numRows; ++i) {
6180:     /* Skip unused dimensions (they are ordered k, j, i, c) */
6181:     for (j = 0; j < 3-sdim; ++j) dxm++;
6182:     /* Local index in X dir */
6183:     tmp = *dxm++ - starts[0];
6184:     /* Loop over remaining dimensions */
6185:     for (j = 0; j < dim-1; ++j) {
6186:       /* If nonlocal, set index to be negative */
6187:       if ((*dxm++ - starts[j+1]) < 0 || tmp < 0) tmp = PETSC_MIN_INT;
6188:       /* Update local index */
6189:       else tmp = tmp*dims[j] + *(dxm-1) - starts[j+1];
6190:     }
6191:     /* Skip component slot if necessary */
6192:     if (mat->stencil.noc) dxm++;
6193:     /* Local row number */
6194:     if (tmp >= 0) {
6195:       jdxm[numNewRows++] = tmp;
6196:     }
6197:   }
6198:   MatZeroRowsColumnsLocal(mat,numNewRows,jdxm,diag,x,b);
6199:   PetscFree(jdxm);
6200:   return(0);
6201: }

6203: /*@C
6204:    MatZeroRowsLocal - Zeros all entries (except possibly the main diagonal)
6205:    of a set of rows of a matrix; using local numbering of rows.

6207:    Collective on Mat

6209:    Input Parameters:
6210: +  mat - the matrix
6211: .  numRows - the number of rows to remove
6212: .  rows - the global row indices
6213: .  diag - value put in all diagonals of eliminated rows
6214: .  x - optional vector of solutions for zeroed rows (other entries in vector are not used)
6215: -  b - optional vector of right hand side, that will be adjusted by provided solution

6217:    Notes:
6218:    Before calling MatZeroRowsLocal(), the user must first set the
6219:    local-to-global mapping by calling MatSetLocalToGlobalMapping().

6221:    For the AIJ matrix formats this removes the old nonzero structure,
6222:    but does not release memory.  For the dense and block diagonal
6223:    formats this does not alter the nonzero structure.

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

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

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

6236:    Level: intermediate

6238: .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRows(), MatSetOption(),
6239:           MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil()
6240: @*/
6241: PetscErrorCode MatZeroRowsLocal(Mat mat,PetscInt numRows,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
6242: {

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

6253:   if (mat->ops->zerorowslocal) {
6254:     (*mat->ops->zerorowslocal)(mat,numRows,rows,diag,x,b);
6255:   } else {
6256:     IS             is, newis;
6257:     const PetscInt *newRows;

6259:     if (!mat->rmap->mapping) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Need to provide local to global mapping to matrix first");
6260:     ISCreateGeneral(PETSC_COMM_SELF,numRows,rows,PETSC_COPY_VALUES,&is);
6261:     ISLocalToGlobalMappingApplyIS(mat->rmap->mapping,is,&newis);
6262:     ISGetIndices(newis,&newRows);
6263:     (*mat->ops->zerorows)(mat,numRows,newRows,diag,x,b);
6264:     ISRestoreIndices(newis,&newRows);
6265:     ISDestroy(&newis);
6266:     ISDestroy(&is);
6267:   }
6268:   PetscObjectStateIncrease((PetscObject)mat);
6269:   return(0);
6270: }

6272: /*@
6273:    MatZeroRowsLocalIS - Zeros all entries (except possibly the main diagonal)
6274:    of a set of rows of a matrix; using local numbering of rows.

6276:    Collective on Mat

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

6285:    Notes:
6286:    Before calling MatZeroRowsLocalIS(), the user must first set the
6287:    local-to-global mapping by calling MatSetLocalToGlobalMapping().

6289:    For the AIJ matrix formats this removes the old nonzero structure,
6290:    but does not release memory.  For the dense and block diagonal
6291:    formats this does not alter the nonzero structure.

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

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

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

6304:    Level: intermediate

6306: .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRows(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(),
6307:           MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil()
6308: @*/
6309: PetscErrorCode MatZeroRowsLocalIS(Mat mat,IS is,PetscScalar diag,Vec x,Vec b)
6310: {
6312:   PetscInt       numRows;
6313:   const PetscInt *rows;

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

6323:   ISGetLocalSize(is,&numRows);
6324:   ISGetIndices(is,&rows);
6325:   MatZeroRowsLocal(mat,numRows,rows,diag,x,b);
6326:   ISRestoreIndices(is,&rows);
6327:   return(0);
6328: }

6330: /*@
6331:    MatZeroRowsColumnsLocal - Zeros all entries (except possibly the main diagonal)
6332:    of a set of rows and columns of a matrix; using local numbering of rows.

6334:    Collective on Mat

6336:    Input Parameters:
6337: +  mat - the matrix
6338: .  numRows - the number of rows to remove
6339: .  rows - the global row indices
6340: .  diag - value put in all diagonals of eliminated rows
6341: .  x - optional vector of solutions for zeroed rows (other entries in vector are not used)
6342: -  b - optional vector of right hand side, that will be adjusted by provided solution

6344:    Notes:
6345:    Before calling MatZeroRowsColumnsLocal(), the user must first set the
6346:    local-to-global mapping by calling MatSetLocalToGlobalMapping().

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

6352:    Level: intermediate

6354: .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(),
6355:           MatZeroRows(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil()
6356: @*/
6357: PetscErrorCode MatZeroRowsColumnsLocal(Mat mat,PetscInt numRows,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
6358: {
6360:   IS             is, newis;
6361:   const PetscInt *newRows;

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

6371:   if (!mat->cmap->mapping) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Need to provide local to global mapping to matrix first");
6372:   ISCreateGeneral(PETSC_COMM_SELF,numRows,rows,PETSC_COPY_VALUES,&is);
6373:   ISLocalToGlobalMappingApplyIS(mat->cmap->mapping,is,&newis);
6374:   ISGetIndices(newis,&newRows);
6375:   (*mat->ops->zerorowscolumns)(mat,numRows,newRows,diag,x,b);
6376:   ISRestoreIndices(newis,&newRows);
6377:   ISDestroy(&newis);
6378:   ISDestroy(&is);
6379:   PetscObjectStateIncrease((PetscObject)mat);
6380:   return(0);
6381: }

6383: /*@
6384:    MatZeroRowsColumnsLocalIS - Zeros all entries (except possibly the main diagonal)
6385:    of a set of rows and columns of a matrix; using local numbering of rows.

6387:    Collective on Mat

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

6396:    Notes:
6397:    Before calling MatZeroRowsColumnsLocalIS(), the user must first set the
6398:    local-to-global mapping by calling MatSetLocalToGlobalMapping().

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

6404:    Level: intermediate

6406: .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(),
6407:           MatZeroRowsColumnsLocal(), MatZeroRows(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil()
6408: @*/
6409: PetscErrorCode MatZeroRowsColumnsLocalIS(Mat mat,IS is,PetscScalar diag,Vec x,Vec b)
6410: {
6412:   PetscInt       numRows;
6413:   const PetscInt *rows;

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

6423:   ISGetLocalSize(is,&numRows);
6424:   ISGetIndices(is,&rows);
6425:   MatZeroRowsColumnsLocal(mat,numRows,rows,diag,x,b);
6426:   ISRestoreIndices(is,&rows);
6427:   return(0);
6428: }

6430: /*@C
6431:    MatGetSize - Returns the numbers of rows and columns in a matrix.

6433:    Not Collective

6435:    Input Parameter:
6436: .  mat - the matrix

6438:    Output Parameters:
6439: +  m - the number of global rows
6440: -  n - the number of global columns

6442:    Note: both output parameters can be NULL on input.

6444:    Level: beginner

6446: .seealso: MatGetLocalSize()
6447: @*/
6448: PetscErrorCode MatGetSize(Mat mat,PetscInt *m,PetscInt *n)
6449: {
6452:   if (m) *m = mat->rmap->N;
6453:   if (n) *n = mat->cmap->N;
6454:   return(0);
6455: }

6457: /*@C
6458:    MatGetLocalSize - Returns the number of local rows and local columns
6459:    of a matrix, that is the local size of the left and right vectors as returned by MatCreateVecs().

6461:    Not Collective

6463:    Input Parameters:
6464: .  mat - the matrix

6466:    Output Parameters:
6467: +  m - the number of local rows
6468: -  n - the number of local columns

6470:    Note: both output parameters can be NULL on input.

6472:    Level: beginner

6474: .seealso: MatGetSize()
6475: @*/
6476: PetscErrorCode MatGetLocalSize(Mat mat,PetscInt *m,PetscInt *n)
6477: {
6482:   if (m) *m = mat->rmap->n;
6483:   if (n) *n = mat->cmap->n;
6484:   return(0);
6485: }

6487: /*@C
6488:    MatGetOwnershipRangeColumn - Returns the range of matrix columns associated with rows of a vector one multiplies by that owned by
6489:    this processor. (The columns of the "diagonal block")

6491:    Not Collective, unless matrix has not been allocated, then collective on Mat

6493:    Input Parameters:
6494: .  mat - the matrix

6496:    Output Parameters:
6497: +  m - the global index of the first local column
6498: -  n - one more than the global index of the last local column

6500:    Notes:
6501:     both output parameters can be NULL on input.

6503:    Level: developer

6505: .seealso:  MatGetOwnershipRange(), MatGetOwnershipRanges(), MatGetOwnershipRangesColumn()

6507: @*/
6508: PetscErrorCode MatGetOwnershipRangeColumn(Mat mat,PetscInt *m,PetscInt *n)
6509: {
6515:   MatCheckPreallocated(mat,1);
6516:   if (m) *m = mat->cmap->rstart;
6517:   if (n) *n = mat->cmap->rend;
6518:   return(0);
6519: }

6521: /*@C
6522:    MatGetOwnershipRange - Returns the range of matrix rows owned by
6523:    this processor, assuming that the matrix is laid out with the first
6524:    n1 rows on the first processor, the next n2 rows on the second, etc.
6525:    For certain parallel layouts this range may not be well defined.

6527:    Not Collective

6529:    Input Parameters:
6530: .  mat - the matrix

6532:    Output Parameters:
6533: +  m - the global index of the first local row
6534: -  n - one more than the global index of the last local row

6536:    Note: Both output parameters can be NULL on input.
6537: $  This function requires that the matrix be preallocated. If you have not preallocated, consider using
6538: $    PetscSplitOwnership(MPI_Comm comm, PetscInt *n, PetscInt *N)
6539: $  and then MPI_Scan() to calculate prefix sums of the local sizes.

6541:    Level: beginner

6543: .seealso:   MatGetOwnershipRanges(), MatGetOwnershipRangeColumn(), MatGetOwnershipRangesColumn(), PetscSplitOwnership(), PetscSplitOwnershipBlock()

6545: @*/
6546: PetscErrorCode MatGetOwnershipRange(Mat mat,PetscInt *m,PetscInt *n)
6547: {
6553:   MatCheckPreallocated(mat,1);
6554:   if (m) *m = mat->rmap->rstart;
6555:   if (n) *n = mat->rmap->rend;
6556:   return(0);
6557: }

6559: /*@C
6560:    MatGetOwnershipRanges - Returns the range of matrix rows owned by
6561:    each process

6563:    Not Collective, unless matrix has not been allocated, then collective on Mat

6565:    Input Parameters:
6566: .  mat - the matrix

6568:    Output Parameters:
6569: .  ranges - start of each processors portion plus one more than the total length at the end

6571:    Level: beginner

6573: .seealso:   MatGetOwnershipRange(), MatGetOwnershipRangeColumn(), MatGetOwnershipRangesColumn()

6575: @*/
6576: PetscErrorCode MatGetOwnershipRanges(Mat mat,const PetscInt **ranges)
6577: {

6583:   MatCheckPreallocated(mat,1);
6584:   PetscLayoutGetRanges(mat->rmap,ranges);
6585:   return(0);
6586: }

6588: /*@C
6589:    MatGetOwnershipRangesColumn - Returns the range of matrix columns associated with rows of a vector one multiplies by that owned by
6590:    this processor. (The columns of the "diagonal blocks" for each process)

6592:    Not Collective, unless matrix has not been allocated, then collective on Mat

6594:    Input Parameters:
6595: .  mat - the matrix

6597:    Output Parameters:
6598: .  ranges - start of each processors portion plus one more then the total length at the end

6600:    Level: beginner

6602: .seealso:   MatGetOwnershipRange(), MatGetOwnershipRangeColumn(), MatGetOwnershipRanges()

6604: @*/
6605: PetscErrorCode MatGetOwnershipRangesColumn(Mat mat,const PetscInt **ranges)
6606: {

6612:   MatCheckPreallocated(mat,1);
6613:   PetscLayoutGetRanges(mat->cmap,ranges);
6614:   return(0);
6615: }

6617: /*@C
6618:    MatGetOwnershipIS - Get row and column ownership as index sets

6620:    Not Collective

6622:    Input Arguments:
6623: .  A - matrix of type Elemental or ScaLAPACK

6625:    Output Arguments:
6626: +  rows - rows in which this process owns elements
6627: -  cols - columns in which this process owns elements

6629:    Level: intermediate

6631: .seealso: MatGetOwnershipRange(), MatGetOwnershipRangeColumn(), MatSetValues(), MATELEMENTAL
6632: @*/
6633: PetscErrorCode MatGetOwnershipIS(Mat A,IS *rows,IS *cols)
6634: {
6635:   PetscErrorCode ierr,(*f)(Mat,IS*,IS*);

6638:   MatCheckPreallocated(A,1);
6639:   PetscObjectQueryFunction((PetscObject)A,"MatGetOwnershipIS_C",&f);
6640:   if (f) {
6641:     (*f)(A,rows,cols);
6642:   } else {   /* Create a standard row-based partition, each process is responsible for ALL columns in their row block */
6643:     if (rows) {ISCreateStride(PETSC_COMM_SELF,A->rmap->n,A->rmap->rstart,1,rows);}
6644:     if (cols) {ISCreateStride(PETSC_COMM_SELF,A->cmap->N,0,1,cols);}
6645:   }
6646:   return(0);
6647: }

6649: /*@C
6650:    MatILUFactorSymbolic - Performs symbolic ILU factorization of a matrix.
6651:    Uses levels of fill only, not drop tolerance. Use MatLUFactorNumeric()
6652:    to complete the factorization.

6654:    Collective on Mat

6656:    Input Parameters:
6657: +  mat - the matrix
6658: .  row - row permutation
6659: .  column - column permutation
6660: -  info - structure containing
6661: $      levels - number of levels of fill.
6662: $      expected fill - as ratio of original fill.
6663: $      1 or 0 - indicating force fill on diagonal (improves robustness for matrices
6664:                 missing diagonal entries)

6666:    Output Parameters:
6667: .  fact - new matrix that has been symbolically factored

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

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

6676:    Level: developer

6678: .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor()
6679:           MatGetOrdering(), MatFactorInfo

6681:     Note: this uses the definition of level of fill as in Y. Saad, 2003

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

6686:    References:
6687:      Y. Saad, Iterative methods for sparse linear systems Philadelphia: Society for Industrial and Applied Mathematics, 2003
6688: @*/
6689: PetscErrorCode MatILUFactorSymbolic(Mat fact,Mat mat,IS row,IS col,const MatFactorInfo *info)
6690: {

6700:   if (info->levels < 0) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"Levels of fill negative %D",(PetscInt)info->levels);
6701:   if (info->fill < 1.0) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"Expected fill less than 1.0 %g",(double)info->fill);
6702:   if (!fact->ops->ilufactorsymbolic) {
6703:     MatSolverType stype;
6704:     MatFactorGetSolverType(fact,&stype);
6705:     SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Matrix type %s symbolic ILU using solver type %s",((PetscObject)mat)->type_name,stype);
6706:   }
6707:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
6708:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
6709:   MatCheckPreallocated(mat,2);

6711:   PetscLogEventBegin(MAT_ILUFactorSymbolic,mat,row,col,0);
6712:   (fact->ops->ilufactorsymbolic)(fact,mat,row,col,info);
6713:   PetscLogEventEnd(MAT_ILUFactorSymbolic,mat,row,col,0);
6714:   return(0);
6715: }

6717: /*@C
6718:    MatICCFactorSymbolic - Performs symbolic incomplete
6719:    Cholesky factorization for a symmetric matrix.  Use
6720:    MatCholeskyFactorNumeric() to complete the factorization.

6722:    Collective on Mat

6724:    Input Parameters:
6725: +  mat - the matrix
6726: .  perm - row and column permutation
6727: -  info - structure containing
6728: $      levels - number of levels of fill.
6729: $      expected fill - as ratio of original fill.

6731:    Output Parameter:
6732: .  fact - the factored matrix

6734:    Notes:
6735:    Most users should employ the KSP interface for linear solvers
6736:    instead of working directly with matrix algebra routines such as this.
6737:    See, e.g., KSPCreate().

6739:    Level: developer

6741: .seealso: MatCholeskyFactorNumeric(), MatCholeskyFactor(), MatFactorInfo

6743:     Note: this uses the definition of level of fill as in Y. Saad, 2003

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

6748:    References:
6749:      Y. Saad, Iterative methods for sparse linear systems Philadelphia: Society for Industrial and Applied Mathematics, 2003
6750: @*/
6751: PetscErrorCode MatICCFactorSymbolic(Mat fact,Mat mat,IS perm,const MatFactorInfo *info)
6752: {

6761:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
6762:   if (info->levels < 0) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"Levels negative %D",(PetscInt) info->levels);
6763:   if (info->fill < 1.0) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"Expected fill less than 1.0 %g",(double)info->fill);
6764:   if (!(fact)->ops->iccfactorsymbolic) {
6765:     MatSolverType stype;
6766:     MatFactorGetSolverType(fact,&stype);
6767:     SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Matrix type %s symbolic ICC using solver type %s",((PetscObject)mat)->type_name,stype);
6768:   }
6769:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
6770:   MatCheckPreallocated(mat,2);

6772:   PetscLogEventBegin(MAT_ICCFactorSymbolic,mat,perm,0,0);
6773:   (fact->ops->iccfactorsymbolic)(fact,mat,perm,info);
6774:   PetscLogEventEnd(MAT_ICCFactorSymbolic,mat,perm,0,0);
6775:   return(0);
6776: }

6778: /*@C
6779:    MatCreateSubMatrices - Extracts several submatrices from a matrix. If submat
6780:    points to an array of valid matrices, they may be reused to store the new
6781:    submatrices.

6783:    Collective on Mat

6785:    Input Parameters:
6786: +  mat - the matrix
6787: .  n   - the number of submatrixes to be extracted (on this processor, may be zero)
6788: .  irow, icol - index sets of rows and columns to extract
6789: -  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX

6791:    Output Parameter:
6792: .  submat - the array of submatrices

6794:    Notes:
6795:    MatCreateSubMatrices() can extract ONLY sequential submatrices
6796:    (from both sequential and parallel matrices). Use MatCreateSubMatrix()
6797:    to extract a parallel submatrix.

6799:    Some matrix types place restrictions on the row and column
6800:    indices, such as that they be sorted or that they be equal to each other.

6802:    The index sets may not have duplicate entries.

6804:    When extracting submatrices from a parallel matrix, each processor can
6805:    form a different submatrix by setting the rows and columns of its
6806:    individual index sets according to the local submatrix desired.

6808:    When finished using the submatrices, the user should destroy
6809:    them with MatDestroySubMatrices().

6811:    MAT_REUSE_MATRIX can only be used when the nonzero structure of the
6812:    original matrix has not changed from that last call to MatCreateSubMatrices().

6814:    This routine creates the matrices in submat; you should NOT create them before
6815:    calling it. It also allocates the array of matrix pointers submat.

6817:    For BAIJ matrices the index sets must respect the block structure, that is if they
6818:    request one row/column in a block, they must request all rows/columns that are in
6819:    that block. For example, if the block size is 2 you cannot request just row 0 and
6820:    column 0.

6822:    Fortran Note:
6823:    The Fortran interface is slightly different from that given below; it
6824:    requires one to pass in  as submat a Mat (integer) array of size at least n+1.

6826:    Level: advanced


6829: .seealso: MatDestroySubMatrices(), MatCreateSubMatrix(), MatGetRow(), MatGetDiagonal(), MatReuse
6830: @*/
6831: PetscErrorCode MatCreateSubMatrices(Mat mat,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *submat[])
6832: {
6834:   PetscInt       i;
6835:   PetscBool      eq;

6840:   if (n) {
6845:   }
6847:   if (n && scall == MAT_REUSE_MATRIX) {
6850:   }
6851:   if (!mat->ops->createsubmatrices) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
6852:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
6853:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
6854:   MatCheckPreallocated(mat,1);

6856:   PetscLogEventBegin(MAT_CreateSubMats,mat,0,0,0);
6857:   (*mat->ops->createsubmatrices)(mat,n,irow,icol,scall,submat);
6858:   PetscLogEventEnd(MAT_CreateSubMats,mat,0,0,0);
6859:   for (i=0; i<n; i++) {
6860:     (*submat)[i]->factortype = MAT_FACTOR_NONE;  /* in case in place factorization was previously done on submatrix */
6861:     ISEqualUnsorted(irow[i],icol[i],&eq);
6862:     if (eq) {
6863:       MatPropagateSymmetryOptions(mat,(*submat)[i]);
6864:     }
6865:   }
6866:   return(0);
6867: }

6869: /*@C
6870:    MatCreateSubMatricesMPI - Extracts MPI submatrices across a sub communicator of mat (by pairs of IS that may live on subcomms).

6872:    Collective on Mat

6874:    Input Parameters:
6875: +  mat - the matrix
6876: .  n   - the number of submatrixes to be extracted
6877: .  irow, icol - index sets of rows and columns to extract
6878: -  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX

6880:    Output Parameter:
6881: .  submat - the array of submatrices

6883:    Level: advanced


6886: .seealso: MatCreateSubMatrices(), MatCreateSubMatrix(), MatGetRow(), MatGetDiagonal(), MatReuse
6887: @*/
6888: PetscErrorCode MatCreateSubMatricesMPI(Mat mat,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *submat[])
6889: {
6891:   PetscInt       i;
6892:   PetscBool      eq;

6897:   if (n) {
6902:   }
6904:   if (n && scall == MAT_REUSE_MATRIX) {
6907:   }
6908:   if (!mat->ops->createsubmatricesmpi) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
6909:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
6910:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
6911:   MatCheckPreallocated(mat,1);

6913:   PetscLogEventBegin(MAT_CreateSubMats,mat,0,0,0);
6914:   (*mat->ops->createsubmatricesmpi)(mat,n,irow,icol,scall,submat);
6915:   PetscLogEventEnd(MAT_CreateSubMats,mat,0,0,0);
6916:   for (i=0; i<n; i++) {
6917:     ISEqualUnsorted(irow[i],icol[i],&eq);
6918:     if (eq) {
6919:       MatPropagateSymmetryOptions(mat,(*submat)[i]);
6920:     }
6921:   }
6922:   return(0);
6923: }

6925: /*@C
6926:    MatDestroyMatrices - Destroys an array of matrices.

6928:    Collective on Mat

6930:    Input Parameters:
6931: +  n - the number of local matrices
6932: -  mat - the matrices (note that this is a pointer to the array of matrices)

6934:    Level: advanced

6936:     Notes:
6937:     Frees not only the matrices, but also the array that contains the matrices
6938:            In Fortran will not free the array.

6940: .seealso: MatCreateSubMatrices() MatDestroySubMatrices()
6941: @*/
6942: PetscErrorCode MatDestroyMatrices(PetscInt n,Mat *mat[])
6943: {
6945:   PetscInt       i;

6948:   if (!*mat) return(0);
6949:   if (n < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Trying to destroy negative number of matrices %D",n);

6952:   for (i=0; i<n; i++) {
6953:     MatDestroy(&(*mat)[i]);
6954:   }

6956:   /* memory is allocated even if n = 0 */
6957:   PetscFree(*mat);
6958:   return(0);
6959: }

6961: /*@C
6962:    MatDestroySubMatrices - Destroys a set of matrices obtained with MatCreateSubMatrices().

6964:    Collective on Mat

6966:    Input Parameters:
6967: +  n - the number of local matrices
6968: -  mat - the matrices (note that this is a pointer to the array of matrices, just to match the calling
6969:                        sequence of MatCreateSubMatrices())

6971:    Level: advanced

6973:     Notes:
6974:     Frees not only the matrices, but also the array that contains the matrices
6975:            In Fortran will not free the array.

6977: .seealso: MatCreateSubMatrices()
6978: @*/
6979: PetscErrorCode MatDestroySubMatrices(PetscInt n,Mat *mat[])
6980: {
6982:   Mat            mat0;

6985:   if (!*mat) return(0);
6986:   /* mat[] is an array of length n+1, see MatCreateSubMatrices_xxx() */
6987:   if (n < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Trying to destroy negative number of matrices %D",n);

6990:   mat0 = (*mat)[0];
6991:   if (mat0 && mat0->ops->destroysubmatrices) {
6992:     (mat0->ops->destroysubmatrices)(n,mat);
6993:   } else {
6994:     MatDestroyMatrices(n,mat);
6995:   }
6996:   return(0);
6997: }

6999: /*@C
7000:    MatGetSeqNonzeroStructure - Extracts the sequential nonzero structure from a matrix.

7002:    Collective on Mat

7004:    Input Parameters:
7005: .  mat - the matrix

7007:    Output Parameter:
7008: .  matstruct - the sequential matrix with the nonzero structure of mat

7010:   Level: intermediate

7012: .seealso: MatDestroySeqNonzeroStructure(), MatCreateSubMatrices(), MatDestroyMatrices()
7013: @*/
7014: PetscErrorCode MatGetSeqNonzeroStructure(Mat mat,Mat *matstruct)
7015: {


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

7026:   if (!mat->ops->getseqnonzerostructure) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Not for matrix type %s\n",((PetscObject)mat)->type_name);
7027:   PetscLogEventBegin(MAT_GetSeqNonzeroStructure,mat,0,0,0);
7028:   (*mat->ops->getseqnonzerostructure)(mat,matstruct);
7029:   PetscLogEventEnd(MAT_GetSeqNonzeroStructure,mat,0,0,0);
7030:   return(0);
7031: }

7033: /*@C
7034:    MatDestroySeqNonzeroStructure - Destroys matrix obtained with MatGetSeqNonzeroStructure().

7036:    Collective on Mat

7038:    Input Parameters:
7039: .  mat - the matrix (note that this is a pointer to the array of matrices, just to match the calling
7040:                        sequence of MatGetSequentialNonzeroStructure())

7042:    Level: advanced

7044:     Notes:
7045:     Frees not only the matrices, but also the array that contains the matrices

7047: .seealso: MatGetSeqNonzeroStructure()
7048: @*/
7049: PetscErrorCode MatDestroySeqNonzeroStructure(Mat *mat)
7050: {

7055:   MatDestroy(mat);
7056:   return(0);
7057: }

7059: /*@
7060:    MatIncreaseOverlap - Given a set of submatrices indicated by index sets,
7061:    replaces the index sets by larger ones that represent submatrices with
7062:    additional overlap.

7064:    Collective on Mat

7066:    Input Parameters:
7067: +  mat - the matrix
7068: .  n   - the number of index sets
7069: .  is  - the array of index sets (these index sets will changed during the call)
7070: -  ov  - the additional overlap requested

7072:    Options Database:
7073: .  -mat_increase_overlap_scalable - use a scalable algorithm to compute the overlap (supported by MPIAIJ matrix)

7075:    Level: developer


7078: .seealso: MatCreateSubMatrices()
7079: @*/
7080: PetscErrorCode MatIncreaseOverlap(Mat mat,PetscInt n,IS is[],PetscInt ov)
7081: {

7087:   if (n < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Must have one or more domains, you have %D",n);
7088:   if (n) {
7091:   }
7092:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
7093:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
7094:   MatCheckPreallocated(mat,1);

7096:   if (!ov) return(0);
7097:   if (!mat->ops->increaseoverlap) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
7098:   PetscLogEventBegin(MAT_IncreaseOverlap,mat,0,0,0);
7099:   (*mat->ops->increaseoverlap)(mat,n,is,ov);
7100:   PetscLogEventEnd(MAT_IncreaseOverlap,mat,0,0,0);
7101:   return(0);
7102: }


7105: PetscErrorCode MatIncreaseOverlapSplit_Single(Mat,IS*,PetscInt);

7107: /*@
7108:    MatIncreaseOverlapSplit - Given a set of submatrices indicated by index sets across
7109:    a sub communicator, replaces the index sets by larger ones that represent submatrices with
7110:    additional overlap.

7112:    Collective on Mat

7114:    Input Parameters:
7115: +  mat - the matrix
7116: .  n   - the number of index sets
7117: .  is  - the array of index sets (these index sets will changed during the call)
7118: -  ov  - the additional overlap requested

7120:    Options Database:
7121: .  -mat_increase_overlap_scalable - use a scalable algorithm to compute the overlap (supported by MPIAIJ matrix)

7123:    Level: developer


7126: .seealso: MatCreateSubMatrices()
7127: @*/
7128: PetscErrorCode MatIncreaseOverlapSplit(Mat mat,PetscInt n,IS is[],PetscInt ov)
7129: {
7130:   PetscInt       i;

7136:   if (n < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Must have one or more domains, you have %D",n);
7137:   if (n) {
7140:   }
7141:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
7142:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
7143:   MatCheckPreallocated(mat,1);
7144:   if (!ov) return(0);
7145:   PetscLogEventBegin(MAT_IncreaseOverlap,mat,0,0,0);
7146:   for (i=0; i<n; i++){
7147:          MatIncreaseOverlapSplit_Single(mat,&is[i],ov);
7148:   }
7149:   PetscLogEventEnd(MAT_IncreaseOverlap,mat,0,0,0);
7150:   return(0);
7151: }




7156: /*@
7157:    MatGetBlockSize - Returns the matrix block size.

7159:    Not Collective

7161:    Input Parameter:
7162: .  mat - the matrix

7164:    Output Parameter:
7165: .  bs - block size

7167:    Notes:
7168:     Block row formats are MATSEQBAIJ, MATMPIBAIJ, MATSEQSBAIJ, MATMPISBAIJ. These formats ALWAYS have square block storage in the matrix.

7170:    If the block size has not been set yet this routine returns 1.

7172:    Level: intermediate

7174: .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSizes()
7175: @*/
7176: PetscErrorCode MatGetBlockSize(Mat mat,PetscInt *bs)
7177: {
7181:   *bs = PetscAbs(mat->rmap->bs);
7182:   return(0);
7183: }

7185: /*@
7186:    MatGetBlockSizes - Returns the matrix block row and column sizes.

7188:    Not Collective

7190:    Input Parameter:
7191: .  mat - the matrix

7193:    Output Parameter:
7194: +  rbs - row block size
7195: -  cbs - column block size

7197:    Notes:
7198:     Block row formats are MATSEQBAIJ, MATMPIBAIJ, MATSEQSBAIJ, MATMPISBAIJ. These formats ALWAYS have square block storage in the matrix.
7199:     If you pass a different block size for the columns than the rows, the row block size determines the square block storage.

7201:    If a block size has not been set yet this routine returns 1.

7203:    Level: intermediate

7205: .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize(), MatSetBlockSize(), MatSetBlockSizes()
7206: @*/
7207: PetscErrorCode MatGetBlockSizes(Mat mat,PetscInt *rbs, PetscInt *cbs)
7208: {
7213:   if (rbs) *rbs = PetscAbs(mat->rmap->bs);
7214:   if (cbs) *cbs = PetscAbs(mat->cmap->bs);
7215:   return(0);
7216: }

7218: /*@
7219:    MatSetBlockSize - Sets the matrix block size.

7221:    Logically Collective on Mat

7223:    Input Parameters:
7224: +  mat - the matrix
7225: -  bs - block size

7227:    Notes:
7228:     Block row formats are MATSEQBAIJ, MATMPIBAIJ, MATSEQSBAIJ, MATMPISBAIJ. These formats ALWAYS have square block storage in the matrix.
7229:     This must be called before MatSetUp() or MatXXXSetPreallocation() (or will default to 1) and the block size cannot be changed later.

7231:     For MATMPIAIJ and MATSEQAIJ matrix formats, this function can be called at a later stage, provided that the specified block size
7232:     is compatible with the matrix local sizes.

7234:    Level: intermediate

7236: .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize(), MatSetBlockSizes(), MatGetBlockSizes()
7237: @*/
7238: PetscErrorCode MatSetBlockSize(Mat mat,PetscInt bs)
7239: {

7245:   MatSetBlockSizes(mat,bs,bs);
7246:   return(0);
7247: }

7249: /*@
7250:    MatSetVariableBlockSizes - Sets a diagonal blocks of the matrix that need not be of the same size

7252:    Logically Collective on Mat

7254:    Input Parameters:
7255: +  mat - the matrix
7256: .  nblocks - the number of blocks on this process
7257: -  bsizes - the block sizes

7259:    Notes:
7260:     Currently used by PCVPBJACOBI for SeqAIJ matrices

7262:    Level: intermediate

7264: .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize(), MatSetBlockSizes(), MatGetBlockSizes(), MatGetVariableBlockSizes()
7265: @*/
7266: PetscErrorCode MatSetVariableBlockSizes(Mat mat,PetscInt nblocks,PetscInt *bsizes)
7267: {
7269:   PetscInt       i,ncnt = 0, nlocal;

7273:   if (nblocks < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Number of local blocks must be great than or equal to zero");
7274:   MatGetLocalSize(mat,&nlocal,NULL);
7275:   for (i=0; i<nblocks; i++) ncnt += bsizes[i];
7276:   if (ncnt != nlocal) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Sum of local block sizes %D does not equal local size of matrix %D",ncnt,nlocal);
7277:   PetscFree(mat->bsizes);
7278:   mat->nblocks = nblocks;
7279:   PetscMalloc1(nblocks,&mat->bsizes);
7280:   PetscArraycpy(mat->bsizes,bsizes,nblocks);
7281:   return(0);
7282: }

7284: /*@C
7285:    MatGetVariableBlockSizes - Gets a diagonal blocks of the matrix that need not be of the same size

7287:    Logically Collective on Mat

7289:    Input Parameters:
7290: .  mat - the matrix

7292:    Output Parameters:
7293: +  nblocks - the number of blocks on this process
7294: -  bsizes - the block sizes

7296:    Notes: Currently not supported from Fortran

7298:    Level: intermediate

7300: .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize(), MatSetBlockSizes(), MatGetBlockSizes(), MatSetVariableBlockSizes()
7301: @*/
7302: PetscErrorCode MatGetVariableBlockSizes(Mat mat,PetscInt *nblocks,const PetscInt **bsizes)
7303: {
7306:   *nblocks = mat->nblocks;
7307:   *bsizes  = mat->bsizes;
7308:   return(0);
7309: }

7311: /*@
7312:    MatSetBlockSizes - Sets the matrix block row and column sizes.

7314:    Logically Collective on Mat

7316:    Input Parameters:
7317: +  mat - the matrix
7318: .  rbs - row block size
7319: -  cbs - column block size

7321:    Notes:
7322:     Block row formats are MATSEQBAIJ, MATMPIBAIJ, MATSEQSBAIJ, MATMPISBAIJ. These formats ALWAYS have square block storage in the matrix.
7323:     If you pass a different block size for the columns than the rows, the row block size determines the square block storage.
7324:     This must be called before MatSetUp() or MatXXXSetPreallocation() (or will default to 1) and the block size cannot be changed later.

7326:     For MATMPIAIJ and MATSEQAIJ matrix formats, this function can be called at a later stage, provided that the specified block sizes
7327:     are compatible with the matrix local sizes.

7329:     The row and column block size determine the blocksize of the "row" and "column" vectors returned by MatCreateVecs().

7331:    Level: intermediate

7333: .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize(), MatSetBlockSize(), MatGetBlockSizes()
7334: @*/
7335: PetscErrorCode MatSetBlockSizes(Mat mat,PetscInt rbs,PetscInt cbs)
7336: {

7343:   if (mat->ops->setblocksizes) {
7344:     (*mat->ops->setblocksizes)(mat,rbs,cbs);
7345:   }
7346:   if (mat->rmap->refcnt) {
7347:     ISLocalToGlobalMapping l2g = NULL;
7348:     PetscLayout            nmap = NULL;

7350:     PetscLayoutDuplicate(mat->rmap,&nmap);
7351:     if (mat->rmap->mapping) {
7352:       ISLocalToGlobalMappingDuplicate(mat->rmap->mapping,&l2g);
7353:     }
7354:     PetscLayoutDestroy(&mat->rmap);
7355:     mat->rmap = nmap;
7356:     mat->rmap->mapping = l2g;
7357:   }
7358:   if (mat->cmap->refcnt) {
7359:     ISLocalToGlobalMapping l2g = NULL;
7360:     PetscLayout            nmap = NULL;

7362:     PetscLayoutDuplicate(mat->cmap,&nmap);
7363:     if (mat->cmap->mapping) {
7364:       ISLocalToGlobalMappingDuplicate(mat->cmap->mapping,&l2g);
7365:     }
7366:     PetscLayoutDestroy(&mat->cmap);
7367:     mat->cmap = nmap;
7368:     mat->cmap->mapping = l2g;
7369:   }
7370:   PetscLayoutSetBlockSize(mat->rmap,rbs);
7371:   PetscLayoutSetBlockSize(mat->cmap,cbs);
7372:   return(0);
7373: }

7375: /*@
7376:    MatSetBlockSizesFromMats - Sets the matrix block row and column sizes to match a pair of matrices

7378:    Logically Collective on Mat

7380:    Input Parameters:
7381: +  mat - the matrix
7382: .  fromRow - matrix from which to copy row block size
7383: -  fromCol - matrix from which to copy column block size (can be same as fromRow)

7385:    Level: developer

7387: .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize(), MatSetBlockSizes()
7388: @*/
7389: PetscErrorCode MatSetBlockSizesFromMats(Mat mat,Mat fromRow,Mat fromCol)
7390: {

7397:   if (fromRow->rmap->bs > 0) {PetscLayoutSetBlockSize(mat->rmap,fromRow->rmap->bs);}
7398:   if (fromCol->cmap->bs > 0) {PetscLayoutSetBlockSize(mat->cmap,fromCol->cmap->bs);}
7399:   return(0);
7400: }

7402: /*@
7403:    MatResidual - Default routine to calculate the residual.

7405:    Collective on Mat

7407:    Input Parameters:
7408: +  mat - the matrix
7409: .  b   - the right-hand-side
7410: -  x   - the approximate solution

7412:    Output Parameter:
7413: .  r - location to store the residual

7415:    Level: developer

7417: .seealso: PCMGSetResidual()
7418: @*/
7419: PetscErrorCode MatResidual(Mat mat,Vec b,Vec x,Vec r)
7420: {

7429:   MatCheckPreallocated(mat,1);
7430:   PetscLogEventBegin(MAT_Residual,mat,0,0,0);
7431:   if (!mat->ops->residual) {
7432:     MatMult(mat,x,r);
7433:     VecAYPX(r,-1.0,b);
7434:   } else {
7435:     (*mat->ops->residual)(mat,b,x,r);
7436:   }
7437:   PetscLogEventEnd(MAT_Residual,mat,0,0,0);
7438:   return(0);
7439: }

7441: /*@C
7442:     MatGetRowIJ - Returns the compressed row storage i and j indices for sequential matrices.

7444:    Collective on Mat

7446:     Input Parameters:
7447: +   mat - the matrix
7448: .   shift -  0 or 1 indicating we want the indices starting at 0 or 1
7449: .   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be   symmetrized
7450: -   inodecompressed - PETSC_TRUE or PETSC_FALSE  indicating if the nonzero structure of the
7451:                  inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is
7452:                  always used.

7454:     Output Parameters:
7455: +   n - number of rows in the (possibly compressed) matrix
7456: .   ia - the row pointers; that is ia[0] = 0, ia[row] = ia[row-1] + number of elements in that row of the matrix
7457: .   ja - the column indices
7458: -   done - indicates if the routine actually worked and returned appropriate ia[] and ja[] arrays; callers
7459:            are responsible for handling the case when done == PETSC_FALSE and ia and ja are not set

7461:     Level: developer

7463:     Notes:
7464:     You CANNOT change any of the ia[] or ja[] values.

7466:     Use MatRestoreRowIJ() when you are finished accessing the ia[] and ja[] values.

7468:     Fortran Notes:
7469:     In Fortran use
7470: $
7471: $      PetscInt ia(1), ja(1)
7472: $      PetscOffset iia, jja
7473: $      call MatGetRowIJ(mat,shift,symmetric,inodecompressed,n,ia,iia,ja,jja,done,ierr)
7474: $      ! Access the ith and jth entries via ia(iia + i) and ja(jja + j)

7476:      or
7477: $
7478: $    PetscInt, pointer :: ia(:),ja(:)
7479: $    call MatGetRowIJF90(mat,shift,symmetric,inodecompressed,n,ia,ja,done,ierr)
7480: $    ! Access the ith and jth entries via ia(i) and ja(j)

7482: .seealso: MatGetColumnIJ(), MatRestoreRowIJ(), MatSeqAIJGetArray()
7483: @*/
7484: PetscErrorCode MatGetRowIJ(Mat mat,PetscInt shift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
7485: {

7495:   MatCheckPreallocated(mat,1);
7496:   if (!mat->ops->getrowij) *done = PETSC_FALSE;
7497:   else {
7498:     *done = PETSC_TRUE;
7499:     PetscLogEventBegin(MAT_GetRowIJ,mat,0,0,0);
7500:     (*mat->ops->getrowij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);
7501:     PetscLogEventEnd(MAT_GetRowIJ,mat,0,0,0);
7502:   }
7503:   return(0);
7504: }

7506: /*@C
7507:     MatGetColumnIJ - Returns the compressed column storage i and j indices for sequential matrices.

7509:     Collective on Mat

7511:     Input Parameters:
7512: +   mat - the matrix
7513: .   shift - 1 or zero indicating we want the indices starting at 0 or 1
7514: .   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be
7515:                 symmetrized
7516: .   inodecompressed - PETSC_TRUE or PETSC_FALSE indicating if the nonzero structure of the
7517:                  inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is
7518:                  always used.
7519: .   n - number of columns in the (possibly compressed) matrix
7520: .   ia - the column pointers; that is ia[0] = 0, ia[col] = i[col-1] + number of elements in that col of the matrix
7521: -   ja - the row indices

7523:     Output Parameters:
7524: .   done - PETSC_TRUE or PETSC_FALSE, indicating whether the values have been returned

7526:     Level: developer

7528: .seealso: MatGetRowIJ(), MatRestoreColumnIJ()
7529: @*/
7530: PetscErrorCode MatGetColumnIJ(Mat mat,PetscInt shift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
7531: {

7541:   MatCheckPreallocated(mat,1);
7542:   if (!mat->ops->getcolumnij) *done = PETSC_FALSE;
7543:   else {
7544:     *done = PETSC_TRUE;
7545:     (*mat->ops->getcolumnij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);
7546:   }
7547:   return(0);
7548: }

7550: /*@C
7551:     MatRestoreRowIJ - Call after you are completed with the ia,ja indices obtained with
7552:     MatGetRowIJ().

7554:     Collective on Mat

7556:     Input Parameters:
7557: +   mat - the matrix
7558: .   shift - 1 or zero indicating we want the indices starting at 0 or 1
7559: .   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be
7560:                 symmetrized
7561: .   inodecompressed -  PETSC_TRUE or PETSC_FALSE indicating if the nonzero structure of the
7562:                  inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is
7563:                  always used.
7564: .   n - size of (possibly compressed) matrix
7565: .   ia - the row pointers
7566: -   ja - the column indices

7568:     Output Parameters:
7569: .   done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned

7571:     Note:
7572:     This routine zeros out n, ia, and ja. This is to prevent accidental
7573:     us of the array after it has been restored. If you pass NULL, it will
7574:     not zero the pointers.  Use of ia or ja after MatRestoreRowIJ() is invalid.

7576:     Level: developer

7578: .seealso: MatGetRowIJ(), MatRestoreColumnIJ()
7579: @*/
7580: PetscErrorCode MatRestoreRowIJ(Mat mat,PetscInt shift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
7581: {

7590:   MatCheckPreallocated(mat,1);

7592:   if (!mat->ops->restorerowij) *done = PETSC_FALSE;
7593:   else {
7594:     *done = PETSC_TRUE;
7595:     (*mat->ops->restorerowij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);
7596:     if (n)  *n = 0;
7597:     if (ia) *ia = NULL;
7598:     if (ja) *ja = NULL;
7599:   }
7600:   return(0);
7601: }

7603: /*@C
7604:     MatRestoreColumnIJ - Call after you are completed with the ia,ja indices obtained with
7605:     MatGetColumnIJ().

7607:     Collective on Mat

7609:     Input Parameters:
7610: +   mat - the matrix
7611: .   shift - 1 or zero indicating we want the indices starting at 0 or 1
7612: -   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be
7613:                 symmetrized
7614: -   inodecompressed - PETSC_TRUE or PETSC_FALSE indicating if the nonzero structure of the
7615:                  inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is
7616:                  always used.

7618:     Output Parameters:
7619: +   n - size of (possibly compressed) matrix
7620: .   ia - the column pointers
7621: .   ja - the row indices
7622: -   done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned

7624:     Level: developer

7626: .seealso: MatGetColumnIJ(), MatRestoreRowIJ()
7627: @*/
7628: PetscErrorCode MatRestoreColumnIJ(Mat mat,PetscInt shift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
7629: {

7638:   MatCheckPreallocated(mat,1);

7640:   if (!mat->ops->restorecolumnij) *done = PETSC_FALSE;
7641:   else {
7642:     *done = PETSC_TRUE;
7643:     (*mat->ops->restorecolumnij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);
7644:     if (n)  *n = 0;
7645:     if (ia) *ia = NULL;
7646:     if (ja) *ja = NULL;
7647:   }
7648:   return(0);
7649: }

7651: /*@C
7652:     MatColoringPatch -Used inside matrix coloring routines that
7653:     use MatGetRowIJ() and/or MatGetColumnIJ().

7655:     Collective on Mat

7657:     Input Parameters:
7658: +   mat - the matrix
7659: .   ncolors - max color value
7660: .   n   - number of entries in colorarray
7661: -   colorarray - array indicating color for each column

7663:     Output Parameters:
7664: .   iscoloring - coloring generated using colorarray information

7666:     Level: developer

7668: .seealso: MatGetRowIJ(), MatGetColumnIJ()

7670: @*/
7671: PetscErrorCode MatColoringPatch(Mat mat,PetscInt ncolors,PetscInt n,ISColoringValue colorarray[],ISColoring *iscoloring)
7672: {

7680:   MatCheckPreallocated(mat,1);

7682:   if (!mat->ops->coloringpatch) {
7683:     ISColoringCreate(PetscObjectComm((PetscObject)mat),ncolors,n,colorarray,PETSC_OWN_POINTER,iscoloring);
7684:   } else {
7685:     (*mat->ops->coloringpatch)(mat,ncolors,n,colorarray,iscoloring);
7686:   }
7687:   return(0);
7688: }


7691: /*@
7692:    MatSetUnfactored - Resets a factored matrix to be treated as unfactored.

7694:    Logically Collective on Mat

7696:    Input Parameter:
7697: .  mat - the factored matrix to be reset

7699:    Notes:
7700:    This routine should be used only with factored matrices formed by in-place
7701:    factorization via ILU(0) (or by in-place LU factorization for the MATSEQDENSE
7702:    format).  This option can save memory, for example, when solving nonlinear
7703:    systems with a matrix-free Newton-Krylov method and a matrix-based, in-place
7704:    ILU(0) preconditioner.

7706:    Note that one can specify in-place ILU(0) factorization by calling
7707: .vb
7708:      PCType(pc,PCILU);
7709:      PCFactorSeUseInPlace(pc);
7710: .ve
7711:    or by using the options -pc_type ilu -pc_factor_in_place

7713:    In-place factorization ILU(0) can also be used as a local
7714:    solver for the blocks within the block Jacobi or additive Schwarz
7715:    methods (runtime option: -sub_pc_factor_in_place).  See Users-Manual: ch_pc
7716:    for details on setting local solver options.

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

7722:    Level: developer

7724: .seealso: PCFactorSetUseInPlace(), PCFactorGetUseInPlace()

7726: @*/
7727: PetscErrorCode MatSetUnfactored(Mat mat)
7728: {

7734:   MatCheckPreallocated(mat,1);
7735:   mat->factortype = MAT_FACTOR_NONE;
7736:   if (!mat->ops->setunfactored) return(0);
7737:   (*mat->ops->setunfactored)(mat);
7738:   return(0);
7739: }

7741: /*MC
7742:     MatDenseGetArrayF90 - Accesses a matrix array from Fortran90.

7744:     Synopsis:
7745:     MatDenseGetArrayF90(Mat x,{Scalar, pointer :: xx_v(:,:)},integer ierr)

7747:     Not collective

7749:     Input Parameter:
7750: .   x - matrix

7752:     Output Parameters:
7753: +   xx_v - the Fortran90 pointer to the array
7754: -   ierr - error code

7756:     Example of Usage:
7757: .vb
7758:       PetscScalar, pointer xx_v(:,:)
7759:       ....
7760:       call MatDenseGetArrayF90(x,xx_v,ierr)
7761:       a = xx_v(3)
7762:       call MatDenseRestoreArrayF90(x,xx_v,ierr)
7763: .ve

7765:     Level: advanced

7767: .seealso:  MatDenseRestoreArrayF90(), MatDenseGetArray(), MatDenseRestoreArray(), MatSeqAIJGetArrayF90()

7769: M*/

7771: /*MC
7772:     MatDenseRestoreArrayF90 - Restores a matrix array that has been
7773:     accessed with MatDenseGetArrayF90().

7775:     Synopsis:
7776:     MatDenseRestoreArrayF90(Mat x,{Scalar, pointer :: xx_v(:,:)},integer ierr)

7778:     Not collective

7780:     Input Parameters:
7781: +   x - matrix
7782: -   xx_v - the Fortran90 pointer to the array

7784:     Output Parameter:
7785: .   ierr - error code

7787:     Example of Usage:
7788: .vb
7789:        PetscScalar, pointer xx_v(:,:)
7790:        ....
7791:        call MatDenseGetArrayF90(x,xx_v,ierr)
7792:        a = xx_v(3)
7793:        call MatDenseRestoreArrayF90(x,xx_v,ierr)
7794: .ve

7796:     Level: advanced

7798: .seealso:  MatDenseGetArrayF90(), MatDenseGetArray(), MatDenseRestoreArray(), MatSeqAIJRestoreArrayF90()

7800: M*/


7803: /*MC
7804:     MatSeqAIJGetArrayF90 - Accesses a matrix array from Fortran90.

7806:     Synopsis:
7807:     MatSeqAIJGetArrayF90(Mat x,{Scalar, pointer :: xx_v(:)},integer ierr)

7809:     Not collective

7811:     Input Parameter:
7812: .   x - matrix

7814:     Output Parameters:
7815: +   xx_v - the Fortran90 pointer to the array
7816: -   ierr - error code

7818:     Example of Usage:
7819: .vb
7820:       PetscScalar, pointer xx_v(:)
7821:       ....
7822:       call MatSeqAIJGetArrayF90(x,xx_v,ierr)
7823:       a = xx_v(3)
7824:       call MatSeqAIJRestoreArrayF90(x,xx_v,ierr)
7825: .ve

7827:     Level: advanced

7829: .seealso:  MatSeqAIJRestoreArrayF90(), MatSeqAIJGetArray(), MatSeqAIJRestoreArray(), MatDenseGetArrayF90()

7831: M*/

7833: /*MC
7834:     MatSeqAIJRestoreArrayF90 - Restores a matrix array that has been
7835:     accessed with MatSeqAIJGetArrayF90().

7837:     Synopsis:
7838:     MatSeqAIJRestoreArrayF90(Mat x,{Scalar, pointer :: xx_v(:)},integer ierr)

7840:     Not collective

7842:     Input Parameters:
7843: +   x - matrix
7844: -   xx_v - the Fortran90 pointer to the array

7846:     Output Parameter:
7847: .   ierr - error code

7849:     Example of Usage:
7850: .vb
7851:        PetscScalar, pointer xx_v(:)
7852:        ....
7853:        call MatSeqAIJGetArrayF90(x,xx_v,ierr)
7854:        a = xx_v(3)
7855:        call MatSeqAIJRestoreArrayF90(x,xx_v,ierr)
7856: .ve

7858:     Level: advanced

7860: .seealso:  MatSeqAIJGetArrayF90(), MatSeqAIJGetArray(), MatSeqAIJRestoreArray(), MatDenseRestoreArrayF90()

7862: M*/


7865: /*@
7866:     MatCreateSubMatrix - Gets a single submatrix on the same number of processors
7867:                       as the original matrix.

7869:     Collective on Mat

7871:     Input Parameters:
7872: +   mat - the original matrix
7873: .   isrow - parallel IS containing the rows this processor should obtain
7874: .   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.
7875: -   cll - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX

7877:     Output Parameter:
7878: .   newmat - the new submatrix, of the same type as the old

7880:     Level: advanced

7882:     Notes:
7883:     The submatrix will be able to be multiplied with vectors using the same layout as iscol.

7885:     Some matrix types place restrictions on the row and column indices, such
7886:     as that they be sorted or that they be equal to each other.

7888:     The index sets may not have duplicate entries.

7890:       The first time this is called you should use a cll of MAT_INITIAL_MATRIX,
7891:    the MatCreateSubMatrix() routine will create the newmat for you. Any additional calls
7892:    to this routine with a mat of the same nonzero structure and with a call of MAT_REUSE_MATRIX
7893:    will reuse the matrix generated the first time.  You should call MatDestroy() on newmat when
7894:    you are finished using it.

7896:     The communicator of the newly obtained matrix is ALWAYS the same as the communicator of
7897:     the input matrix.

7899:     If iscol is NULL then all columns are obtained (not supported in Fortran).

7901:    Example usage:
7902:    Consider the following 8x8 matrix with 34 non-zero values, that is
7903:    assembled across 3 processors. Let's assume that proc0 owns 3 rows,
7904:    proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown
7905:    as follows:

7907: .vb
7908:             1  2  0  |  0  3  0  |  0  4
7909:     Proc0   0  5  6  |  7  0  0  |  8  0
7910:             9  0 10  | 11  0  0  | 12  0
7911:     -------------------------------------
7912:            13  0 14  | 15 16 17  |  0  0
7913:     Proc1   0 18  0  | 19 20 21  |  0  0
7914:             0  0  0  | 22 23  0  | 24  0
7915:     -------------------------------------
7916:     Proc2  25 26 27  |  0  0 28  | 29  0
7917:            30  0  0  | 31 32 33  |  0 34
7918: .ve

7920:     Suppose isrow = [0 1 | 4 | 6 7] and iscol = [1 2 | 3 4 5 | 6].  The resulting submatrix is

7922: .vb
7923:             2  0  |  0  3  0  |  0
7924:     Proc0   5  6  |  7  0  0  |  8
7925:     -------------------------------
7926:     Proc1  18  0  | 19 20 21  |  0
7927:     -------------------------------
7928:     Proc2  26 27  |  0  0 28  | 29
7929:             0  0  | 31 32 33  |  0
7930: .ve


7933: .seealso: MatCreateSubMatrices(), MatCreateSubMatricesMPI(), MatCreateSubMatrixVirtual(), MatSubMatrixVirtualUpdate()
7934: @*/
7935: PetscErrorCode MatCreateSubMatrix(Mat mat,IS isrow,IS iscol,MatReuse cll,Mat *newmat)
7936: {
7938:   PetscMPIInt    size;
7939:   Mat            *local;
7940:   IS             iscoltmp;
7941:   PetscBool      flg;

7950:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
7951:   if (cll == MAT_IGNORE_MATRIX) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Cannot use MAT_IGNORE_MATRIX");

7953:   MatCheckPreallocated(mat,1);
7954:   MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);

7956:   if (!iscol || isrow == iscol) {
7957:     PetscBool   stride;
7958:     PetscMPIInt grabentirematrix = 0,grab;
7959:     PetscObjectTypeCompare((PetscObject)isrow,ISSTRIDE,&stride);
7960:     if (stride) {
7961:       PetscInt first,step,n,rstart,rend;
7962:       ISStrideGetInfo(isrow,&first,&step);
7963:       if (step == 1) {
7964:         MatGetOwnershipRange(mat,&rstart,&rend);
7965:         if (rstart == first) {
7966:           ISGetLocalSize(isrow,&n);
7967:           if (n == rend-rstart) {
7968:             grabentirematrix = 1;
7969:           }
7970:         }
7971:       }
7972:     }
7973:     MPIU_Allreduce(&grabentirematrix,&grab,1,MPI_INT,MPI_MIN,PetscObjectComm((PetscObject)mat));
7974:     if (grab) {
7975:       PetscInfo(mat,"Getting entire matrix as submatrix\n");
7976:       if (cll == MAT_INITIAL_MATRIX) {
7977:         *newmat = mat;
7978:         PetscObjectReference((PetscObject)mat);
7979:       }
7980:       return(0);
7981:     }
7982:   }

7984:   if (!iscol) {
7985:     ISCreateStride(PetscObjectComm((PetscObject)mat),mat->cmap->n,mat->cmap->rstart,1,&iscoltmp);
7986:   } else {
7987:     iscoltmp = iscol;
7988:   }

7990:   /* if original matrix is on just one processor then use submatrix generated */
7991:   if (mat->ops->createsubmatrices && !mat->ops->createsubmatrix && size == 1 && cll == MAT_REUSE_MATRIX) {
7992:     MatCreateSubMatrices(mat,1,&isrow,&iscoltmp,MAT_REUSE_MATRIX,&newmat);
7993:     goto setproperties;
7994:   } else if (mat->ops->createsubmatrices && !mat->ops->createsubmatrix && size == 1) {
7995:     MatCreateSubMatrices(mat,1,&isrow,&iscoltmp,MAT_INITIAL_MATRIX,&local);
7996:     *newmat = *local;
7997:     PetscFree(local);
7998:     goto setproperties;
7999:   } else if (!mat->ops->createsubmatrix) {
8000:     /* Create a new matrix type that implements the operation using the full matrix */
8001:     PetscLogEventBegin(MAT_CreateSubMat,mat,0,0,0);
8002:     switch (cll) {
8003:     case MAT_INITIAL_MATRIX:
8004:       MatCreateSubMatrixVirtual(mat,isrow,iscoltmp,newmat);
8005:       break;
8006:     case MAT_REUSE_MATRIX:
8007:       MatSubMatrixVirtualUpdate(*newmat,mat,isrow,iscoltmp);
8008:       break;
8009:     default: SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"Invalid MatReuse, must be either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX");
8010:     }
8011:     PetscLogEventEnd(MAT_CreateSubMat,mat,0,0,0);
8012:     goto setproperties;
8013:   }

8015:   if (!mat->ops->createsubmatrix) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
8016:   PetscLogEventBegin(MAT_CreateSubMat,mat,0,0,0);
8017:   (*mat->ops->createsubmatrix)(mat,isrow,iscoltmp,cll,newmat);
8018:   PetscLogEventEnd(MAT_CreateSubMat,mat,0,0,0);

8020: setproperties:
8021:   ISEqualUnsorted(isrow,iscoltmp,&flg);
8022:   if (flg) {
8023:     MatPropagateSymmetryOptions(mat,*newmat);
8024:   }
8025:   if (!iscol) {ISDestroy(&iscoltmp);}
8026:   if (*newmat && cll == MAT_INITIAL_MATRIX) {PetscObjectStateIncrease((PetscObject)*newmat);}
8027:   return(0);
8028: }

8030: /*@
8031:    MatPropagateSymmetryOptions - Propagates symmetry options set on a matrix to another matrix

8033:    Not Collective

8035:    Input Parameters:
8036: +  A - the matrix we wish to propagate options from
8037: -  B - the matrix we wish to propagate options to

8039:    Level: beginner

8041:    Notes: Propagates the options associated to MAT_SYMMETRY_ETERNAL, MAT_STRUCTURALLY_SYMMETRIC, MAT_HERMITIAN, MAT_SPD and MAT_SYMMETRIC

8043: .seealso: MatSetOption()
8044: @*/
8045: PetscErrorCode MatPropagateSymmetryOptions(Mat A, Mat B)
8046: {

8052:   if (A->symmetric_eternal) { /* symmetric_eternal does not have a corresponding *set flag */
8053:     MatSetOption(B,MAT_SYMMETRY_ETERNAL,A->symmetric_eternal);
8054:   }
8055:   if (A->structurally_symmetric_set) {
8056:     MatSetOption(B,MAT_STRUCTURALLY_SYMMETRIC,A->structurally_symmetric);
8057:   }
8058:   if (A->hermitian_set) {
8059:     MatSetOption(B,MAT_HERMITIAN,A->hermitian);
8060:   }
8061:   if (A->spd_set) {
8062:     MatSetOption(B,MAT_SPD,A->spd);
8063:   }
8064:   if (A->symmetric_set) {
8065:     MatSetOption(B,MAT_SYMMETRIC,A->symmetric);
8066:   }
8067:   return(0);
8068: }

8070: /*@
8071:    MatStashSetInitialSize - sets the sizes of the matrix stash, that is
8072:    used during the assembly process to store values that belong to
8073:    other processors.

8075:    Not Collective

8077:    Input Parameters:
8078: +  mat   - the matrix
8079: .  size  - the initial size of the stash.
8080: -  bsize - the initial size of the block-stash(if used).

8082:    Options Database Keys:
8083: +   -matstash_initial_size <size> or <size0,size1,...sizep-1>
8084: -   -matstash_block_initial_size <bsize>  or <bsize0,bsize1,...bsizep-1>

8086:    Level: intermediate

8088:    Notes:
8089:      The block-stash is used for values set with MatSetValuesBlocked() while
8090:      the stash is used for values set with MatSetValues()

8092:      Run with the option -info and look for output of the form
8093:      MatAssemblyBegin_MPIXXX:Stash has MM entries, uses nn mallocs.
8094:      to determine the appropriate value, MM, to use for size and
8095:      MatAssemblyBegin_MPIXXX:Block-Stash has BMM entries, uses nn mallocs.
8096:      to determine the value, BMM to use for bsize


8099: .seealso: MatAssemblyBegin(), MatAssemblyEnd(), Mat, MatStashGetInfo()

8101: @*/
8102: PetscErrorCode MatStashSetInitialSize(Mat mat,PetscInt size, PetscInt bsize)
8103: {

8109:   MatStashSetInitialSize_Private(&mat->stash,size);
8110:   MatStashSetInitialSize_Private(&mat->bstash,bsize);
8111:   return(0);
8112: }

8114: /*@
8115:    MatInterpolateAdd - w = y + A*x or A'*x depending on the shape of
8116:      the matrix

8118:    Neighbor-wise Collective on Mat

8120:    Input Parameters:
8121: +  mat   - the matrix
8122: .  x,y - the vectors
8123: -  w - where the result is stored

8125:    Level: intermediate

8127:    Notes:
8128:     w may be the same vector as y.

8130:     This allows one to use either the restriction or interpolation (its transpose)
8131:     matrix to do the interpolation

8133: .seealso: MatMultAdd(), MatMultTransposeAdd(), MatRestrict()

8135: @*/
8136: PetscErrorCode MatInterpolateAdd(Mat A,Vec x,Vec y,Vec w)
8137: {
8139:   PetscInt       M,N,Ny;

8147:   MatCheckPreallocated(A,1);
8148:   MatGetSize(A,&M,&N);
8149:   VecGetSize(y,&Ny);
8150:   if (M == Ny) {
8151:     MatMultAdd(A,x,y,w);
8152:   } else {
8153:     MatMultTransposeAdd(A,x,y,w);
8154:   }
8155:   return(0);
8156: }

8158: /*@
8159:    MatInterpolate - y = A*x or A'*x depending on the shape of
8160:      the matrix

8162:    Neighbor-wise Collective on Mat

8164:    Input Parameters:
8165: +  mat   - the matrix
8166: -  x,y - the vectors

8168:    Level: intermediate

8170:    Notes:
8171:     This allows one to use either the restriction or interpolation (its transpose)
8172:     matrix to do the interpolation

8174: .seealso: MatMultAdd(), MatMultTransposeAdd(), MatRestrict()

8176: @*/
8177: PetscErrorCode MatInterpolate(Mat A,Vec x,Vec y)
8178: {
8180:   PetscInt       M,N,Ny;

8187:   MatCheckPreallocated(A,1);
8188:   MatGetSize(A,&M,&N);
8189:   VecGetSize(y,&Ny);
8190:   if (M == Ny) {
8191:     MatMult(A,x,y);
8192:   } else {
8193:     MatMultTranspose(A,x,y);
8194:   }
8195:   return(0);
8196: }

8198: /*@
8199:    MatRestrict - y = A*x or A'*x

8201:    Neighbor-wise Collective on Mat

8203:    Input Parameters:
8204: +  mat   - the matrix
8205: -  x,y - the vectors

8207:    Level: intermediate

8209:    Notes:
8210:     This allows one to use either the restriction or interpolation (its transpose)
8211:     matrix to do the restriction

8213: .seealso: MatMultAdd(), MatMultTransposeAdd(), MatInterpolate()

8215: @*/
8216: PetscErrorCode MatRestrict(Mat A,Vec x,Vec y)
8217: {
8219:   PetscInt       M,N,Ny;

8226:   MatCheckPreallocated(A,1);

8228:   MatGetSize(A,&M,&N);
8229:   VecGetSize(y,&Ny);
8230:   if (M == Ny) {
8231:     MatMult(A,x,y);
8232:   } else {
8233:     MatMultTranspose(A,x,y);
8234:   }
8235:   return(0);
8236: }

8238: /*@
8239:    MatGetNullSpace - retrieves the null space of a matrix.

8241:    Logically Collective on Mat

8243:    Input Parameters:
8244: +  mat - the matrix
8245: -  nullsp - the null space object

8247:    Level: developer

8249: .seealso: MatCreate(), MatNullSpaceCreate(), MatSetNearNullSpace(), MatSetNullSpace()
8250: @*/
8251: PetscErrorCode MatGetNullSpace(Mat mat, MatNullSpace *nullsp)
8252: {
8256:   *nullsp = (mat->symmetric_set && mat->symmetric && !mat->nullsp) ? mat->transnullsp : mat->nullsp;
8257:   return(0);
8258: }

8260: /*@
8261:    MatSetNullSpace - attaches a null space to a matrix.

8263:    Logically Collective on Mat

8265:    Input Parameters:
8266: +  mat - the matrix
8267: -  nullsp - the null space object

8269:    Level: advanced

8271:    Notes:
8272:       This null space is used by the linear solvers. Overwrites any previous null space that may have been attached

8274:       For inconsistent singular systems (linear systems where the right hand side is not in the range of the operator) you also likely should
8275:       call MatSetTransposeNullSpace(). This allows the linear system to be solved in a least squares sense.

8277:       You can remove the null space by calling this routine with an nullsp of NULL


8280:       The fundamental theorem of linear algebra (Gilbert Strang, Introduction to Applied Mathematics, page 72) states that
8281:    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).
8282:    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
8283:    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
8284:    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).

8286:       Krylov solvers can produce the minimal norm solution to the least squares problem by utilizing MatNullSpaceRemove().

8288:     If the matrix is known to be symmetric because it is an SBAIJ matrix or one as called MatSetOption(mat,MAT_SYMMETRIC or MAT_SYMMETRIC_ETERNAL,PETSC_TRUE); this
8289:     routine also automatically calls MatSetTransposeNullSpace().

8291: .seealso: MatCreate(), MatNullSpaceCreate(), MatSetNearNullSpace(), MatGetNullSpace(), MatSetTransposeNullSpace(), MatGetTransposeNullSpace(), MatNullSpaceRemove()
8292: @*/
8293: PetscErrorCode MatSetNullSpace(Mat mat,MatNullSpace nullsp)
8294: {

8300:   if (nullsp) {PetscObjectReference((PetscObject)nullsp);}
8301:   MatNullSpaceDestroy(&mat->nullsp);
8302:   mat->nullsp = nullsp;
8303:   if (mat->symmetric_set && mat->symmetric) {
8304:     MatSetTransposeNullSpace(mat,nullsp);
8305:   }
8306:   return(0);
8307: }

8309: /*@
8310:    MatGetTransposeNullSpace - retrieves the null space of the transpose of a matrix.

8312:    Logically Collective on Mat

8314:    Input Parameters:
8315: +  mat - the matrix
8316: -  nullsp - the null space object

8318:    Level: developer

8320: .seealso: MatCreate(), MatNullSpaceCreate(), MatSetNearNullSpace(), MatSetTransposeNullSpace(), MatSetNullSpace(), MatGetNullSpace()
8321: @*/
8322: PetscErrorCode MatGetTransposeNullSpace(Mat mat, MatNullSpace *nullsp)
8323: {
8328:   *nullsp = (mat->symmetric_set && mat->symmetric && !mat->transnullsp) ? mat->nullsp : mat->transnullsp;
8329:   return(0);
8330: }

8332: /*@
8333:    MatSetTransposeNullSpace - attaches a null space to a matrix.

8335:    Logically Collective on Mat

8337:    Input Parameters:
8338: +  mat - the matrix
8339: -  nullsp - the null space object

8341:    Level: advanced

8343:    Notes:
8344:       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.
8345:       You must also call MatSetNullSpace()


8348:       The fundamental theorem of linear algebra (Gilbert Strang, Introduction to Applied Mathematics, page 72) states that
8349:    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).
8350:    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
8351:    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
8352:    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).

8354:       Krylov solvers can produce the minimal norm solution to the least squares problem by utilizing MatNullSpaceRemove().

8356: .seealso: MatCreate(), MatNullSpaceCreate(), MatSetNearNullSpace(), MatGetNullSpace(), MatSetNullSpace(), MatGetTransposeNullSpace(), MatNullSpaceRemove()
8357: @*/
8358: PetscErrorCode MatSetTransposeNullSpace(Mat mat,MatNullSpace nullsp)
8359: {

8365:   if (nullsp) {PetscObjectReference((PetscObject)nullsp);}
8366:   MatNullSpaceDestroy(&mat->transnullsp);
8367:   mat->transnullsp = nullsp;
8368:   return(0);
8369: }

8371: /*@
8372:    MatSetNearNullSpace - attaches a null space to a matrix, which is often the null space (rigid body modes) of the operator without boundary conditions
8373:         This null space will be used to provide near null space vectors to a multigrid preconditioner built from this matrix.

8375:    Logically Collective on Mat

8377:    Input Parameters:
8378: +  mat - the matrix
8379: -  nullsp - the null space object

8381:    Level: advanced

8383:    Notes:
8384:       Overwrites any previous near null space that may have been attached

8386:       You can remove the null space by calling this routine with an nullsp of NULL

8388: .seealso: MatCreate(), MatNullSpaceCreate(), MatSetNullSpace(), MatNullSpaceCreateRigidBody(), MatGetNearNullSpace()
8389: @*/
8390: PetscErrorCode MatSetNearNullSpace(Mat mat,MatNullSpace nullsp)
8391: {

8398:   MatCheckPreallocated(mat,1);
8399:   if (nullsp) {PetscObjectReference((PetscObject)nullsp);}
8400:   MatNullSpaceDestroy(&mat->nearnullsp);
8401:   mat->nearnullsp = nullsp;
8402:   return(0);
8403: }

8405: /*@
8406:    MatGetNearNullSpace - Get null space attached with MatSetNearNullSpace()

8408:    Not Collective

8410:    Input Parameter:
8411: .  mat - the matrix

8413:    Output Parameter:
8414: .  nullsp - the null space object, NULL if not set

8416:    Level: developer

8418: .seealso: MatSetNearNullSpace(), MatGetNullSpace(), MatNullSpaceCreate()
8419: @*/
8420: PetscErrorCode MatGetNearNullSpace(Mat mat,MatNullSpace *nullsp)
8421: {
8426:   MatCheckPreallocated(mat,1);
8427:   *nullsp = mat->nearnullsp;
8428:   return(0);
8429: }

8431: /*@C
8432:    MatICCFactor - Performs in-place incomplete Cholesky factorization of matrix.

8434:    Collective on Mat

8436:    Input Parameters:
8437: +  mat - the matrix
8438: .  row - row/column permutation
8439: .  fill - expected fill factor >= 1.0
8440: -  level - level of fill, for ICC(k)

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

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

8450:    Level: developer


8453: .seealso: MatICCFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor()

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

8458: @*/
8459: PetscErrorCode MatICCFactor(Mat mat,IS row,const MatFactorInfo *info)
8460: {

8468:   if (mat->rmap->N != mat->cmap->N) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONG,"matrix must be square");
8469:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8470:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8471:   if (!mat->ops->iccfactor) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
8472:   MatCheckPreallocated(mat,1);
8473:   (*mat->ops->iccfactor)(mat,row,info);
8474:   PetscObjectStateIncrease((PetscObject)mat);
8475:   return(0);
8476: }

8478: /*@
8479:    MatDiagonalScaleLocal - Scales columns of a matrix given the scaling values including the
8480:          ghosted ones.

8482:    Not Collective

8484:    Input Parameters:
8485: +  mat - the matrix
8486: -  diag = the diagonal values, including ghost ones

8488:    Level: developer

8490:    Notes:
8491:     Works only for MPIAIJ and MPIBAIJ matrices

8493: .seealso: MatDiagonalScale()
8494: @*/
8495: PetscErrorCode MatDiagonalScaleLocal(Mat mat,Vec diag)
8496: {
8498:   PetscMPIInt    size;


8505:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Matrix must be already assembled");
8506:   PetscLogEventBegin(MAT_Scale,mat,0,0,0);
8507:   MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);
8508:   if (size == 1) {
8509:     PetscInt n,m;
8510:     VecGetSize(diag,&n);
8511:     MatGetSize(mat,NULL,&m);
8512:     if (m == n) {
8513:       MatDiagonalScale(mat,NULL,diag);
8514:     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only supported for sequential matrices when no ghost points/periodic conditions");
8515:   } else {
8516:     PetscUseMethod(mat,"MatDiagonalScaleLocal_C",(Mat,Vec),(mat,diag));
8517:   }
8518:   PetscLogEventEnd(MAT_Scale,mat,0,0,0);
8519:   PetscObjectStateIncrease((PetscObject)mat);
8520:   return(0);
8521: }

8523: /*@
8524:    MatGetInertia - Gets the inertia from a factored matrix

8526:    Collective on Mat

8528:    Input Parameter:
8529: .  mat - the matrix

8531:    Output Parameters:
8532: +   nneg - number of negative eigenvalues
8533: .   nzero - number of zero eigenvalues
8534: -   npos - number of positive eigenvalues

8536:    Level: advanced

8538:    Notes:
8539:     Matrix must have been factored by MatCholeskyFactor()


8542: @*/
8543: PetscErrorCode MatGetInertia(Mat mat,PetscInt *nneg,PetscInt *nzero,PetscInt *npos)
8544: {

8550:   if (!mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
8551:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Numeric factor mat is not assembled");
8552:   if (!mat->ops->getinertia) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
8553:   (*mat->ops->getinertia)(mat,nneg,nzero,npos);
8554:   return(0);
8555: }

8557: /* ----------------------------------------------------------------*/
8558: /*@C
8559:    MatSolves - Solves A x = b, given a factored matrix, for a collection of vectors

8561:    Neighbor-wise Collective on Mats

8563:    Input Parameters:
8564: +  mat - the factored matrix
8565: -  b - the right-hand-side vectors

8567:    Output Parameter:
8568: .  x - the result vectors

8570:    Notes:
8571:    The vectors b and x cannot be the same.  I.e., one cannot
8572:    call MatSolves(A,x,x).

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

8579:    Level: developer

8581: .seealso: MatSolveAdd(), MatSolveTranspose(), MatSolveTransposeAdd(), MatSolve()
8582: @*/
8583: PetscErrorCode MatSolves(Mat mat,Vecs b,Vecs x)
8584: {

8590:   if (x == b) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"x and b must be different vectors");
8591:   if (!mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
8592:   if (!mat->rmap->N && !mat->cmap->N) return(0);

8594:   if (!mat->ops->solves) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
8595:   MatCheckPreallocated(mat,1);
8596:   PetscLogEventBegin(MAT_Solves,mat,0,0,0);
8597:   (*mat->ops->solves)(mat,b,x);
8598:   PetscLogEventEnd(MAT_Solves,mat,0,0,0);
8599:   return(0);
8600: }

8602: /*@
8603:    MatIsSymmetric - Test whether a matrix is symmetric

8605:    Collective on Mat

8607:    Input Parameter:
8608: +  A - the matrix to test
8609: -  tol - difference between value and its transpose less than this amount counts as equal (use 0.0 for exact transpose)

8611:    Output Parameters:
8612: .  flg - the result

8614:    Notes:
8615:     For real numbers MatIsSymmetric() and MatIsHermitian() return identical results

8617:    Level: intermediate

8619: .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), MatIsSymmetricKnown()
8620: @*/
8621: PetscErrorCode MatIsSymmetric(Mat A,PetscReal tol,PetscBool  *flg)
8622: {


8629:   if (!A->symmetric_set) {
8630:     if (!A->ops->issymmetric) {
8631:       MatType mattype;
8632:       MatGetType(A,&mattype);
8633:       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type %s does not support checking for symmetric",mattype);
8634:     }
8635:     (*A->ops->issymmetric)(A,tol,flg);
8636:     if (!tol) {
8637:       MatSetOption(A,MAT_SYMMETRIC,*flg);
8638:     }
8639:   } else if (A->symmetric) {
8640:     *flg = PETSC_TRUE;
8641:   } else if (!tol) {
8642:     *flg = PETSC_FALSE;
8643:   } else {
8644:     if (!A->ops->issymmetric) {
8645:       MatType mattype;
8646:       MatGetType(A,&mattype);
8647:       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type %s does not support checking for symmetric",mattype);
8648:     }
8649:     (*A->ops->issymmetric)(A,tol,flg);
8650:   }
8651:   return(0);
8652: }

8654: /*@
8655:    MatIsHermitian - Test whether a matrix is Hermitian

8657:    Collective on Mat

8659:    Input Parameter:
8660: +  A - the matrix to test
8661: -  tol - difference between value and its transpose less than this amount counts as equal (use 0.0 for exact Hermitian)

8663:    Output Parameters:
8664: .  flg - the result

8666:    Level: intermediate

8668: .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(),
8669:           MatIsSymmetricKnown(), MatIsSymmetric()
8670: @*/
8671: PetscErrorCode MatIsHermitian(Mat A,PetscReal tol,PetscBool  *flg)
8672: {


8679:   if (!A->hermitian_set) {
8680:     if (!A->ops->ishermitian) {
8681:       MatType mattype;
8682:       MatGetType(A,&mattype);
8683:       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type %s does not support checking for hermitian",mattype);
8684:     }
8685:     (*A->ops->ishermitian)(A,tol,flg);
8686:     if (!tol) {
8687:       MatSetOption(A,MAT_HERMITIAN,*flg);
8688:     }
8689:   } else if (A->hermitian) {
8690:     *flg = PETSC_TRUE;
8691:   } else if (!tol) {
8692:     *flg = PETSC_FALSE;
8693:   } else {
8694:     if (!A->ops->ishermitian) {
8695:       MatType mattype;
8696:       MatGetType(A,&mattype);
8697:       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type %s does not support checking for hermitian",mattype);
8698:     }
8699:     (*A->ops->ishermitian)(A,tol,flg);
8700:   }
8701:   return(0);
8702: }

8704: /*@
8705:    MatIsSymmetricKnown - Checks the flag on the matrix to see if it is symmetric.

8707:    Not Collective

8709:    Input Parameter:
8710: .  A - the matrix to check

8712:    Output Parameters:
8713: +  set - if the symmetric flag is set (this tells you if the next flag is valid)
8714: -  flg - the result

8716:    Level: advanced

8718:    Note: Does not check the matrix values directly, so this may return unknown (set = PETSC_FALSE). Use MatIsSymmetric()
8719:          if you want it explicitly checked

8721: .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), MatIsSymmetric()
8722: @*/
8723: PetscErrorCode MatIsSymmetricKnown(Mat A,PetscBool *set,PetscBool *flg)
8724: {
8729:   if (A->symmetric_set) {
8730:     *set = PETSC_TRUE;
8731:     *flg = A->symmetric;
8732:   } else {
8733:     *set = PETSC_FALSE;
8734:   }
8735:   return(0);
8736: }

8738: /*@
8739:    MatIsHermitianKnown - Checks the flag on the matrix to see if it is hermitian.

8741:    Not Collective

8743:    Input Parameter:
8744: .  A - the matrix to check

8746:    Output Parameters:
8747: +  set - if the hermitian flag is set (this tells you if the next flag is valid)
8748: -  flg - the result

8750:    Level: advanced

8752:    Note: Does not check the matrix values directly, so this may return unknown (set = PETSC_FALSE). Use MatIsHermitian()
8753:          if you want it explicitly checked

8755: .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), MatIsSymmetric()
8756: @*/
8757: PetscErrorCode MatIsHermitianKnown(Mat A,PetscBool *set,PetscBool *flg)
8758: {
8763:   if (A->hermitian_set) {
8764:     *set = PETSC_TRUE;
8765:     *flg = A->hermitian;
8766:   } else {
8767:     *set = PETSC_FALSE;
8768:   }
8769:   return(0);
8770: }

8772: /*@
8773:    MatIsStructurallySymmetric - Test whether a matrix is structurally symmetric

8775:    Collective on Mat

8777:    Input Parameter:
8778: .  A - the matrix to test

8780:    Output Parameters:
8781: .  flg - the result

8783:    Level: intermediate

8785: .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsSymmetric(), MatSetOption()
8786: @*/
8787: PetscErrorCode MatIsStructurallySymmetric(Mat A,PetscBool *flg)
8788: {

8794:   if (!A->structurally_symmetric_set) {
8795:     if (!A->ops->isstructurallysymmetric) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Matrix of type %s does not support checking for structural symmetric",((PetscObject)A)->type_name);
8796:     (*A->ops->isstructurallysymmetric)(A,flg);
8797:     MatSetOption(A,MAT_STRUCTURALLY_SYMMETRIC,*flg);
8798:   } else *flg = A->structurally_symmetric;
8799:   return(0);
8800: }

8802: /*@
8803:    MatStashGetInfo - Gets how many values are currently in the matrix stash, i.e. need
8804:        to be communicated to other processors during the MatAssemblyBegin/End() process

8806:     Not collective

8808:    Input Parameter:
8809: .   vec - the vector

8811:    Output Parameters:
8812: +   nstash   - the size of the stash
8813: .   reallocs - the number of additional mallocs incurred.
8814: .   bnstash   - the size of the block stash
8815: -   breallocs - the number of additional mallocs incurred.in the block stash

8817:    Level: advanced

8819: .seealso: MatAssemblyBegin(), MatAssemblyEnd(), Mat, MatStashSetInitialSize()

8821: @*/
8822: PetscErrorCode MatStashGetInfo(Mat mat,PetscInt *nstash,PetscInt *reallocs,PetscInt *bnstash,PetscInt *breallocs)
8823: {

8827:   MatStashGetInfo_Private(&mat->stash,nstash,reallocs);
8828:   MatStashGetInfo_Private(&mat->bstash,bnstash,breallocs);
8829:   return(0);
8830: }

8832: /*@C
8833:    MatCreateVecs - Get vector(s) compatible with the matrix, i.e. with the same
8834:      parallel layout

8836:    Collective on Mat

8838:    Input Parameter:
8839: .  mat - the matrix

8841:    Output Parameter:
8842: +   right - (optional) vector that the matrix can be multiplied against
8843: -   left - (optional) vector that the matrix vector product can be stored in

8845:    Notes:
8846:     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().

8848:   Notes:
8849:     These are new vectors which are not owned by the Mat, they should be destroyed in VecDestroy() when no longer needed

8851:   Level: advanced

8853: .seealso: MatCreate(), VecDestroy()
8854: @*/
8855: PetscErrorCode MatCreateVecs(Mat mat,Vec *right,Vec *left)
8856: {

8862:   if (mat->ops->getvecs) {
8863:     (*mat->ops->getvecs)(mat,right,left);
8864:   } else {
8865:     PetscInt rbs,cbs;
8866:     MatGetBlockSizes(mat,&rbs,&cbs);
8867:     if (right) {
8868:       if (mat->cmap->n < 0) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"PetscLayout for columns not yet setup");
8869:       VecCreate(PetscObjectComm((PetscObject)mat),right);
8870:       VecSetSizes(*right,mat->cmap->n,PETSC_DETERMINE);
8871:       VecSetBlockSize(*right,cbs);
8872:       VecSetType(*right,mat->defaultvectype);
8873:       PetscLayoutReference(mat->cmap,&(*right)->map);
8874:     }
8875:     if (left) {
8876:       if (mat->rmap->n < 0) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"PetscLayout for rows not yet setup");
8877:       VecCreate(PetscObjectComm((PetscObject)mat),left);
8878:       VecSetSizes(*left,mat->rmap->n,PETSC_DETERMINE);
8879:       VecSetBlockSize(*left,rbs);
8880:       VecSetType(*left,mat->defaultvectype);
8881:       PetscLayoutReference(mat->rmap,&(*left)->map);
8882:     }
8883:   }
8884:   return(0);
8885: }

8887: /*@C
8888:    MatFactorInfoInitialize - Initializes a MatFactorInfo data structure
8889:      with default values.

8891:    Not Collective

8893:    Input Parameters:
8894: .    info - the MatFactorInfo data structure


8897:    Notes:
8898:     The solvers are generally used through the KSP and PC objects, for example
8899:           PCLU, PCILU, PCCHOLESKY, PCICC

8901:    Level: developer

8903: .seealso: MatFactorInfo

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

8908: @*/

8910: PetscErrorCode MatFactorInfoInitialize(MatFactorInfo *info)
8911: {

8915:   PetscMemzero(info,sizeof(MatFactorInfo));
8916:   return(0);
8917: }

8919: /*@
8920:    MatFactorSetSchurIS - Set indices corresponding to the Schur complement you wish to have computed

8922:    Collective on Mat

8924:    Input Parameters:
8925: +  mat - the factored matrix
8926: -  is - the index set defining the Schur indices (0-based)

8928:    Notes:
8929:     Call MatFactorSolveSchurComplement() or MatFactorSolveSchurComplementTranspose() after this call to solve a Schur complement system.

8931:    You can call MatFactorGetSchurComplement() or MatFactorCreateSchurComplement() after this call.

8933:    Level: developer

8935: .seealso: MatGetFactor(), MatFactorGetSchurComplement(), MatFactorRestoreSchurComplement(), MatFactorCreateSchurComplement(), MatFactorSolveSchurComplement(),
8936:           MatFactorSolveSchurComplementTranspose(), MatFactorSolveSchurComplement()

8938: @*/
8939: PetscErrorCode MatFactorSetSchurIS(Mat mat,IS is)
8940: {
8941:   PetscErrorCode ierr,(*f)(Mat,IS);

8949:   if (!mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Only for factored matrix");
8950:   PetscObjectQueryFunction((PetscObject)mat,"MatFactorSetSchurIS_C",&f);
8951:   if (!f) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"The selected MatSolverType does not support Schur complement computation. You should use MATSOLVERMUMPS or MATSOLVERMKL_PARDISO");
8952:   MatDestroy(&mat->schur);
8953:   (*f)(mat,is);
8954:   if (!mat->schur) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_PLIB,"Schur complement has not been created");
8955:   return(0);
8956: }

8958: /*@
8959:   MatFactorCreateSchurComplement - Create a Schur complement matrix object using Schur data computed during the factorization step

8961:    Logically Collective on Mat

8963:    Input Parameters:
8964: +  F - the factored matrix obtained by calling MatGetFactor() from PETSc-MUMPS interface
8965: .  S - location where to return the Schur complement, can be NULL
8966: -  status - the status of the Schur complement matrix, can be NULL

8968:    Notes:
8969:    You must call MatFactorSetSchurIS() before calling this routine.

8971:    The routine provides a copy of the Schur matrix stored within the solver data structures.
8972:    The caller must destroy the object when it is no longer needed.
8973:    If MatFactorInvertSchurComplement() has been called, the routine gets back the inverse.

8975:    Use MatFactorGetSchurComplement() to get access to the Schur complement matrix inside the factored matrix instead of making a copy of it (which this function does)

8977:    Developer Notes:
8978:     The reason this routine exists is because the representation of the Schur complement within the factor matrix may be different than a standard PETSc
8979:    matrix representation and we normally do not want to use the time or memory to make a copy as a regular PETSc matrix.

8981:    See MatCreateSchurComplement() or MatGetSchurComplement() for ways to create virtual or approximate Schur complements.

8983:    Level: advanced

8985:    References:

8987: .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorGetSchurComplement(), MatFactorSchurStatus
8988: @*/
8989: PetscErrorCode MatFactorCreateSchurComplement(Mat F,Mat* S,MatFactorSchurStatus* status)
8990: {

8997:   if (S) {
8998:     PetscErrorCode (*f)(Mat,Mat*);

9000:     PetscObjectQueryFunction((PetscObject)F,"MatFactorCreateSchurComplement_C",&f);
9001:     if (f) {
9002:       (*f)(F,S);
9003:     } else {
9004:       MatDuplicate(F->schur,MAT_COPY_VALUES,S);
9005:     }
9006:   }
9007:   if (status) *status = F->schur_status;
9008:   return(0);
9009: }

9011: /*@
9012:   MatFactorGetSchurComplement - Gets access to a Schur complement matrix using the current Schur data within a factored matrix

9014:    Logically Collective on Mat

9016:    Input Parameters:
9017: +  F - the factored matrix obtained by calling MatGetFactor()
9018: .  *S - location where to return the Schur complement, can be NULL
9019: -  status - the status of the Schur complement matrix, can be NULL

9021:    Notes:
9022:    You must call MatFactorSetSchurIS() before calling this routine.

9024:    Schur complement mode is currently implemented for sequential matrices.
9025:    The routine returns a the Schur Complement stored within the data strutures of the solver.
9026:    If MatFactorInvertSchurComplement() has previously been called, the returned matrix is actually the inverse of the Schur complement.
9027:    The returned matrix should not be destroyed; the caller should call MatFactorRestoreSchurComplement() when the object is no longer needed.

9029:    Use MatFactorCreateSchurComplement() to create a copy of the Schur complement matrix that is within a factored matrix

9031:    See MatCreateSchurComplement() or MatGetSchurComplement() for ways to create virtual or approximate Schur complements.

9033:    Level: advanced

9035:    References:

9037: .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorRestoreSchurComplement(), MatFactorCreateSchurComplement(), MatFactorSchurStatus
9038: @*/
9039: PetscErrorCode MatFactorGetSchurComplement(Mat F,Mat* S,MatFactorSchurStatus* status)
9040: {
9045:   if (S) *S = F->schur;
9046:   if (status) *status = F->schur_status;
9047:   return(0);
9048: }

9050: /*@
9051:   MatFactorRestoreSchurComplement - Restore the Schur complement matrix object obtained from a call to MatFactorGetSchurComplement

9053:    Logically Collective on Mat

9055:    Input Parameters:
9056: +  F - the factored matrix obtained by calling MatGetFactor()
9057: .  *S - location where the Schur complement is stored
9058: -  status - the status of the Schur complement matrix (see MatFactorSchurStatus)

9060:    Notes:

9062:    Level: advanced

9064:    References:

9066: .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorRestoreSchurComplement(), MatFactorCreateSchurComplement(), MatFactorSchurStatus
9067: @*/
9068: PetscErrorCode MatFactorRestoreSchurComplement(Mat F,Mat* S,MatFactorSchurStatus status)
9069: {

9074:   if (S) {
9076:     *S = NULL;
9077:   }
9078:   F->schur_status = status;
9079:   MatFactorUpdateSchurStatus_Private(F);
9080:   return(0);
9081: }

9083: /*@
9084:   MatFactorSolveSchurComplementTranspose - Solve the transpose of the Schur complement system computed during the factorization step

9086:    Logically Collective on Mat

9088:    Input Parameters:
9089: +  F - the factored matrix obtained by calling MatGetFactor()
9090: .  rhs - location where the right hand side of the Schur complement system is stored
9091: -  sol - location where the solution of the Schur complement system has to be returned

9093:    Notes:
9094:    The sizes of the vectors should match the size of the Schur complement

9096:    Must be called after MatFactorSetSchurIS()

9098:    Level: advanced

9100:    References:

9102: .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorSolveSchurComplement()
9103: @*/
9104: PetscErrorCode MatFactorSolveSchurComplementTranspose(Mat F, Vec rhs, Vec sol)
9105: {

9117:   MatFactorFactorizeSchurComplement(F);
9118:   switch (F->schur_status) {
9119:   case MAT_FACTOR_SCHUR_FACTORED:
9120:     MatSolveTranspose(F->schur,rhs,sol);
9121:     break;
9122:   case MAT_FACTOR_SCHUR_INVERTED:
9123:     MatMultTranspose(F->schur,rhs,sol);
9124:     break;
9125:   default:
9126:     SETERRQ1(PetscObjectComm((PetscObject)F),PETSC_ERR_SUP,"Unhandled MatFactorSchurStatus %D",F->schur_status);
9127:   }
9128:   return(0);
9129: }

9131: /*@
9132:   MatFactorSolveSchurComplement - Solve the Schur complement system computed during the factorization step

9134:    Logically Collective on Mat

9136:    Input Parameters:
9137: +  F - the factored matrix obtained by calling MatGetFactor()
9138: .  rhs - location where the right hand side of the Schur complement system is stored
9139: -  sol - location where the solution of the Schur complement system has to be returned

9141:    Notes:
9142:    The sizes of the vectors should match the size of the Schur complement

9144:    Must be called after MatFactorSetSchurIS()

9146:    Level: advanced

9148:    References:

9150: .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorSolveSchurComplementTranspose()
9151: @*/
9152: PetscErrorCode MatFactorSolveSchurComplement(Mat F, Vec rhs, Vec sol)
9153: {

9165:   MatFactorFactorizeSchurComplement(F);
9166:   switch (F->schur_status) {
9167:   case MAT_FACTOR_SCHUR_FACTORED:
9168:     MatSolve(F->schur,rhs,sol);
9169:     break;
9170:   case MAT_FACTOR_SCHUR_INVERTED:
9171:     MatMult(F->schur,rhs,sol);
9172:     break;
9173:   default:
9174:     SETERRQ1(PetscObjectComm((PetscObject)F),PETSC_ERR_SUP,"Unhandled MatFactorSchurStatus %D",F->schur_status);
9175:   }
9176:   return(0);
9177: }

9179: /*@
9180:   MatFactorInvertSchurComplement - Invert the Schur complement matrix computed during the factorization step

9182:    Logically Collective on Mat

9184:    Input Parameters:
9185: .  F - the factored matrix obtained by calling MatGetFactor()

9187:    Notes:
9188:     Must be called after MatFactorSetSchurIS().

9190:    Call MatFactorGetSchurComplement() or  MatFactorCreateSchurComplement() AFTER this call to actually compute the inverse and get access to it.

9192:    Level: advanced

9194:    References:

9196: .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorGetSchurComplement(), MatFactorCreateSchurComplement()
9197: @*/
9198: PetscErrorCode MatFactorInvertSchurComplement(Mat F)
9199: {

9205:   if (F->schur_status == MAT_FACTOR_SCHUR_INVERTED) return(0);
9206:   MatFactorFactorizeSchurComplement(F);
9207:   MatFactorInvertSchurComplement_Private(F);
9208:   F->schur_status = MAT_FACTOR_SCHUR_INVERTED;
9209:   return(0);
9210: }

9212: /*@
9213:   MatFactorFactorizeSchurComplement - Factorize the Schur complement matrix computed during the factorization step

9215:    Logically Collective on Mat

9217:    Input Parameters:
9218: .  F - the factored matrix obtained by calling MatGetFactor()

9220:    Notes:
9221:     Must be called after MatFactorSetSchurIS().

9223:    Level: advanced

9225:    References:

9227: .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorInvertSchurComplement()
9228: @*/
9229: PetscErrorCode MatFactorFactorizeSchurComplement(Mat F)
9230: {

9236:   if (F->schur_status == MAT_FACTOR_SCHUR_INVERTED || F->schur_status == MAT_FACTOR_SCHUR_FACTORED) return(0);
9237:   MatFactorFactorizeSchurComplement_Private(F);
9238:   F->schur_status = MAT_FACTOR_SCHUR_FACTORED;
9239:   return(0);
9240: }

9242: /*@
9243:    MatPtAP - Creates the matrix product C = P^T * A * P

9245:    Neighbor-wise Collective on Mat

9247:    Input Parameters:
9248: +  A - the matrix
9249: .  P - the projection matrix
9250: .  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
9251: -  fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(P)), use PETSC_DEFAULT if you do not have a good estimate
9252:           if the result is a dense matrix this is irrelevent

9254:    Output Parameters:
9255: .  C - the product matrix

9257:    Notes:
9258:    C will be created and must be destroyed by the user with MatDestroy().

9260:    For matrix types without special implementation the function fallbacks to MatMatMult() followed by MatTransposeMatMult().

9262:    Level: intermediate

9264: .seealso: MatMatMult(), MatRARt()
9265: @*/
9266: PetscErrorCode MatPtAP(Mat A,Mat P,MatReuse scall,PetscReal fill,Mat *C)
9267: {

9271:   if (scall == MAT_REUSE_MATRIX) MatCheckProduct(*C,5);
9272:   if (scall == MAT_INPLACE_MATRIX) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Inplace product not supported");

9274:   if (scall == MAT_INITIAL_MATRIX) {
9275:     MatProductCreate(A,P,NULL,C);
9276:     MatProductSetType(*C,MATPRODUCT_PtAP);
9277:     MatProductSetAlgorithm(*C,"default");
9278:     MatProductSetFill(*C,fill);

9280:     (*C)->product->api_user = PETSC_TRUE;
9281:     MatProductSetFromOptions(*C);
9282:     if (!(*C)->ops->productsymbolic) SETERRQ3(PetscObjectComm((PetscObject)(*C)),PETSC_ERR_SUP,"MatProduct %s not supported for A %s and P %s",MatProductTypes[MATPRODUCT_PtAP],((PetscObject)A)->type_name,((PetscObject)P)->type_name);
9283:     MatProductSymbolic(*C);
9284:   } else { /* scall == MAT_REUSE_MATRIX */
9285:     MatProductReplaceMats(A,P,NULL,*C);
9286:   }

9288:   MatProductNumeric(*C);
9289:   if (A->symmetric_set && A->symmetric) {
9290:     MatSetOption(*C,MAT_SYMMETRIC,PETSC_TRUE);
9291:   }
9292:   return(0);
9293: }

9295: /*@
9296:    MatRARt - Creates the matrix product C = R * A * R^T

9298:    Neighbor-wise Collective on Mat

9300:    Input Parameters:
9301: +  A - the matrix
9302: .  R - the projection matrix
9303: .  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
9304: -  fill - expected fill as ratio of nnz(C)/nnz(A), use PETSC_DEFAULT if you do not have a good estimate
9305:           if the result is a dense matrix this is irrelevent

9307:    Output Parameters:
9308: .  C - the product matrix

9310:    Notes:
9311:    C will be created and must be destroyed by the user with MatDestroy().

9313:    This routine is currently only implemented for pairs of AIJ matrices and classes
9314:    which inherit from AIJ. Due to PETSc sparse matrix block row distribution among processes,
9315:    parallel MatRARt is implemented via explicit transpose of R, which could be very expensive.
9316:    We recommend using MatPtAP().

9318:    Level: intermediate

9320: .seealso: MatMatMult(), MatPtAP()
9321: @*/
9322: PetscErrorCode MatRARt(Mat A,Mat R,MatReuse scall,PetscReal fill,Mat *C)
9323: {

9327:   if (scall == MAT_REUSE_MATRIX) MatCheckProduct(*C,5);
9328:   if (scall == MAT_INPLACE_MATRIX) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Inplace product not supported");

9330:   if (scall == MAT_INITIAL_MATRIX) {
9331:     MatProductCreate(A,R,NULL,C);
9332:     MatProductSetType(*C,MATPRODUCT_RARt);
9333:     MatProductSetAlgorithm(*C,"default");
9334:     MatProductSetFill(*C,fill);

9336:     (*C)->product->api_user = PETSC_TRUE;
9337:     MatProductSetFromOptions(*C);
9338:     if (!(*C)->ops->productsymbolic) SETERRQ3(PetscObjectComm((PetscObject)(*C)),PETSC_ERR_SUP,"MatProduct %s not supported for A %s and R %s",MatProductTypes[MATPRODUCT_RARt],((PetscObject)A)->type_name,((PetscObject)R)->type_name);
9339:     MatProductSymbolic(*C);
9340:   } else { /* scall == MAT_REUSE_MATRIX */
9341:     MatProductReplaceMats(A,R,NULL,*C);
9342:   }

9344:   MatProductNumeric(*C);
9345:   if (A->symmetric_set && A->symmetric) {
9346:     MatSetOption(*C,MAT_SYMMETRIC,PETSC_TRUE);
9347:   }
9348:   return(0);
9349: }


9352: static PetscErrorCode MatProduct_Private(Mat A,Mat B,MatReuse scall,PetscReal fill,MatProductType ptype, Mat *C)
9353: {

9357:   if (scall == MAT_INPLACE_MATRIX) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Inplace product not supported");

9359:   if (scall == MAT_INITIAL_MATRIX) {
9360:     PetscInfo1(A,"Calling MatProduct API with MAT_INITIAL_MATRIX and product type %s\n",MatProductTypes[ptype]);
9361:     MatProductCreate(A,B,NULL,C);
9362:     MatProductSetType(*C,ptype);
9363:     MatProductSetAlgorithm(*C,MATPRODUCTALGORITHM_DEFAULT);
9364:     MatProductSetFill(*C,fill);

9366:     (*C)->product->api_user = PETSC_TRUE;
9367:     MatProductSetFromOptions(*C);
9368:     MatProductSymbolic(*C);
9369:   } else { /* scall == MAT_REUSE_MATRIX */
9370:     Mat_Product *product = (*C)->product;

9372:     PetscInfo2(A,"Calling MatProduct API with MAT_REUSE_MATRIX %s product present and product type %s\n",product ? "with" : "without",MatProductTypes[ptype]);
9373:     if (!product) {
9374:       /* user provide the dense matrix *C without calling MatProductCreate() */
9375:       PetscBool isdense;

9377:       PetscObjectBaseTypeCompareAny((PetscObject)(*C),&isdense,MATSEQDENSE,MATMPIDENSE,"");
9378:       if (isdense) {
9379:         /* user wants to reuse an assembled dense matrix */
9380:         /* Create product -- see MatCreateProduct() */
9381:         MatProductCreate_Private(A,B,NULL,*C);
9382:         product = (*C)->product;
9383:         product->fill     = fill;
9384:         product->api_user = PETSC_TRUE;
9385:         product->clear    = PETSC_TRUE;

9387:         MatProductSetType(*C,ptype);
9388:         MatProductSetFromOptions(*C);
9389:         if (!(*C)->ops->productsymbolic) SETERRQ3(PetscObjectComm((PetscObject)(*C)),PETSC_ERR_SUP,"MatProduct %s not supported for %s and %s",MatProductTypes[ptype],((PetscObject)A)->type_name,((PetscObject)B)->type_name);
9390:         MatProductSymbolic(*C);
9391:       } else SETERRQ(PetscObjectComm((PetscObject)(*C)),PETSC_ERR_SUP,"Call MatProductCreate() first");
9392:     } else { /* user may change input matrices A or B when REUSE */
9393:       MatProductReplaceMats(A,B,NULL,*C);
9394:     }
9395:   }
9396:   MatProductNumeric(*C);
9397:   return(0);
9398: }

9400: /*@
9401:    MatMatMult - Performs Matrix-Matrix Multiplication C=A*B.

9403:    Neighbor-wise Collective on Mat

9405:    Input Parameters:
9406: +  A - the left matrix
9407: .  B - the right matrix
9408: .  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
9409: -  fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if you do not have a good estimate
9410:           if the result is a dense matrix this is irrelevent

9412:    Output Parameters:
9413: .  C - the product matrix

9415:    Notes:
9416:    Unless scall is MAT_REUSE_MATRIX C will be created.

9418:    MAT_REUSE_MATRIX can only be used if the matrices A and B have the same nonzero pattern as in the previous call and C was obtained from a previous
9419:    call to this function with MAT_INITIAL_MATRIX.

9421:    To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value actually needed.

9423:    If you have many matrices with the same non-zero structure to multiply, you should use MatProductCreate()/MatProductSymbolic(C)/ReplaceMats(), and call MatProductNumeric() repeatedly.

9425:    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 with MAT_REUSE_MATRIX, rather than first having MatMatMult() create it for you. You can NEVER do this if the matrix C is sparse.

9427:    Level: intermediate

9429: .seealso: MatTransposeMatMult(), MatMatTransposeMult(), MatPtAP()
9430: @*/
9431: PetscErrorCode MatMatMult(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
9432: {

9436:   MatProduct_Private(A,B,scall,fill,MATPRODUCT_AB,C);
9437:   return(0);
9438: }

9440: /*@
9441:    MatMatTransposeMult - Performs Matrix-Matrix Multiplication C=A*B^T.

9443:    Neighbor-wise Collective on Mat

9445:    Input Parameters:
9446: +  A - the left matrix
9447: .  B - the right matrix
9448: .  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
9449: -  fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if not known

9451:    Output Parameters:
9452: .  C - the product matrix

9454:    Notes:
9455:    C will be created if MAT_INITIAL_MATRIX and must be destroyed by the user with MatDestroy().

9457:    MAT_REUSE_MATRIX can only be used if the matrices A and B have the same nonzero pattern as in the previous call

9459:   To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value
9460:    actually needed.

9462:    This routine is currently only implemented for pairs of SeqAIJ matrices, for the SeqDense class,
9463:    and for pairs of MPIDense matrices.

9465:    Options Database Keys:
9466: .  -matmattransmult_mpidense_mpidense_via {allgatherv,cyclic} - Choose between algorthims for MPIDense matrices: the
9467:                                                                 first redundantly copies the transposed B matrix on each process and requiers O(log P) communication complexity;
9468:                                                                 the second never stores more than one portion of the B matrix at a time by requires O(P) communication complexity.

9470:    Level: intermediate

9472: .seealso: MatMatMult(), MatTransposeMatMult() MatPtAP()
9473: @*/
9474: PetscErrorCode MatMatTransposeMult(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
9475: {

9479:   MatProduct_Private(A,B,scall,fill,MATPRODUCT_ABt,C);
9480:   return(0);
9481: }

9483: /*@
9484:    MatTransposeMatMult - Performs Matrix-Matrix Multiplication C=A^T*B.

9486:    Neighbor-wise Collective on Mat

9488:    Input Parameters:
9489: +  A - the left matrix
9490: .  B - the right matrix
9491: .  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
9492: -  fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if not known

9494:    Output Parameters:
9495: .  C - the product matrix

9497:    Notes:
9498:    C will be created if MAT_INITIAL_MATRIX and must be destroyed by the user with MatDestroy().

9500:    MAT_REUSE_MATRIX can only be used if the matrices A and B have the same nonzero pattern as in the previous call.

9502:   To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value
9503:    actually needed.

9505:    This routine is currently implemented for pairs of AIJ matrices and pairs of SeqDense matrices and classes
9506:    which inherit from SeqAIJ.  C will be of same type as the input matrices.

9508:    Level: intermediate

9510: .seealso: MatMatMult(), MatMatTransposeMult(), MatPtAP()
9511: @*/
9512: PetscErrorCode MatTransposeMatMult(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
9513: {

9517:   MatProduct_Private(A,B,scall,fill,MATPRODUCT_AtB,C);
9518:   return(0);
9519: }

9521: /*@
9522:    MatMatMatMult - Performs Matrix-Matrix-Matrix Multiplication D=A*B*C.

9524:    Neighbor-wise Collective on Mat

9526:    Input Parameters:
9527: +  A - the left matrix
9528: .  B - the middle matrix
9529: .  C - the right matrix
9530: .  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
9531: -  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
9532:           if the result is a dense matrix this is irrelevent

9534:    Output Parameters:
9535: .  D - the product matrix

9537:    Notes:
9538:    Unless scall is MAT_REUSE_MATRIX D will be created.

9540:    MAT_REUSE_MATRIX can only be used if the matrices A, B and C have the same nonzero pattern as in the previous call

9542:    To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value
9543:    actually needed.

9545:    If you have many matrices with the same non-zero structure to multiply, you
9546:    should use MAT_REUSE_MATRIX in all calls but the first or

9548:    Level: intermediate

9550: .seealso: MatMatMult, MatPtAP()
9551: @*/
9552: PetscErrorCode MatMatMatMult(Mat A,Mat B,Mat C,MatReuse scall,PetscReal fill,Mat *D)
9553: {

9557:   if (scall == MAT_REUSE_MATRIX) MatCheckProduct(*D,6);
9558:   if (scall == MAT_INPLACE_MATRIX) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Inplace product not supported");

9560:   if (scall == MAT_INITIAL_MATRIX) {
9561:     MatProductCreate(A,B,C,D);
9562:     MatProductSetType(*D,MATPRODUCT_ABC);
9563:     MatProductSetAlgorithm(*D,"default");
9564:     MatProductSetFill(*D,fill);

9566:     (*D)->product->api_user = PETSC_TRUE;
9567:     MatProductSetFromOptions(*D);
9568:     if (!(*D)->ops->productsymbolic) SETERRQ4(PetscObjectComm((PetscObject)(*D)),PETSC_ERR_SUP,"MatProduct %s not supported for A %s, B %s and C %s",MatProductTypes[MATPRODUCT_ABC],((PetscObject)A)->type_name,((PetscObject)B)->type_name,((PetscObject)C)->type_name);
9569:     MatProductSymbolic(*D);
9570:   } else { /* user may change input matrices when REUSE */
9571:     MatProductReplaceMats(A,B,C,*D);
9572:   }
9573:   MatProductNumeric(*D);
9574:   return(0);
9575: }

9577: /*@
9578:    MatCreateRedundantMatrix - Create redundant matrices and put them into processors of subcommunicators.

9580:    Collective on Mat

9582:    Input Parameters:
9583: +  mat - the matrix
9584: .  nsubcomm - the number of subcommunicators (= number of redundant parallel or sequential matrices)
9585: .  subcomm - MPI communicator split from the communicator where mat resides in (or MPI_COMM_NULL if nsubcomm is used)
9586: -  reuse - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX

9588:    Output Parameter:
9589: .  matredundant - redundant matrix

9591:    Notes:
9592:    MAT_REUSE_MATRIX can only be used when the nonzero structure of the
9593:    original matrix has not changed from that last call to MatCreateRedundantMatrix().

9595:    This routine creates the duplicated matrices in subcommunicators; you should NOT create them before
9596:    calling it.

9598:    Level: advanced


9601: .seealso: MatDestroy()
9602: @*/
9603: PetscErrorCode MatCreateRedundantMatrix(Mat mat,PetscInt nsubcomm,MPI_Comm subcomm,MatReuse reuse,Mat *matredundant)
9604: {
9606:   MPI_Comm       comm;
9607:   PetscMPIInt    size;
9608:   PetscInt       mloc_sub,nloc_sub,rstart,rend,M=mat->rmap->N,N=mat->cmap->N,bs=mat->rmap->bs;
9609:   Mat_Redundant  *redund=NULL;
9610:   PetscSubcomm   psubcomm=NULL;
9611:   MPI_Comm       subcomm_in=subcomm;
9612:   Mat            *matseq;
9613:   IS             isrow,iscol;
9614:   PetscBool      newsubcomm=PETSC_FALSE;

9618:   if (nsubcomm && reuse == MAT_REUSE_MATRIX) {
9621:   }

9623:   MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);
9624:   if (size == 1 || nsubcomm == 1) {
9625:     if (reuse == MAT_INITIAL_MATRIX) {
9626:       MatDuplicate(mat,MAT_COPY_VALUES,matredundant);
9627:     } else {
9628:       if (*matredundant == mat) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"MAT_REUSE_MATRIX means reuse the matrix passed in as the final argument, not the original matrix");
9629:       MatCopy(mat,*matredundant,SAME_NONZERO_PATTERN);
9630:     }
9631:     return(0);
9632:   }

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

9638:   PetscLogEventBegin(MAT_RedundantMat,mat,0,0,0);
9639:   if (subcomm_in == MPI_COMM_NULL && reuse == MAT_INITIAL_MATRIX) { /* get subcomm if user does not provide subcomm */
9640:     /* create psubcomm, then get subcomm */
9641:     PetscObjectGetComm((PetscObject)mat,&comm);
9642:     MPI_Comm_size(comm,&size);
9643:     if (nsubcomm < 1 || nsubcomm > size) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"nsubcomm must between 1 and %D",size);

9645:     PetscSubcommCreate(comm,&psubcomm);
9646:     PetscSubcommSetNumber(psubcomm,nsubcomm);
9647:     PetscSubcommSetType(psubcomm,PETSC_SUBCOMM_CONTIGUOUS);
9648:     PetscSubcommSetFromOptions(psubcomm);
9649:     PetscCommDuplicate(PetscSubcommChild(psubcomm),&subcomm,NULL);
9650:     newsubcomm = PETSC_TRUE;
9651:     PetscSubcommDestroy(&psubcomm);
9652:   }

9654:   /* get isrow, iscol and a local sequential matrix matseq[0] */
9655:   if (reuse == MAT_INITIAL_MATRIX) {
9656:     mloc_sub = PETSC_DECIDE;
9657:     nloc_sub = PETSC_DECIDE;
9658:     if (bs < 1) {
9659:       PetscSplitOwnership(subcomm,&mloc_sub,&M);
9660:       PetscSplitOwnership(subcomm,&nloc_sub,&N);
9661:     } else {
9662:       PetscSplitOwnershipBlock(subcomm,bs,&mloc_sub,&M);
9663:       PetscSplitOwnershipBlock(subcomm,bs,&nloc_sub,&N);
9664:     }
9665:     MPI_Scan(&mloc_sub,&rend,1,MPIU_INT,MPI_SUM,subcomm);
9666:     rstart = rend - mloc_sub;
9667:     ISCreateStride(PETSC_COMM_SELF,mloc_sub,rstart,1,&isrow);
9668:     ISCreateStride(PETSC_COMM_SELF,N,0,1,&iscol);
9669:   } else { /* reuse == MAT_REUSE_MATRIX */
9670:     if (*matredundant == mat) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"MAT_REUSE_MATRIX means reuse the matrix passed in as the final argument, not the original matrix");
9671:     /* retrieve subcomm */
9672:     PetscObjectGetComm((PetscObject)(*matredundant),&subcomm);
9673:     redund = (*matredundant)->redundant;
9674:     isrow  = redund->isrow;
9675:     iscol  = redund->iscol;
9676:     matseq = redund->matseq;
9677:   }
9678:   MatCreateSubMatrices(mat,1,&isrow,&iscol,reuse,&matseq);

9680:   /* get matredundant over subcomm */
9681:   if (reuse == MAT_INITIAL_MATRIX) {
9682:     MatCreateMPIMatConcatenateSeqMat(subcomm,matseq[0],nloc_sub,reuse,matredundant);

9684:     /* create a supporting struct and attach it to C for reuse */
9685:     PetscNewLog(*matredundant,&redund);
9686:     (*matredundant)->redundant = redund;
9687:     redund->isrow              = isrow;
9688:     redund->iscol              = iscol;
9689:     redund->matseq             = matseq;
9690:     if (newsubcomm) {
9691:       redund->subcomm          = subcomm;
9692:     } else {
9693:       redund->subcomm          = MPI_COMM_NULL;
9694:     }
9695:   } else {
9696:     MatCreateMPIMatConcatenateSeqMat(subcomm,matseq[0],PETSC_DECIDE,reuse,matredundant);
9697:   }
9698:   PetscLogEventEnd(MAT_RedundantMat,mat,0,0,0);
9699:   return(0);
9700: }

9702: /*@C
9703:    MatGetMultiProcBlock - Create multiple [bjacobi] 'parallel submatrices' from
9704:    a given 'mat' object. Each submatrix can span multiple procs.

9706:    Collective on Mat

9708:    Input Parameters:
9709: +  mat - the matrix
9710: .  subcomm - the subcommunicator obtained by com_split(comm)
9711: -  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX

9713:    Output Parameter:
9714: .  subMat - 'parallel submatrices each spans a given subcomm

9716:   Notes:
9717:   The submatrix partition across processors is dictated by 'subComm' a
9718:   communicator obtained by com_split(comm). The comm_split
9719:   is not restriced to be grouped with consecutive original ranks.

9721:   Due the comm_split() usage, the parallel layout of the submatrices
9722:   map directly to the layout of the original matrix [wrt the local
9723:   row,col partitioning]. So the original 'DiagonalMat' naturally maps
9724:   into the 'DiagonalMat' of the subMat, hence it is used directly from
9725:   the subMat. However the offDiagMat looses some columns - and this is
9726:   reconstructed with MatSetValues()

9728:   Level: advanced


9731: .seealso: MatCreateSubMatrices()
9732: @*/
9733: PetscErrorCode   MatGetMultiProcBlock(Mat mat, MPI_Comm subComm, MatReuse scall,Mat *subMat)
9734: {
9736:   PetscMPIInt    commsize,subCommSize;

9739:   MPI_Comm_size(PetscObjectComm((PetscObject)mat),&commsize);
9740:   MPI_Comm_size(subComm,&subCommSize);
9741:   if (subCommSize > commsize) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"CommSize %D < SubCommZize %D",commsize,subCommSize);

9743:   if (scall == MAT_REUSE_MATRIX && *subMat == mat) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"MAT_REUSE_MATRIX means reuse the matrix passed in as the final argument, not the original matrix");
9744:   PetscLogEventBegin(MAT_GetMultiProcBlock,mat,0,0,0);
9745:   (*mat->ops->getmultiprocblock)(mat,subComm,scall,subMat);
9746:   PetscLogEventEnd(MAT_GetMultiProcBlock,mat,0,0,0);
9747:   return(0);
9748: }

9750: /*@
9751:    MatGetLocalSubMatrix - Gets a reference to a submatrix specified in local numbering

9753:    Not Collective

9755:    Input Arguments:
9756: +  mat - matrix to extract local submatrix from
9757: .  isrow - local row indices for submatrix
9758: -  iscol - local column indices for submatrix

9760:    Output Arguments:
9761: .  submat - the submatrix

9763:    Level: intermediate

9765:    Notes:
9766:    The submat should be returned with MatRestoreLocalSubMatrix().

9768:    Depending on the format of mat, the returned submat may not implement MatMult().  Its communicator may be
9769:    the same as mat, it may be PETSC_COMM_SELF, or some other subcomm of mat's.

9771:    The submat always implements MatSetValuesLocal().  If isrow and iscol have the same block size, then
9772:    MatSetValuesBlockedLocal() will also be implemented.

9774:    The mat must have had a ISLocalToGlobalMapping provided to it with MatSetLocalToGlobalMapping(). Note that
9775:    matrices obtained with DMCreateMatrix() generally already have the local to global mapping provided.

9777: .seealso: MatRestoreLocalSubMatrix(), MatCreateLocalRef(), MatSetLocalToGlobalMapping()
9778: @*/
9779: PetscErrorCode MatGetLocalSubMatrix(Mat mat,IS isrow,IS iscol,Mat *submat)
9780: {

9789:   if (!mat->rmap->mapping) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Matrix must have local to global mapping provided before this call");

9791:   if (mat->ops->getlocalsubmatrix) {
9792:     (*mat->ops->getlocalsubmatrix)(mat,isrow,iscol,submat);
9793:   } else {
9794:     MatCreateLocalRef(mat,isrow,iscol,submat);
9795:   }
9796:   return(0);
9797: }

9799: /*@
9800:    MatRestoreLocalSubMatrix - Restores a reference to a submatrix specified in local numbering

9802:    Not Collective

9804:    Input Arguments:
9805:    mat - matrix to extract local submatrix from
9806:    isrow - local row indices for submatrix
9807:    iscol - local column indices for submatrix
9808:    submat - the submatrix

9810:    Level: intermediate

9812: .seealso: MatGetLocalSubMatrix()
9813: @*/
9814: PetscErrorCode MatRestoreLocalSubMatrix(Mat mat,IS isrow,IS iscol,Mat *submat)
9815: {

9824:   if (*submat) {
9826:   }

9828:   if (mat->ops->restorelocalsubmatrix) {
9829:     (*mat->ops->restorelocalsubmatrix)(mat,isrow,iscol,submat);
9830:   } else {
9831:     MatDestroy(submat);
9832:   }
9833:   *submat = NULL;
9834:   return(0);
9835: }

9837: /* --------------------------------------------------------*/
9838: /*@
9839:    MatFindZeroDiagonals - Finds all the rows of a matrix that have zero or no diagonal entry in the matrix

9841:    Collective on Mat

9843:    Input Parameter:
9844: .  mat - the matrix

9846:    Output Parameter:
9847: .  is - if any rows have zero diagonals this contains the list of them

9849:    Level: developer

9851: .seealso: MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd()
9852: @*/
9853: PetscErrorCode MatFindZeroDiagonals(Mat mat,IS *is)
9854: {

9860:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
9861:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");

9863:   if (!mat->ops->findzerodiagonals) {
9864:     Vec                diag;
9865:     const PetscScalar *a;
9866:     PetscInt          *rows;
9867:     PetscInt           rStart, rEnd, r, nrow = 0;

9869:     MatCreateVecs(mat, &diag, NULL);
9870:     MatGetDiagonal(mat, diag);
9871:     MatGetOwnershipRange(mat, &rStart, &rEnd);
9872:     VecGetArrayRead(diag, &a);
9873:     for (r = 0; r < rEnd-rStart; ++r) if (a[r] == 0.0) ++nrow;
9874:     PetscMalloc1(nrow, &rows);
9875:     nrow = 0;
9876:     for (r = 0; r < rEnd-rStart; ++r) if (a[r] == 0.0) rows[nrow++] = r+rStart;
9877:     VecRestoreArrayRead(diag, &a);
9878:     VecDestroy(&diag);
9879:     ISCreateGeneral(PetscObjectComm((PetscObject) mat), nrow, rows, PETSC_OWN_POINTER, is);
9880:   } else {
9881:     (*mat->ops->findzerodiagonals)(mat, is);
9882:   }
9883:   return(0);
9884: }

9886: /*@
9887:    MatFindOffBlockDiagonalEntries - Finds all the rows of a matrix that have entries outside of the main diagonal block (defined by the matrix block size)

9889:    Collective on Mat

9891:    Input Parameter:
9892: .  mat - the matrix

9894:    Output Parameter:
9895: .  is - contains the list of rows with off block diagonal entries

9897:    Level: developer

9899: .seealso: MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd()
9900: @*/
9901: PetscErrorCode MatFindOffBlockDiagonalEntries(Mat mat,IS *is)
9902: {

9908:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
9909:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");

9911:   if (!mat->ops->findoffblockdiagonalentries) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Matrix type %s does not have a find off block diagonal entries defined",((PetscObject)mat)->type_name);
9912:   (*mat->ops->findoffblockdiagonalentries)(mat,is);
9913:   return(0);
9914: }

9916: /*@C
9917:   MatInvertBlockDiagonal - Inverts the block diagonal entries.

9919:   Collective on Mat

9921:   Input Parameters:
9922: . mat - the matrix

9924:   Output Parameters:
9925: . values - the block inverses in column major order (FORTRAN-like)

9927:    Note:
9928:    This routine is not available from Fortran.

9930:   Level: advanced

9932: .seealso: MatInvertBockDiagonalMat
9933: @*/
9934: PetscErrorCode MatInvertBlockDiagonal(Mat mat,const PetscScalar **values)
9935: {

9940:   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
9941:   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
9942:   if (!mat->ops->invertblockdiagonal) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not supported for type %s",((PetscObject)mat)->type_name);
9943:   (*mat->ops->invertblockdiagonal)(mat,values);
9944:   return(0);
9945: }

9947: /*@C
9948:   MatInvertVariableBlockDiagonal - Inverts the block diagonal entries.

9950:   Collective on Mat

9952:   Input Parameters:
9953: + mat - the matrix
9954: . nblocks - the number of blocks
9955: - bsizes - the size of each block

9957:   Output Parameters:
9958: . values - the block inverses in column major order (FORTRAN-like)

9960:    Note:
9961:    This routine is not available from Fortran.

9963:   Level: advanced

9965: .seealso: MatInvertBockDiagonal()
9966: @*/
9967: PetscErrorCode MatInvertVariableBlockDiagonal(Mat mat,PetscInt nblocks,const PetscInt *bsizes,PetscScalar *values)
9968: {

9973:   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
9974:   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
9975:   if (!mat->ops->invertvariableblockdiagonal) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not supported for type",((PetscObject)mat)->type_name);
9976:   (*mat->ops->invertvariableblockdiagonal)(mat,nblocks,bsizes,values);
9977:   return(0);
9978: }

9980: /*@
9981:   MatInvertBlockDiagonalMat - set matrix C to be the inverted block diagonal of matrix A

9983:   Collective on Mat

9985:   Input Parameters:
9986: . A - the matrix

9988:   Output Parameters:
9989: . C - matrix with inverted block diagonal of A.  This matrix should be created and may have its type set.

9991:   Notes: the blocksize of the matrix is used to determine the blocks on the diagonal of C

9993:   Level: advanced

9995: .seealso: MatInvertBockDiagonal()
9996: @*/
9997: PetscErrorCode MatInvertBlockDiagonalMat(Mat A,Mat C)
9998: {
9999:   PetscErrorCode     ierr;
10000:   const PetscScalar *vals;
10001:   PetscInt          *dnnz;
10002:   PetscInt           M,N,m,n,rstart,rend,bs,i,j;

10005:   MatInvertBlockDiagonal(A,&vals);
10006:   MatGetBlockSize(A,&bs);
10007:   MatGetSize(A,&M,&N);
10008:   MatGetLocalSize(A,&m,&n);
10009:   MatSetSizes(C,m,n,M,N);
10010:   MatSetBlockSize(C,bs);
10011:   PetscMalloc1(m/bs,&dnnz);
10012:   for (j = 0; j < m/bs; j++) dnnz[j] = 1;
10013:   MatXAIJSetPreallocation(C,bs,dnnz,NULL,NULL,NULL);
10014:   PetscFree(dnnz);
10015:   MatGetOwnershipRange(C,&rstart,&rend);
10016:   MatSetOption(C,MAT_ROW_ORIENTED,PETSC_FALSE);
10017:   for (i = rstart/bs; i < rend/bs; i++) {
10018:     MatSetValuesBlocked(C,1,&i,1,&i,&vals[(i-rstart/bs)*bs*bs],INSERT_VALUES);
10019:   }
10020:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
10021:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
10022:   MatSetOption(C,MAT_ROW_ORIENTED,PETSC_TRUE);
10023:   return(0);
10024: }

10026: /*@C
10027:     MatTransposeColoringDestroy - Destroys a coloring context for matrix product C=A*B^T that was created
10028:     via MatTransposeColoringCreate().

10030:     Collective on MatTransposeColoring

10032:     Input Parameter:
10033: .   c - coloring context

10035:     Level: intermediate

10037: .seealso: MatTransposeColoringCreate()
10038: @*/
10039: PetscErrorCode MatTransposeColoringDestroy(MatTransposeColoring *c)
10040: {
10041:   PetscErrorCode       ierr;
10042:   MatTransposeColoring matcolor=*c;

10045:   if (!matcolor) return(0);
10046:   if (--((PetscObject)matcolor)->refct > 0) {matcolor = NULL; return(0);}

10048:   PetscFree3(matcolor->ncolumns,matcolor->nrows,matcolor->colorforrow);
10049:   PetscFree(matcolor->rows);
10050:   PetscFree(matcolor->den2sp);
10051:   PetscFree(matcolor->colorforcol);
10052:   PetscFree(matcolor->columns);
10053:   if (matcolor->brows>0) {
10054:     PetscFree(matcolor->lstart);
10055:   }
10056:   PetscHeaderDestroy(c);
10057:   return(0);
10058: }

10060: /*@C
10061:     MatTransColoringApplySpToDen - Given a symbolic matrix product C=A*B^T for which
10062:     a MatTransposeColoring context has been created, computes a dense B^T by Apply
10063:     MatTransposeColoring to sparse B.

10065:     Collective on MatTransposeColoring

10067:     Input Parameters:
10068: +   B - sparse matrix B
10069: .   Btdense - symbolic dense matrix B^T
10070: -   coloring - coloring context created with MatTransposeColoringCreate()

10072:     Output Parameter:
10073: .   Btdense - dense matrix B^T

10075:     Level: advanced

10077:      Notes:
10078:     These are used internally for some implementations of MatRARt()

10080: .seealso: MatTransposeColoringCreate(), MatTransposeColoringDestroy(), MatTransColoringApplyDenToSp()

10082: @*/
10083: PetscErrorCode MatTransColoringApplySpToDen(MatTransposeColoring coloring,Mat B,Mat Btdense)
10084: {


10092:   if (!B->ops->transcoloringapplysptoden) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not supported for this matrix type %s",((PetscObject)B)->type_name);
10093:   (B->ops->transcoloringapplysptoden)(coloring,B,Btdense);
10094:   return(0);
10095: }

10097: /*@C
10098:     MatTransColoringApplyDenToSp - Given a symbolic matrix product Csp=A*B^T for which
10099:     a MatTransposeColoring context has been created and a dense matrix Cden=A*Btdense
10100:     in which Btdens is obtained from MatTransColoringApplySpToDen(), recover sparse matrix
10101:     Csp from Cden.

10103:     Collective on MatTransposeColoring

10105:     Input Parameters:
10106: +   coloring - coloring context created with MatTransposeColoringCreate()
10107: -   Cden - matrix product of a sparse matrix and a dense matrix Btdense

10109:     Output Parameter:
10110: .   Csp - sparse matrix

10112:     Level: advanced

10114:      Notes:
10115:     These are used internally for some implementations of MatRARt()

10117: .seealso: MatTransposeColoringCreate(), MatTransposeColoringDestroy(), MatTransColoringApplySpToDen()

10119: @*/
10120: PetscErrorCode MatTransColoringApplyDenToSp(MatTransposeColoring matcoloring,Mat Cden,Mat Csp)
10121: {


10129:   if (!Csp->ops->transcoloringapplydentosp) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not supported for this matrix type %s",((PetscObject)Csp)->type_name);
10130:   (Csp->ops->transcoloringapplydentosp)(matcoloring,Cden,Csp);
10131:   MatAssemblyBegin(Csp,MAT_FINAL_ASSEMBLY);
10132:   MatAssemblyEnd(Csp,MAT_FINAL_ASSEMBLY);
10133:   return(0);
10134: }

10136: /*@C
10137:    MatTransposeColoringCreate - Creates a matrix coloring context for matrix product C=A*B^T.

10139:    Collective on Mat

10141:    Input Parameters:
10142: +  mat - the matrix product C
10143: -  iscoloring - the coloring of the matrix; usually obtained with MatColoringCreate() or DMCreateColoring()

10145:     Output Parameter:
10146: .   color - the new coloring context

10148:     Level: intermediate

10150: .seealso: MatTransposeColoringDestroy(),  MatTransColoringApplySpToDen(),
10151:            MatTransColoringApplyDenToSp()
10152: @*/
10153: PetscErrorCode MatTransposeColoringCreate(Mat mat,ISColoring iscoloring,MatTransposeColoring *color)
10154: {
10155:   MatTransposeColoring c;
10156:   MPI_Comm             comm;
10157:   PetscErrorCode       ierr;

10160:   PetscLogEventBegin(MAT_TransposeColoringCreate,mat,0,0,0);
10161:   PetscObjectGetComm((PetscObject)mat,&comm);
10162:   PetscHeaderCreate(c,MAT_TRANSPOSECOLORING_CLASSID,"MatTransposeColoring","Matrix product C=A*B^T via coloring","Mat",comm,MatTransposeColoringDestroy,NULL);

10164:   c->ctype = iscoloring->ctype;
10165:   if (mat->ops->transposecoloringcreate) {
10166:     (*mat->ops->transposecoloringcreate)(mat,iscoloring,c);
10167:   } else SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Code not yet written for matrix type %s",((PetscObject)mat)->type_name);

10169:   *color = c;
10170:   PetscLogEventEnd(MAT_TransposeColoringCreate,mat,0,0,0);
10171:   return(0);
10172: }

10174: /*@
10175:       MatGetNonzeroState - Returns a 64 bit integer representing the current state of nonzeros in the matrix. If the
10176:         matrix has had no new nonzero locations added to the matrix since the previous call then the value will be the
10177:         same, otherwise it will be larger

10179:      Not Collective

10181:   Input Parameter:
10182: .    A  - the matrix

10184:   Output Parameter:
10185: .    state - the current state

10187:   Notes:
10188:     You can only compare states from two different calls to the SAME matrix, you cannot compare calls between
10189:          different matrices

10191:   Level: intermediate

10193: @*/
10194: PetscErrorCode MatGetNonzeroState(Mat mat,PetscObjectState *state)
10195: {
10198:   *state = mat->nonzerostate;
10199:   return(0);
10200: }

10202: /*@
10203:       MatCreateMPIMatConcatenateSeqMat - Creates a single large PETSc matrix by concatenating sequential
10204:                  matrices from each processor

10206:     Collective

10208:    Input Parameters:
10209: +    comm - the communicators the parallel matrix will live on
10210: .    seqmat - the input sequential matrices
10211: .    n - number of local columns (or PETSC_DECIDE)
10212: -    reuse - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX

10214:    Output Parameter:
10215: .    mpimat - the parallel matrix generated

10217:     Level: advanced

10219:    Notes:
10220:     The number of columns of the matrix in EACH processor MUST be the same.

10222: @*/
10223: PetscErrorCode MatCreateMPIMatConcatenateSeqMat(MPI_Comm comm,Mat seqmat,PetscInt n,MatReuse reuse,Mat *mpimat)
10224: {

10228:   if (!seqmat->ops->creatempimatconcatenateseqmat) SETERRQ1(PetscObjectComm((PetscObject)seqmat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)seqmat)->type_name);
10229:   if (reuse == MAT_REUSE_MATRIX && seqmat == *mpimat) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"MAT_REUSE_MATRIX means reuse the matrix passed in as the final argument, not the original matrix");

10231:   PetscLogEventBegin(MAT_Merge,seqmat,0,0,0);
10232:   (*seqmat->ops->creatempimatconcatenateseqmat)(comm,seqmat,n,reuse,mpimat);
10233:   PetscLogEventEnd(MAT_Merge,seqmat,0,0,0);
10234:   return(0);
10235: }

10237: /*@
10238:      MatSubdomainsCreateCoalesce - Creates index subdomains by coalescing adjacent
10239:                  ranks' ownership ranges.

10241:     Collective on A

10243:    Input Parameters:
10244: +    A   - the matrix to create subdomains from
10245: -    N   - requested number of subdomains


10248:    Output Parameters:
10249: +    n   - number of subdomains resulting on this rank
10250: -    iss - IS list with indices of subdomains on this rank

10252:     Level: advanced

10254:     Notes:
10255:     number of subdomains must be smaller than the communicator size
10256: @*/
10257: PetscErrorCode MatSubdomainsCreateCoalesce(Mat A,PetscInt N,PetscInt *n,IS *iss[])
10258: {
10259:   MPI_Comm        comm,subcomm;
10260:   PetscMPIInt     size,rank,color;
10261:   PetscInt        rstart,rend,k;
10262:   PetscErrorCode  ierr;

10265:   PetscObjectGetComm((PetscObject)A,&comm);
10266:   MPI_Comm_size(comm,&size);
10267:   MPI_Comm_rank(comm,&rank);
10268:   if (N < 1 || N >= (PetscInt)size) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"number of subdomains must be > 0 and < %D, got N = %D",size,N);
10269:   *n = 1;
10270:   k = ((PetscInt)size)/N + ((PetscInt)size%N>0); /* There are up to k ranks to a color */
10271:   color = rank/k;
10272:   MPI_Comm_split(comm,color,rank,&subcomm);
10273:   PetscMalloc1(1,iss);
10274:   MatGetOwnershipRange(A,&rstart,&rend);
10275:   ISCreateStride(subcomm,rend-rstart,rstart,1,iss[0]);
10276:   MPI_Comm_free(&subcomm);
10277:   return(0);
10278: }

10280: /*@
10281:    MatGalerkin - Constructs the coarse grid problem via Galerkin projection.

10283:    If the interpolation and restriction operators are the same, uses MatPtAP.
10284:    If they are not the same, use MatMatMatMult.

10286:    Once the coarse grid problem is constructed, correct for interpolation operators
10287:    that are not of full rank, which can legitimately happen in the case of non-nested
10288:    geometric multigrid.

10290:    Input Parameters:
10291: +  restrct - restriction operator
10292: .  dA - fine grid matrix
10293: .  interpolate - interpolation operator
10294: .  reuse - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
10295: -  fill - expected fill, use PETSC_DEFAULT if you do not have a good estimate

10297:    Output Parameters:
10298: .  A - the Galerkin coarse matrix

10300:    Options Database Key:
10301: .  -pc_mg_galerkin <both,pmat,mat,none>

10303:    Level: developer

10305: .seealso: MatPtAP(), MatMatMatMult()
10306: @*/
10307: PetscErrorCode  MatGalerkin(Mat restrct, Mat dA, Mat interpolate, MatReuse reuse, PetscReal fill, Mat *A)
10308: {
10310:   IS             zerorows;
10311:   Vec            diag;

10314:   if (reuse == MAT_INPLACE_MATRIX) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Inplace product not supported");
10315:   /* Construct the coarse grid matrix */
10316:   if (interpolate == restrct) {
10317:     MatPtAP(dA,interpolate,reuse,fill,A);
10318:   } else {
10319:     MatMatMatMult(restrct,dA,interpolate,reuse,fill,A);
10320:   }

10322:   /* If the interpolation matrix is not of full rank, A will have zero rows.
10323:      This can legitimately happen in the case of non-nested geometric multigrid.
10324:      In that event, we set the rows of the matrix to the rows of the identity,
10325:      ignoring the equations (as the RHS will also be zero). */

10327:   MatFindZeroRows(*A, &zerorows);

10329:   if (zerorows != NULL) { /* if there are any zero rows */
10330:     MatCreateVecs(*A, &diag, NULL);
10331:     MatGetDiagonal(*A, diag);
10332:     VecISSet(diag, zerorows, 1.0);
10333:     MatDiagonalSet(*A, diag, INSERT_VALUES);
10334:     VecDestroy(&diag);
10335:     ISDestroy(&zerorows);
10336:   }
10337:   return(0);
10338: }

10340: /*@C
10341:     MatSetOperation - Allows user to set a matrix operation for any matrix type

10343:    Logically Collective on Mat

10345:     Input Parameters:
10346: +   mat - the matrix
10347: .   op - the name of the operation
10348: -   f - the function that provides the operation

10350:    Level: developer

10352:     Usage:
10353: $      extern PetscErrorCode usermult(Mat,Vec,Vec);
10354: $      MatCreateXXX(comm,...&A);
10355: $      MatSetOperation(A,MATOP_MULT,(void(*)(void))usermult);

10357:     Notes:
10358:     See the file include/petscmat.h for a complete list of matrix
10359:     operations, which all have the form MATOP_<OPERATION>, where
10360:     <OPERATION> is the name (in all capital letters) of the
10361:     user interface routine (e.g., MatMult() -> MATOP_MULT).

10363:     All user-provided functions (except for MATOP_DESTROY) should have the same calling
10364:     sequence as the usual matrix interface routines, since they
10365:     are intended to be accessed via the usual matrix interface
10366:     routines, e.g.,
10367: $       MatMult(Mat,Vec,Vec) -> usermult(Mat,Vec,Vec)

10369:     In particular each function MUST return an error code of 0 on success and
10370:     nonzero on failure.

10372:     This routine is distinct from MatShellSetOperation() in that it can be called on any matrix type.

10374: .seealso: MatGetOperation(), MatCreateShell(), MatShellSetContext(), MatShellSetOperation()
10375: @*/
10376: PetscErrorCode MatSetOperation(Mat mat,MatOperation op,void (*f)(void))
10377: {
10380:   if (op == MATOP_VIEW && !mat->ops->viewnative && f != (void (*)(void))(mat->ops->view)) {
10381:     mat->ops->viewnative = mat->ops->view;
10382:   }
10383:   (((void(**)(void))mat->ops)[op]) = f;
10384:   return(0);
10385: }

10387: /*@C
10388:     MatGetOperation - Gets a matrix operation for any matrix type.

10390:     Not Collective

10392:     Input Parameters:
10393: +   mat - the matrix
10394: -   op - the name of the operation

10396:     Output Parameter:
10397: .   f - the function that provides the operation

10399:     Level: developer

10401:     Usage:
10402: $      PetscErrorCode (*usermult)(Mat,Vec,Vec);
10403: $      MatGetOperation(A,MATOP_MULT,(void(**)(void))&usermult);

10405:     Notes:
10406:     See the file include/petscmat.h for a complete list of matrix
10407:     operations, which all have the form MATOP_<OPERATION>, where
10408:     <OPERATION> is the name (in all capital letters) of the
10409:     user interface routine (e.g., MatMult() -> MATOP_MULT).

10411:     This routine is distinct from MatShellGetOperation() in that it can be called on any matrix type.

10413: .seealso: MatSetOperation(), MatCreateShell(), MatShellGetContext(), MatShellGetOperation()
10414: @*/
10415: PetscErrorCode MatGetOperation(Mat mat,MatOperation op,void(**f)(void))
10416: {
10419:   *f = (((void (**)(void))mat->ops)[op]);
10420:   return(0);
10421: }

10423: /*@
10424:     MatHasOperation - Determines whether the given matrix supports the particular
10425:     operation.

10427:    Not Collective

10429:    Input Parameters:
10430: +  mat - the matrix
10431: -  op - the operation, for example, MATOP_GET_DIAGONAL

10433:    Output Parameter:
10434: .  has - either PETSC_TRUE or PETSC_FALSE

10436:    Level: advanced

10438:    Notes:
10439:    See the file include/petscmat.h for a complete list of matrix
10440:    operations, which all have the form MATOP_<OPERATION>, where
10441:    <OPERATION> is the name (in all capital letters) of the
10442:    user-level routine.  E.g., MatNorm() -> MATOP_NORM.

10444: .seealso: MatCreateShell()
10445: @*/
10446: PetscErrorCode MatHasOperation(Mat mat,MatOperation op,PetscBool *has)
10447: {

10452:   /* symbolic product can be set before matrix type */
10455:   if (mat->ops->hasoperation) {
10456:     (*mat->ops->hasoperation)(mat,op,has);
10457:   } else {
10458:     if (((void**)mat->ops)[op]) *has =  PETSC_TRUE;
10459:     else {
10460:       *has = PETSC_FALSE;
10461:       if (op == MATOP_CREATE_SUBMATRIX) {
10462:         PetscMPIInt size;

10464:         MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);
10465:         if (size == 1) {
10466:           MatHasOperation(mat,MATOP_CREATE_SUBMATRICES,has);
10467:         }
10468:       }
10469:     }
10470:   }
10471:   return(0);
10472: }

10474: /*@
10475:     MatHasCongruentLayouts - Determines whether the rows and columns layouts
10476:     of the matrix are congruent

10478:    Collective on mat

10480:    Input Parameters:
10481: .  mat - the matrix

10483:    Output Parameter:
10484: .  cong - either PETSC_TRUE or PETSC_FALSE

10486:    Level: beginner

10488:    Notes:

10490: .seealso: MatCreate(), MatSetSizes()
10491: @*/
10492: PetscErrorCode MatHasCongruentLayouts(Mat mat,PetscBool *cong)
10493: {

10500:   if (!mat->rmap || !mat->cmap) {
10501:     *cong = mat->rmap == mat->cmap ? PETSC_TRUE : PETSC_FALSE;
10502:     return(0);
10503:   }
10504:   if (mat->congruentlayouts == PETSC_DECIDE) { /* first time we compare rows and cols layouts */
10505:     PetscLayoutCompare(mat->rmap,mat->cmap,cong);
10506:     if (*cong) mat->congruentlayouts = 1;
10507:     else       mat->congruentlayouts = 0;
10508:   } else *cong = mat->congruentlayouts ? PETSC_TRUE : PETSC_FALSE;
10509:   return(0);
10510: }

10512: PetscErrorCode MatSetInf(Mat A)
10513: {

10517:   if (!A->ops->setinf) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"No support for this operation for this matrix type");
10518:   (*A->ops->setinf)(A);
10519:   return(0);
10520: }