Actual source code: matrix.c

  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_QRFactorNumeric, MAT_QRFactorSymbolic, MAT_QRFactor;
 21: PetscLogEvent MAT_AssemblyEnd, MAT_SetValues, MAT_GetValues, MAT_GetRow, MAT_GetRowIJ, MAT_CreateSubMats, MAT_GetOrdering, MAT_RedundantMat, MAT_GetSeqNonzeroStructure;
 22: PetscLogEvent MAT_IncreaseOverlap, MAT_Partitioning, MAT_PartitioningND, MAT_Coarsen, MAT_ZeroEntries, MAT_Load, MAT_View, MAT_AXPY, MAT_FDColoringCreate;
 23: PetscLogEvent MAT_FDColoringSetUp, MAT_FDColoringApply,MAT_Transpose,MAT_FDColoringFunction, MAT_CreateSubMat;
 24: PetscLogEvent MAT_TransposeColoringCreate;
 25: PetscLogEvent MAT_MatMult, MAT_MatMultSymbolic, MAT_MatMultNumeric;
 26: PetscLogEvent MAT_PtAP, MAT_PtAPSymbolic, MAT_PtAPNumeric,MAT_RARt, MAT_RARtSymbolic, MAT_RARtNumeric;
 27: PetscLogEvent MAT_MatTransposeMult, MAT_MatTransposeMultSymbolic, MAT_MatTransposeMultNumeric;
 28: PetscLogEvent MAT_TransposeMatMult, MAT_TransposeMatMultSymbolic, MAT_TransposeMatMultNumeric;
 29: PetscLogEvent MAT_MatMatMult, MAT_MatMatMultSymbolic, MAT_MatMatMultNumeric;
 30: PetscLogEvent MAT_MultHermitianTranspose,MAT_MultHermitianTransposeAdd;
 31: PetscLogEvent MAT_Getsymtranspose, MAT_Getsymtransreduced, MAT_GetBrowsOfAcols;
 32: PetscLogEvent MAT_GetBrowsOfAocols, MAT_Getlocalmat, MAT_Getlocalmatcondensed, MAT_Seqstompi, MAT_Seqstompinum, MAT_Seqstompisym;
 33: PetscLogEvent MAT_Applypapt, MAT_Applypapt_numeric, MAT_Applypapt_symbolic, MAT_GetSequentialNonzeroStructure;
 34: PetscLogEvent MAT_GetMultiProcBlock;
 35: PetscLogEvent MAT_CUSPARSECopyToGPU, MAT_CUSPARSECopyFromGPU, MAT_CUSPARSEGenerateTranspose, MAT_CUSPARSESolveAnalysis;
 36: PetscLogEvent MAT_PreallCOO, MAT_SetVCOO;
 37: PetscLogEvent MAT_SetValuesBatch;
 38: PetscLogEvent MAT_ViennaCLCopyToGPU;
 39: PetscLogEvent MAT_DenseCopyToGPU, MAT_DenseCopyFromGPU;
 40: PetscLogEvent MAT_Merge,MAT_Residual,MAT_SetRandom;
 41: PetscLogEvent MAT_FactorFactS,MAT_FactorInvS;
 42: PetscLogEvent MATCOLORING_Apply,MATCOLORING_Comm,MATCOLORING_Local,MATCOLORING_ISCreate,MATCOLORING_SetUp,MATCOLORING_Weights;
 43: PetscLogEvent MAT_H2Opus_Build,MAT_H2Opus_Compress,MAT_H2Opus_Orthog;

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

 47: /*@
 48:    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,
 49:                   for sparse matrices that already have locations it fills the locations with random numbers

 51:    Logically Collective on Mat

 53:    Input Parameters:
 54: +  x  - the matrix
 55: -  rctx - the random number context, formed by PetscRandomCreate(), or NULL and
 56:           it will create one internally.

 58:    Output Parameter:
 59: .  x  - the matrix

 61:    Example of Usage:
 62: .vb
 63:      PetscRandomCreate(PETSC_COMM_WORLD,&rctx);
 64:      MatSetRandom(x,rctx);
 65:      PetscRandomDestroy(rctx);
 66: .ve

 68:    Level: intermediate

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


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

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

 92:   PetscLogEventBegin(MAT_SetRandom,x,rctx,0,0);
 93:   (*x->ops->setrandom)(x,rctx);
 94:   PetscLogEventEnd(MAT_SetRandom,x,rctx,0,0);

 96:   MatAssemblyBegin(x, MAT_FINAL_ASSEMBLY);
 97:   MatAssemblyEnd(x, MAT_FINAL_ASSEMBLY);
 98:   PetscRandomDestroy(&randObj);
 99:   return(0);
100: }

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

105:    Logically Collective on Mat

107:    Input Parameter:
108: .  mat - the factored matrix

110:    Output Parameters:
111: +  pivot - the pivot value computed
112: -  row - the row that the zero pivot occurred. Note that this row must be interpreted carefully due to row reorderings and which processes
113:          the share the matrix

115:    Level: advanced

117:    Notes:
118:     This routine does not work for factorizations done with external packages.

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

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

124: .seealso: MatZeroEntries(), MatFactor(), MatGetFactor(), MatLUFactorSymbolic(), MatCholeskyFactorSymbolic(), MatFactorClearError(), MatFactorGetErrorZeroPivot()
125: @*/
126: PetscErrorCode MatFactorGetErrorZeroPivot(Mat mat,PetscReal *pivot,PetscInt *row)
127: {
130:   *pivot = mat->factorerror_zeropivot_value;
131:   *row   = mat->factorerror_zeropivot_row;
132:   return(0);
133: }

135: /*@
136:    MatFactorGetError - gets the error code from a factorization

138:    Logically Collective on Mat

140:    Input Parameters:
141: .  mat - the factored matrix

143:    Output Parameter:
144: .  err  - the error code

146:    Level: advanced

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

151: .seealso: MatZeroEntries(), MatFactor(), MatGetFactor(), MatLUFactorSymbolic(), MatCholeskyFactorSymbolic(), MatFactorClearError(), MatFactorGetErrorZeroPivot()
152: @*/
153: PetscErrorCode MatFactorGetError(Mat mat,MatFactorError *err)
154: {
157:   *err = mat->factorerrortype;
158:   return(0);
159: }

161: /*@
162:    MatFactorClearError - clears the error code in a factorization

164:    Logically Collective on Mat

166:    Input Parameter:
167: .  mat - the factored matrix

169:    Level: developer

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

174: .seealso: MatZeroEntries(), MatFactor(), MatGetFactor(), MatLUFactorSymbolic(), MatCholeskyFactorSymbolic(), MatFactorGetError(), MatFactorGetErrorZeroPivot()
175: @*/
176: PetscErrorCode MatFactorClearError(Mat mat)
177: {
180:   mat->factorerrortype             = MAT_FACTOR_NOERROR;
181:   mat->factorerror_zeropivot_value = 0.0;
182:   mat->factorerror_zeropivot_row   = 0;
183:   return(0);
184: }

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

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

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

235:   Input Parameter:
236: .    A  - the matrix

238:   Output Parameter:
239: .    keptrows - the rows that are not completely zero

241:   Notes:
242:     keptrows is set to NULL if all rows are nonzero.

244:   Level: intermediate

246:  @*/
247: PetscErrorCode MatFindNonzeroRows(Mat mat,IS *keptrows)
248: {

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

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

268:   Input Parameter:
269: .    A  - the matrix

271:   Output Parameter:
272: .    zerorows - the rows that are completely zero

274:   Notes:
275:     zerorows is set to NULL if no rows are zero.

277:   Level: intermediate

279:  @*/
280: PetscErrorCode MatFindZeroRows(Mat mat,IS *zerorows)
281: {
283:   IS             keptrows;
284:   PetscInt       m, n;

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

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

307:    Not Collective

309:    Input Parameters:
310: .   A - the matrix

312:    Output Parameters:
313: .   a - the diagonal part (which is a SEQUENTIAL matrix)

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

320:    Level: advanced

322: @*/
323: PetscErrorCode MatGetDiagonalBlock(Mat A,Mat *a)
324: {

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

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

347:    Collective on Mat

349:    Input Parameters:
350: .  mat - the matrix

352:    Output Parameter:
353: .   trace - the sum of the diagonal entries

355:    Level: advanced

357: @*/
358: PetscErrorCode MatGetTrace(Mat mat,PetscScalar *trace)
359: {
361:   Vec            diag;

364:   MatCreateVecs(mat,&diag,NULL);
365:   MatGetDiagonal(mat,diag);
366:   VecSum(diag,trace);
367:   VecDestroy(&diag);
368:   return(0);
369: }

371: /*@
372:    MatRealPart - Zeros out the imaginary part of the matrix

374:    Logically Collective on Mat

376:    Input Parameters:
377: .  mat - the matrix

379:    Level: advanced

381: .seealso: MatImaginaryPart()
382: @*/
383: PetscErrorCode MatRealPart(Mat mat)
384: {

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

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

401:    Collective on Mat

403:    Input Parameter:
404: .  mat - the matrix

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

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

413:    Level: advanced

415: @*/
416: PetscErrorCode MatGetGhosts(Mat mat,PetscInt *nghosts,const PetscInt *ghosts[])
417: {

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

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

437:    Logically Collective on Mat

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

442:    Level: advanced

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

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

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

464:    Not Collective

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

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

473:    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.

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

546:    Level: advanced

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

558:   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
559:   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
560:   if (!mat->ops->getrow) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
561:   MatCheckPreallocated(mat,1);
562:   if (row < mat->rmap->rstart || row >= mat->rmap->rend) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Only for local rows, %D not in [%D,%D)",row,mat->rmap->rstart,mat->rmap->rend);
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().

693:    Level: advanced

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

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

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

716:    Logically Collective on Mat

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

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

726:    Level: advanced

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

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

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

744:    Logically Collective on Mat

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

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

754:    Level: advanced

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

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

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

772:    Not Collective

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

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

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

784:    Level: advanced

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

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

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

801:    Collective on Mat

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

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

810:    Level: beginner

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

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

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

828:    Collective on Mat

830:    Input Parameters:
831: .  A - the Mat context

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

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

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

840:    Level: beginner

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

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

869: #if defined(PETSC_HAVE_SAWS)
870: #include <petscviewersaws.h>
871: #endif

873: /*@C
874:    MatViewFromOptions - View from Options

876:    Collective on Mat

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

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

892:   PetscObjectViewFromOptions((PetscObject)A,obj,name);
893:   return(0);
894: }

896: /*@C
897:    MatView - Visualizes a matrix object.

899:    Collective on Mat

901:    Input Parameters:
902: +  mat - the matrix
903: -  viewer - visualization context

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1093:    Collective on PetscViewer

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

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

1105:    Level: beginner

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1190:    Corresponding MatView() is not yet implemented.

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

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

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

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


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

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

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

1236: static PetscErrorCode MatDestroy_Redundant(Mat_Redundant **redundant)
1237: {
1239:   Mat_Redundant  *redund = *redundant;
1240:   PetscInt       i;

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

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

1267: /*@C
1268:    MatDestroy - Frees space taken by a matrix.

1270:    Collective on Mat

1272:    Input Parameter:
1273: .  A - the matrix

1275:    Level: beginner

1277: @*/
1278: PetscErrorCode MatDestroy(Mat *A)
1279: {

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

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

1293:   PetscFree((*A)->defaultvectype);
1294:   PetscFree((*A)->bsizes);
1295:   PetscFree((*A)->solvertype);
1296:   for (PetscInt i=0; i<MAT_FACTOR_NUM_TYPES; i++) {
1297:     PetscFree((*A)->preferredordering[i]);
1298:   }
1299:   MatDestroy_Redundant(&(*A)->redundant);
1300:   MatProductClear(*A);
1301:   MatNullSpaceDestroy(&(*A)->nullsp);
1302:   MatNullSpaceDestroy(&(*A)->transnullsp);
1303:   MatNullSpaceDestroy(&(*A)->nearnullsp);
1304:   MatDestroy(&(*A)->schur);
1305:   PetscLayoutDestroy(&(*A)->rmap);
1306:   PetscLayoutDestroy(&(*A)->cmap);
1307:   PetscHeaderDestroy(A);
1308:   return(0);
1309: }

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

1316:    Not Collective

1318:    Input Parameters:
1319: +  mat - the matrix
1320: .  v - a logically two-dimensional array of values
1321: .  m, idxm - the number of rows and their global indices
1322: .  n, idxn - the number of columns and their global indices
1323: -  addv - either ADD_VALUES or INSERT_VALUES, where
1324:    ADD_VALUES adds values to any existing entries, and
1325:    INSERT_VALUES replaces existing entries with new values

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

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

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

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

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

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

1349:    Level: beginner

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

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

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

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

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

1379:     for (i=0; i<m; i++) {
1380:       for (j=0; j<n; j++) {
1381:         if (mat->erroriffailure && PetscIsInfOrNanScalar(v[i*n+j]))
1382: #if defined(PETSC_USE_COMPLEX)
1383:           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]);
1384: #else
1385:           SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_FP,"Inserting %g at matrix entry (%D,%D)",(double)v[i*n+j],idxm[i],idxn[j]);
1386: #endif
1387:       }
1388:     }
1389:     for (i=0; i<m; i++) if (idxm[i] >= mat->rmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Cannot insert in row %D, maximum is %D",idxm[i],mat->rmap->N-1);
1390:     for (i=0; i<n; i++) if (idxn[i] >= mat->cmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Cannot insert in column %D, maximum is %D",idxn[i],mat->cmap->N-1);
1391:   }

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

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

1407:    Not Collective

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

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

1417:    All the nonzeros in the row must be provided

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

1421:    The row must belong to this process

1423:    Level: intermediate

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

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

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

1446:    Not Collective

1448:    Input Parameters:
1449: +  mat - the matrix
1450: .  row - the (block) row to set
1451: -  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

1453:    Notes:
1454:    The values, v, are column-oriented for the block version.

1456:    All the nonzeros in the row must be provided

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

1460:    The row must belong to this process

1462:    Level: advanced

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

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

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

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

1495:    Not Collective

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

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

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

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

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

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

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

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

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

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

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

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

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

1555:    Level: beginner

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

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

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

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

1609:    Not Collective

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

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

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

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

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

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

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

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

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

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

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

1662:    Level: beginner

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

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

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

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

1718:    Not Collective

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

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

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

1733:    Level: beginner

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


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

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

1761:    Not Collective

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

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

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

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

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

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

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

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

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

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

1821:    Level: intermediate

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

1832:   if (!m || !n) return(0); /* no values to insert */
1836:   MatCheckPreallocated(mat,1);
1837:   if (mat->insertmode == NOT_SET_VALUES) {
1838:     mat->insertmode = addv;
1839:   } else if (PetscUnlikely(mat->insertmode != addv)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values");
1840:   if (PetscDefined(USE_DEBUG)) {
1841:     if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1842:     if (!mat->ops->setvaluesblocked && !mat->ops->setvalues) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
1843:   }
1844:   if (PetscDefined(USE_DEBUG)) {
1845:     PetscInt rbs,cbs,M,N,i;
1846:     MatGetBlockSizes(mat,&rbs,&cbs);
1847:     MatGetSize(mat,&M,&N);
1848:     for (i=0; i<m; i++) {
1849:       if (idxm[i]*rbs >= M) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row block index %D (index %D) greater than row length %D",i,idxm[i],M);
1850:     }
1851:     for (i=0; i<n; i++) {
1852:       if (idxn[i]*cbs >= N) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column block index %D (index %D) great than column length %D",i,idxn[i],N);
1853:     }
1854:   }
1855:   if (mat->assembled) {
1856:     mat->was_assembled = PETSC_TRUE;
1857:     mat->assembled     = PETSC_FALSE;
1858:   }
1859:   PetscLogEventBegin(MAT_SetValues,mat,0,0,0);
1860:   if (mat->ops->setvaluesblocked) {
1861:     (*mat->ops->setvaluesblocked)(mat,m,idxm,n,idxn,v,addv);
1862:   } else {
1863:     PetscInt buf[8192],*bufr=NULL,*bufc=NULL,*iidxm,*iidxn;
1864:     PetscInt i,j,bs,cbs;
1865:     MatGetBlockSizes(mat,&bs,&cbs);
1866:     if (m*bs+n*cbs <= (PetscInt)(sizeof(buf)/sizeof(PetscInt))) {
1867:       iidxm = buf; iidxn = buf + m*bs;
1868:     } else {
1869:       PetscMalloc2(m*bs,&bufr,n*cbs,&bufc);
1870:       iidxm = bufr; iidxn = bufc;
1871:     }
1872:     for (i=0; i<m; i++) {
1873:       for (j=0; j<bs; j++) {
1874:         iidxm[i*bs+j] = bs*idxm[i] + j;
1875:       }
1876:     }
1877:     for (i=0; i<n; i++) {
1878:       for (j=0; j<cbs; j++) {
1879:         iidxn[i*cbs+j] = cbs*idxn[i] + j;
1880:       }
1881:     }
1882:     MatSetValues(mat,m*bs,iidxm,n*cbs,iidxn,v,addv);
1883:     PetscFree2(bufr,bufc);
1884:   }
1885:   PetscLogEventEnd(MAT_SetValues,mat,0,0,0);
1886:   return(0);
1887: }

1889: /*@C
1890:    MatGetValues - Gets a block of values from a matrix.

1892:    Not Collective; can only return values that are owned by the give process

1894:    Input Parameters:
1895: +  mat - the matrix
1896: .  v - a logically two-dimensional array for storing the values
1897: .  m, idxm - the number of rows and their global indices
1898: -  n, idxn - the number of columns and their global indices

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

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

1908:      MatGetValues() requires that the matrix has been assembled
1909:      with MatAssemblyBegin()/MatAssemblyEnd().  Thus, calls to
1910:      MatSetValues() and MatGetValues() CANNOT be made in succession
1911:      without intermediate matrix assembly.

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

1916:      For the standard row-based matrix formats, idxm[] can only contain rows owned by the requesting MPI rank.
1917:      That is, rows with global index greater than or equal to rstart and less than rend where rstart and rend are obtainable
1918:      from MatGetOwnershipRange(mat,&rstart,&rend).

1920:    Level: advanced

1922: .seealso: MatGetRow(), MatCreateSubMatrices(), MatSetValues(), MatGetOwnershipRange(), MatGetValuesLocal(), MatGetValue()
1923: @*/
1924: PetscErrorCode MatGetValues(Mat mat,PetscInt m,const PetscInt idxm[],PetscInt n,const PetscInt idxn[],PetscScalar v[])
1925: {

1931:   if (!m || !n) return(0);
1935:   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
1936:   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1937:   if (!mat->ops->getvalues) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
1938:   MatCheckPreallocated(mat,1);

1940:   PetscLogEventBegin(MAT_GetValues,mat,0,0,0);
1941:   (*mat->ops->getvalues)(mat,m,idxm,n,idxn,v);
1942:   PetscLogEventEnd(MAT_GetValues,mat,0,0,0);
1943:   return(0);
1944: }

1946: /*@C
1947:    MatGetValuesLocal - retrieves values from certain locations in a matrix using the local numbering of the indices
1948:      defined previously by MatSetLocalToGlobalMapping()

1950:    Not Collective

1952:    Input Parameters:
1953: +  mat - the matrix
1954: .  nrow, irow - number of rows and their local indices
1955: -  ncol, icol - number of columns and their local indices

1957:    Output Parameter:
1958: .  y -  a logically two-dimensional array of values

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

1963:      This routine can only return values that are owned by the requesting MPI rank. That is, for standard matrix formats, rows that, in the global numbering,
1964:      are greater than or equal to rstart and less than rend where rstart and rend are obtainable from MatGetOwnershipRange(mat,&rstart,&rend). One can
1965:      determine if the resulting global row associated with the local row r is owned by the requesting MPI rank by applying the ISLocalToGlobalMapping set
1966:      with MatSetLocalToGlobalMapping().

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

1972:    Level: advanced

1974: .seealso:  MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetLocalToGlobalMapping(),
1975:            MatSetValuesLocal(), MatGetValues()
1976: @*/
1977: PetscErrorCode MatGetValuesLocal(Mat mat,PetscInt nrow,const PetscInt irow[],PetscInt ncol,const PetscInt icol[],PetscScalar y[])
1978: {

1984:   MatCheckPreallocated(mat,1);
1985:   if (!nrow || !ncol) return(0); /* no values to retrieve */
1988:   if (PetscDefined(USE_DEBUG)) {
1989:     if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1990:     if (!mat->ops->getvalueslocal && !mat->ops->getvalues) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
1991:   }
1992:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
1993:   PetscLogEventBegin(MAT_GetValues,mat,0,0,0);
1994:   if (mat->ops->getvalueslocal) {
1995:     (*mat->ops->getvalueslocal)(mat,nrow,irow,ncol,icol,y);
1996:   } else {
1997:     PetscInt buf[8192],*bufr=NULL,*bufc=NULL,*irowm,*icolm;
1998:     if ((nrow+ncol) <= (PetscInt)(sizeof(buf)/sizeof(PetscInt))) {
1999:       irowm = buf; icolm = buf+nrow;
2000:     } else {
2001:       PetscMalloc2(nrow,&bufr,ncol,&bufc);
2002:       irowm = bufr; icolm = bufc;
2003:     }
2004:     if (!mat->rmap->mapping) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"MatGetValuesLocal() cannot proceed without local-to-global row mapping (See MatSetLocalToGlobalMapping()).");
2005:     if (!mat->cmap->mapping) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"MatGetValuesLocal() cannot proceed without local-to-global column mapping (See MatSetLocalToGlobalMapping()).");
2006:     ISLocalToGlobalMappingApply(mat->rmap->mapping,nrow,irow,irowm);
2007:     ISLocalToGlobalMappingApply(mat->cmap->mapping,ncol,icol,icolm);
2008:     MatGetValues(mat,nrow,irowm,ncol,icolm,y);
2009:     PetscFree2(bufr,bufc);
2010:   }
2011:   PetscLogEventEnd(MAT_GetValues,mat,0,0,0);
2012:   return(0);
2013: }

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

2019:   Not Collective

2021:   Input Parameters:
2022: + mat - the matrix
2023: . nb - the number of blocks
2024: . bs - the number of rows (and columns) in each block
2025: . rows - a concatenation of the rows for each block
2026: - v - a concatenation of logically two-dimensional arrays of values

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

2031:   Level: advanced

2033: .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(),
2034:           InsertMode, INSERT_VALUES, ADD_VALUES, MatSetValues()
2035: @*/
2036: PetscErrorCode MatSetValuesBatch(Mat mat, PetscInt nb, PetscInt bs, PetscInt rows[], const PetscScalar v[])
2037: {

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

2047:   PetscLogEventBegin(MAT_SetValuesBatch,mat,0,0,0);
2048:   if (mat->ops->setvaluesbatch) {
2049:     (*mat->ops->setvaluesbatch)(mat,nb,bs,rows,v);
2050:   } else {
2051:     PetscInt b;
2052:     for (b = 0; b < nb; ++b) {
2053:       MatSetValues(mat, bs, &rows[b*bs], bs, &rows[b*bs], &v[b*bs*bs], ADD_VALUES);
2054:     }
2055:   }
2056:   PetscLogEventEnd(MAT_SetValuesBatch,mat,0,0,0);
2057:   return(0);
2058: }

2060: /*@
2061:    MatSetLocalToGlobalMapping - Sets a local-to-global numbering for use by
2062:    the routine MatSetValuesLocal() to allow users to insert matrix entries
2063:    using a local (per-processor) numbering.

2065:    Not Collective

2067:    Input Parameters:
2068: +  x - the matrix
2069: .  rmapping - row mapping created with ISLocalToGlobalMappingCreate()   or ISLocalToGlobalMappingCreateIS()
2070: - cmapping - column mapping

2072:    Level: intermediate

2074: .seealso:  MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetValuesLocal(), MatGetValuesLocal()
2075: @*/
2076: PetscErrorCode MatSetLocalToGlobalMapping(Mat x,ISLocalToGlobalMapping rmapping,ISLocalToGlobalMapping cmapping)
2077: {


2086:   if (x->ops->setlocaltoglobalmapping) {
2087:     (*x->ops->setlocaltoglobalmapping)(x,rmapping,cmapping);
2088:   } else {
2089:     PetscLayoutSetISLocalToGlobalMapping(x->rmap,rmapping);
2090:     PetscLayoutSetISLocalToGlobalMapping(x->cmap,cmapping);
2091:   }
2092:   return(0);
2093: }

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

2098:    Not Collective

2100:    Input Parameter:
2101: .  A - the matrix

2103:    Output Parameters:
2104: + rmapping - row mapping
2105: - cmapping - column mapping

2107:    Level: advanced

2109: .seealso:  MatSetValuesLocal()
2110: @*/
2111: PetscErrorCode MatGetLocalToGlobalMapping(Mat A,ISLocalToGlobalMapping *rmapping,ISLocalToGlobalMapping *cmapping)
2112: {
2118:   if (rmapping) *rmapping = A->rmap->mapping;
2119:   if (cmapping) *cmapping = A->cmap->mapping;
2120:   return(0);
2121: }

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

2126:    Logically Collective on A

2128:    Input Parameters:
2129: +  A - the matrix
2130: . rmap - row layout
2131: - cmap - column layout

2133:    Level: advanced

2135: .seealso:  MatCreateVecs(), MatGetLocalToGlobalMapping(), MatGetLayouts()
2136: @*/
2137: PetscErrorCode MatSetLayouts(Mat A,PetscLayout rmap,PetscLayout cmap)
2138: {


2144:   PetscLayoutReference(rmap,&A->rmap);
2145:   PetscLayoutReference(cmap,&A->cmap);
2146:   return(0);
2147: }

2149: /*@
2150:    MatGetLayouts - Gets the PetscLayout objects for rows and columns

2152:    Not Collective

2154:    Input Parameter:
2155: .  A - the matrix

2157:    Output Parameters:
2158: + rmap - row layout
2159: - cmap - column layout

2161:    Level: advanced

2163: .seealso:  MatCreateVecs(), MatGetLocalToGlobalMapping(), MatSetLayouts()
2164: @*/
2165: PetscErrorCode MatGetLayouts(Mat A,PetscLayout *rmap,PetscLayout *cmap)
2166: {
2172:   if (rmap) *rmap = A->rmap;
2173:   if (cmap) *cmap = A->cmap;
2174:   return(0);
2175: }

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

2181:    Not Collective

2183:    Input Parameters:
2184: +  mat - the matrix
2185: .  nrow, irow - number of rows and their local indices
2186: .  ncol, icol - number of columns and their local indices
2187: .  y -  a logically two-dimensional array of values
2188: -  addv - either INSERT_VALUES or ADD_VALUES, where
2189:    ADD_VALUES adds values to any existing entries, and
2190:    INSERT_VALUES replaces existing entries with new values

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

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

2198:    Calls to MatSetValuesLocal() with the INSERT_VALUES and ADD_VALUES
2199:    options cannot be mixed without intervening calls to the assembly
2200:    routines.

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

2205:    Level: intermediate

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

2211: .seealso:  MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetLocalToGlobalMapping(),
2212:            MatSetValueLocal(), MatGetValuesLocal()
2213: @*/
2214: PetscErrorCode MatSetValuesLocal(Mat mat,PetscInt nrow,const PetscInt irow[],PetscInt ncol,const PetscInt icol[],const PetscScalar y[],InsertMode addv)
2215: {

2221:   MatCheckPreallocated(mat,1);
2222:   if (!nrow || !ncol) return(0); /* no values to insert */
2225:   if (mat->insertmode == NOT_SET_VALUES) {
2226:     mat->insertmode = addv;
2227:   }
2228:   else if (PetscUnlikely(mat->insertmode != addv)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values");
2229:   if (PetscDefined(USE_DEBUG)) {
2230:     if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2231:     if (!mat->ops->setvalueslocal && !mat->ops->setvalues) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
2232:   }

2234:   if (mat->assembled) {
2235:     mat->was_assembled = PETSC_TRUE;
2236:     mat->assembled     = PETSC_FALSE;
2237:   }
2238:   PetscLogEventBegin(MAT_SetValues,mat,0,0,0);
2239:   if (mat->ops->setvalueslocal) {
2240:     (*mat->ops->setvalueslocal)(mat,nrow,irow,ncol,icol,y,addv);
2241:   } else {
2242:     PetscInt buf[8192],*bufr=NULL,*bufc=NULL,*irowm,*icolm;
2243:     if ((nrow+ncol) <= (PetscInt)(sizeof(buf)/sizeof(PetscInt))) {
2244:       irowm = buf; icolm = buf+nrow;
2245:     } else {
2246:       PetscMalloc2(nrow,&bufr,ncol,&bufc);
2247:       irowm = bufr; icolm = bufc;
2248:     }
2249:     if (!mat->rmap->mapping) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"MatSetValuesLocal() cannot proceed without local-to-global row mapping (See MatSetLocalToGlobalMapping()).");
2250:     if (!mat->cmap->mapping) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"MatSetValuesLocal() cannot proceed without local-to-global column mapping (See MatSetLocalToGlobalMapping()).");
2251:     ISLocalToGlobalMappingApply(mat->rmap->mapping,nrow,irow,irowm);
2252:     ISLocalToGlobalMappingApply(mat->cmap->mapping,ncol,icol,icolm);
2253:     MatSetValues(mat,nrow,irowm,ncol,icolm,y,addv);
2254:     PetscFree2(bufr,bufc);
2255:   }
2256:   PetscLogEventEnd(MAT_SetValues,mat,0,0,0);
2257:   return(0);
2258: }

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

2264:    Not Collective

2266:    Input Parameters:
2267: +  x - the matrix
2268: .  nrow, irow - number of rows and their local indices
2269: .  ncol, icol - number of columns and their local indices
2270: .  y -  a logically two-dimensional array of values
2271: -  addv - either INSERT_VALUES or ADD_VALUES, where
2272:    ADD_VALUES adds values to any existing entries, and
2273:    INSERT_VALUES replaces existing entries with new values

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

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

2282:    Calls to MatSetValuesBlockedLocal() with the INSERT_VALUES and ADD_VALUES
2283:    options cannot be mixed without intervening calls to the assembly
2284:    routines.

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

2289:    Level: intermediate

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

2295: .seealso:  MatSetBlockSize(), MatSetLocalToGlobalMapping(), MatAssemblyBegin(), MatAssemblyEnd(),
2296:            MatSetValuesLocal(),  MatSetValuesBlocked()
2297: @*/
2298: PetscErrorCode MatSetValuesBlockedLocal(Mat mat,PetscInt nrow,const PetscInt irow[],PetscInt ncol,const PetscInt icol[],const PetscScalar y[],InsertMode addv)
2299: {

2305:   MatCheckPreallocated(mat,1);
2306:   if (!nrow || !ncol) return(0); /* no values to insert */
2310:   if (mat->insertmode == NOT_SET_VALUES) {
2311:     mat->insertmode = addv;
2312:   } else if (PetscUnlikely(mat->insertmode != addv)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values");
2313:   if (PetscDefined(USE_DEBUG)) {
2314:     if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2315:     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);
2316:   }

2318:   if (mat->assembled) {
2319:     mat->was_assembled = PETSC_TRUE;
2320:     mat->assembled     = PETSC_FALSE;
2321:   }
2322:   if (PetscUnlikelyDebug(mat->rmap->mapping)) { /* Condition on the mapping existing, because MatSetValuesBlockedLocal_IS does not require it to be set. */
2323:     PetscInt irbs, rbs;
2324:     MatGetBlockSizes(mat, &rbs, NULL);
2325:     ISLocalToGlobalMappingGetBlockSize(mat->rmap->mapping,&irbs);
2326:     if (rbs != irbs) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Different row block sizes! mat %D, row l2g map %D",rbs,irbs);
2327:   }
2328:   if (PetscUnlikelyDebug(mat->cmap->mapping)) {
2329:     PetscInt icbs, cbs;
2330:     MatGetBlockSizes(mat,NULL,&cbs);
2331:     ISLocalToGlobalMappingGetBlockSize(mat->cmap->mapping,&icbs);
2332:     if (cbs != icbs) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Different col block sizes! mat %D, col l2g map %D",cbs,icbs);
2333:   }
2334:   PetscLogEventBegin(MAT_SetValues,mat,0,0,0);
2335:   if (mat->ops->setvaluesblockedlocal) {
2336:     (*mat->ops->setvaluesblockedlocal)(mat,nrow,irow,ncol,icol,y,addv);
2337:   } else {
2338:     PetscInt buf[8192],*bufr=NULL,*bufc=NULL,*irowm,*icolm;
2339:     if ((nrow+ncol) <= (PetscInt)(sizeof(buf)/sizeof(PetscInt))) {
2340:       irowm = buf; icolm = buf + nrow;
2341:     } else {
2342:       PetscMalloc2(nrow,&bufr,ncol,&bufc);
2343:       irowm = bufr; icolm = bufc;
2344:     }
2345:     ISLocalToGlobalMappingApplyBlock(mat->rmap->mapping,nrow,irow,irowm);
2346:     ISLocalToGlobalMappingApplyBlock(mat->cmap->mapping,ncol,icol,icolm);
2347:     MatSetValuesBlocked(mat,nrow,irowm,ncol,icolm,y,addv);
2348:     PetscFree2(bufr,bufc);
2349:   }
2350:   PetscLogEventEnd(MAT_SetValues,mat,0,0,0);
2351:   return(0);
2352: }

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

2357:    Collective on Mat

2359:    Input Parameters:
2360: +  mat - the matrix
2361: -  x   - the vector to be multiplied

2363:    Output Parameters:
2364: .  y - the result

2366:    Notes:
2367:    The vectors x and y cannot be the same.  I.e., one cannot
2368:    call MatMult(A,y,y).

2370:    Level: developer

2372: .seealso: MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd()
2373: @*/
2374: PetscErrorCode MatMultDiagonalBlock(Mat mat,Vec x,Vec y)
2375: {


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

2389:   if (!mat->ops->multdiagonalblock) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Matrix type %s does not have a multiply defined",((PetscObject)mat)->type_name);
2390:   (*mat->ops->multdiagonalblock)(mat,x,y);
2391:   PetscObjectStateIncrease((PetscObject)y);
2392:   return(0);
2393: }

2395: /* --------------------------------------------------------*/
2396: /*@
2397:    MatMult - Computes the matrix-vector product, y = Ax.

2399:    Neighbor-wise Collective on Mat

2401:    Input Parameters:
2402: +  mat - the matrix
2403: -  x   - the vector to be multiplied

2405:    Output Parameters:
2406: .  y - the result

2408:    Notes:
2409:    The vectors x and y cannot be the same.  I.e., one cannot
2410:    call MatMult(A,y,y).

2412:    Level: beginner

2414: .seealso: MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd()
2415: @*/
2416: PetscErrorCode MatMult(Mat mat,Vec x,Vec y)
2417: {

2425:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2426:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2427:   if (x == y) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors");
2428:   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);
2429:   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);
2430:   if (mat->cmap->n != x->map->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: local dim %D %D",mat->cmap->n,x->map->n);
2431:   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);
2432:   VecSetErrorIfLocked(y,3);
2433:   if (mat->erroriffailure) {VecValidValues(x,2,PETSC_TRUE);}
2434:   MatCheckPreallocated(mat,1);

2436:   VecLockReadPush(x);
2437:   if (!mat->ops->mult) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Matrix type %s does not have a multiply defined",((PetscObject)mat)->type_name);
2438:   PetscLogEventBegin(MAT_Mult,mat,x,y,0);
2439:   (*mat->ops->mult)(mat,x,y);
2440:   PetscLogEventEnd(MAT_Mult,mat,x,y,0);
2441:   if (mat->erroriffailure) {VecValidValues(y,3,PETSC_FALSE);}
2442:   VecLockReadPop(x);
2443:   return(0);
2444: }

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

2449:    Neighbor-wise Collective on Mat

2451:    Input Parameters:
2452: +  mat - the matrix
2453: -  x   - the vector to be multiplied

2455:    Output Parameters:
2456: .  y - the result

2458:    Notes:
2459:    The vectors x and y cannot be the same.  I.e., one cannot
2460:    call MatMultTranspose(A,y,y).

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

2465:    Level: beginner

2467: .seealso: MatMult(), MatMultAdd(), MatMultTransposeAdd(), MatMultHermitianTranspose(), MatTranspose()
2468: @*/
2469: PetscErrorCode MatMultTranspose(Mat mat,Vec x,Vec y)
2470: {
2471:   PetscErrorCode (*op)(Mat,Vec,Vec)=NULL,ierr;


2479:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2480:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2481:   if (x == y) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors");
2482:   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);
2483:   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);
2484:   if (mat->cmap->n != y->map->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: local dim %D %D",mat->cmap->n,y->map->n);
2485:   if (mat->rmap->n != x->map->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: local dim %D %D",mat->rmap->n,x->map->n);
2486:   if (mat->erroriffailure) {VecValidValues(x,2,PETSC_TRUE);}
2487:   MatCheckPreallocated(mat,1);

2489:   if (!mat->ops->multtranspose) {
2490:     if (mat->symmetric && mat->ops->mult) op = mat->ops->mult;
2491:     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);
2492:   } else op = mat->ops->multtranspose;
2493:   PetscLogEventBegin(MAT_MultTranspose,mat,x,y,0);
2494:   VecLockReadPush(x);
2495:   (*op)(mat,x,y);
2496:   VecLockReadPop(x);
2497:   PetscLogEventEnd(MAT_MultTranspose,mat,x,y,0);
2498:   PetscObjectStateIncrease((PetscObject)y);
2499:   if (mat->erroriffailure) {VecValidValues(y,3,PETSC_FALSE);}
2500:   return(0);
2501: }

2503: /*@
2504:    MatMultHermitianTranspose - Computes matrix Hermitian transpose times a vector.

2506:    Neighbor-wise Collective on Mat

2508:    Input Parameters:
2509: +  mat - the matrix
2510: -  x   - the vector to be multilplied

2512:    Output Parameters:
2513: .  y - the result

2515:    Notes:
2516:    The vectors x and y cannot be the same.  I.e., one cannot
2517:    call MatMultHermitianTranspose(A,y,y).

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

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

2523:    Level: beginner

2525: .seealso: MatMult(), MatMultAdd(), MatMultHermitianTransposeAdd(), MatMultTranspose()
2526: @*/
2527: PetscErrorCode MatMultHermitianTranspose(Mat mat,Vec x,Vec y)
2528: {


2537:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2538:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2539:   if (x == y) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors");
2540:   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);
2541:   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);
2542:   if (mat->cmap->n != y->map->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: local dim %D %D",mat->cmap->n,y->map->n);
2543:   if (mat->rmap->n != x->map->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: local dim %D %D",mat->rmap->n,x->map->n);
2544:   MatCheckPreallocated(mat,1);

2546:   PetscLogEventBegin(MAT_MultHermitianTranspose,mat,x,y,0);
2547: #if defined(PETSC_USE_COMPLEX)
2548:   if (mat->ops->multhermitiantranspose || (mat->hermitian && mat->ops->mult)) {
2549:     VecLockReadPush(x);
2550:     if (mat->ops->multhermitiantranspose) {
2551:       (*mat->ops->multhermitiantranspose)(mat,x,y);
2552:     } else {
2553:       (*mat->ops->mult)(mat,x,y);
2554:     }
2555:     VecLockReadPop(x);
2556:   } else {
2557:     Vec w;
2558:     VecDuplicate(x,&w);
2559:     VecCopy(x,w);
2560:     VecConjugate(w);
2561:     MatMultTranspose(mat,w,y);
2562:     VecDestroy(&w);
2563:     VecConjugate(y);
2564:   }
2565:   PetscObjectStateIncrease((PetscObject)y);
2566: #else
2567:   MatMultTranspose(mat,x,y);
2568: #endif
2569:   PetscLogEventEnd(MAT_MultHermitianTranspose,mat,x,y,0);
2570:   return(0);
2571: }

2573: /*@
2574:     MatMultAdd -  Computes v3 = v2 + A * v1.

2576:     Neighbor-wise Collective on Mat

2578:     Input Parameters:
2579: +   mat - the matrix
2580: -   v1, v2 - the vectors

2582:     Output Parameters:
2583: .   v3 - the result

2585:     Notes:
2586:     The vectors v1 and v3 cannot be the same.  I.e., one cannot
2587:     call MatMultAdd(A,v1,v2,v1).

2589:     Level: beginner

2591: .seealso: MatMultTranspose(), MatMult(), MatMultTransposeAdd()
2592: @*/
2593: PetscErrorCode MatMultAdd(Mat mat,Vec v1,Vec v2,Vec v3)
2594: {


2604:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2605:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2606:   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);
2607:   /* 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);
2608:      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); */
2609:   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);
2610:   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);
2611:   if (v1 == v3) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"v1 and v3 must be different vectors");
2612:   MatCheckPreallocated(mat,1);

2614:   if (!mat->ops->multadd) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"No MatMultAdd() for matrix type %s",((PetscObject)mat)->type_name);
2615:   PetscLogEventBegin(MAT_MultAdd,mat,v1,v2,v3);
2616:   VecLockReadPush(v1);
2617:   (*mat->ops->multadd)(mat,v1,v2,v3);
2618:   VecLockReadPop(v1);
2619:   PetscLogEventEnd(MAT_MultAdd,mat,v1,v2,v3);
2620:   PetscObjectStateIncrease((PetscObject)v3);
2621:   return(0);
2622: }

2624: /*@
2625:    MatMultTransposeAdd - Computes v3 = v2 + A' * v1.

2627:    Neighbor-wise Collective on Mat

2629:    Input Parameters:
2630: +  mat - the matrix
2631: -  v1, v2 - the vectors

2633:    Output Parameters:
2634: .  v3 - the result

2636:    Notes:
2637:    The vectors v1 and v3 cannot be the same.  I.e., one cannot
2638:    call MatMultTransposeAdd(A,v1,v2,v1).

2640:    Level: beginner

2642: .seealso: MatMultTranspose(), MatMultAdd(), MatMult()
2643: @*/
2644: PetscErrorCode MatMultTransposeAdd(Mat mat,Vec v1,Vec v2,Vec v3)
2645: {


2655:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2656:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2657:   if (!mat->ops->multtransposeadd) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
2658:   if (v1 == v3) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"v1 and v3 must be different vectors");
2659:   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);
2660:   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);
2661:   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);
2662:   MatCheckPreallocated(mat,1);

2664:   PetscLogEventBegin(MAT_MultTransposeAdd,mat,v1,v2,v3);
2665:   VecLockReadPush(v1);
2666:   (*mat->ops->multtransposeadd)(mat,v1,v2,v3);
2667:   VecLockReadPop(v1);
2668:   PetscLogEventEnd(MAT_MultTransposeAdd,mat,v1,v2,v3);
2669:   PetscObjectStateIncrease((PetscObject)v3);
2670:   return(0);
2671: }

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

2676:    Neighbor-wise Collective on Mat

2678:    Input Parameters:
2679: +  mat - the matrix
2680: -  v1, v2 - the vectors

2682:    Output Parameters:
2683: .  v3 - the result

2685:    Notes:
2686:    The vectors v1 and v3 cannot be the same.  I.e., one cannot
2687:    call MatMultHermitianTransposeAdd(A,v1,v2,v1).

2689:    Level: beginner

2691: .seealso: MatMultHermitianTranspose(), MatMultTranspose(), MatMultAdd(), MatMult()
2692: @*/
2693: PetscErrorCode MatMultHermitianTransposeAdd(Mat mat,Vec v1,Vec v2,Vec v3)
2694: {


2704:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2705:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2706:   if (v1 == v3) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"v1 and v3 must be different vectors");
2707:   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);
2708:   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);
2709:   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);
2710:   MatCheckPreallocated(mat,1);

2712:   PetscLogEventBegin(MAT_MultHermitianTransposeAdd,mat,v1,v2,v3);
2713:   VecLockReadPush(v1);
2714:   if (mat->ops->multhermitiantransposeadd) {
2715:     (*mat->ops->multhermitiantransposeadd)(mat,v1,v2,v3);
2716:   } else {
2717:     Vec w,z;
2718:     VecDuplicate(v1,&w);
2719:     VecCopy(v1,w);
2720:     VecConjugate(w);
2721:     VecDuplicate(v3,&z);
2722:     MatMultTranspose(mat,w,z);
2723:     VecDestroy(&w);
2724:     VecConjugate(z);
2725:     if (v2 != v3) {
2726:       VecWAXPY(v3,1.0,v2,z);
2727:     } else {
2728:       VecAXPY(v3,1.0,z);
2729:     }
2730:     VecDestroy(&z);
2731:   }
2732:   VecLockReadPop(v1);
2733:   PetscLogEventEnd(MAT_MultHermitianTransposeAdd,mat,v1,v2,v3);
2734:   PetscObjectStateIncrease((PetscObject)v3);
2735:   return(0);
2736: }

2738: /*@
2739:    MatMultConstrained - The inner multiplication routine for a
2740:    constrained matrix P^T A P.

2742:    Neighbor-wise Collective on Mat

2744:    Input Parameters:
2745: +  mat - the matrix
2746: -  x   - the vector to be multilplied

2748:    Output Parameters:
2749: .  y - the result

2751:    Notes:
2752:    The vectors x and y cannot be the same.  I.e., one cannot
2753:    call MatMult(A,y,y).

2755:    Level: beginner

2757: .seealso: MatMult(), MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd()
2758: @*/
2759: PetscErrorCode MatMultConstrained(Mat mat,Vec x,Vec y)
2760: {

2767:   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2768:   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2769:   if (x == y) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors");
2770:   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);
2771:   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);
2772:   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);

2774:   PetscLogEventBegin(MAT_MultConstrained,mat,x,y,0);
2775:   VecLockReadPush(x);
2776:   (*mat->ops->multconstrained)(mat,x,y);
2777:   VecLockReadPop(x);
2778:   PetscLogEventEnd(MAT_MultConstrained,mat,x,y,0);
2779:   PetscObjectStateIncrease((PetscObject)y);
2780:   return(0);
2781: }

2783: /*@
2784:    MatMultTransposeConstrained - The inner multiplication routine for a
2785:    constrained matrix P^T A^T P.

2787:    Neighbor-wise Collective on Mat

2789:    Input Parameters:
2790: +  mat - the matrix
2791: -  x   - the vector to be multilplied

2793:    Output Parameters:
2794: .  y - the result

2796:    Notes:
2797:    The vectors x and y cannot be the same.  I.e., one cannot
2798:    call MatMult(A,y,y).

2800:    Level: beginner

2802: .seealso: MatMult(), MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd()
2803: @*/
2804: PetscErrorCode MatMultTransposeConstrained(Mat mat,Vec x,Vec y)
2805: {

2812:   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2813:   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2814:   if (x == y) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors");
2815:   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);
2816:   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);

2818:   PetscLogEventBegin(MAT_MultConstrained,mat,x,y,0);
2819:   (*mat->ops->multtransposeconstrained)(mat,x,y);
2820:   PetscLogEventEnd(MAT_MultConstrained,mat,x,y,0);
2821:   PetscObjectStateIncrease((PetscObject)y);
2822:   return(0);
2823: }

2825: /*@C
2826:    MatGetFactorType - gets the type of factorization it is

2828:    Not Collective

2830:    Input Parameters:
2831: .  mat - the matrix

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

2836:    Level: intermediate

2838: .seealso: MatFactorType, MatGetFactor(), MatSetFactorType()
2839: @*/
2840: PetscErrorCode MatGetFactorType(Mat mat,MatFactorType *t)
2841: {
2846:   *t = mat->factortype;
2847:   return(0);
2848: }

2850: /*@C
2851:    MatSetFactorType - sets the type of factorization it is

2853:    Logically Collective on Mat

2855:    Input Parameters:
2856: +  mat - the matrix
2857: -  t - the type, one of MAT_FACTOR_NONE, MAT_FACTOR_LU, MAT_FACTOR_CHOLESKY, MAT_FACTOR_ILU, MAT_FACTOR_ICC,MAT_FACTOR_ILUDT

2859:    Level: intermediate

2861: .seealso: MatFactorType, MatGetFactor(), MatGetFactorType()
2862: @*/
2863: PetscErrorCode MatSetFactorType(Mat mat, MatFactorType t)
2864: {
2868:   mat->factortype = t;
2869:   return(0);
2870: }

2872: /* ------------------------------------------------------------*/
2873: /*@C
2874:    MatGetInfo - Returns information about matrix storage (number of
2875:    nonzeros, memory, etc.).

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

2879:    Input Parameter:
2880: .  mat - the matrix

2882:    Output Parameters:
2883: +  flag - flag indicating the type of parameters to be returned
2884:    (MAT_LOCAL - local matrix, MAT_GLOBAL_MAX - maximum over all processors,
2885:    MAT_GLOBAL_SUM - sum over all processors)
2886: -  info - matrix information context

2888:    Notes:
2889:    The MatInfo context contains a variety of matrix data, including
2890:    number of nonzeros allocated and used, number of mallocs during
2891:    matrix assembly, etc.  Additional information for factored matrices
2892:    is provided (such as the fill ratio, number of mallocs during
2893:    factorization, etc.).  Much of this info is printed to PETSC_STDOUT
2894:    when using the runtime options
2895: $       -info -mat_view ::ascii_info

2897:    Example for C/C++ Users:
2898:    See the file ${PETSC_DIR}/include/petscmat.h for a complete list of
2899:    data within the MatInfo context.  For example,
2900: .vb
2901:       MatInfo info;
2902:       Mat     A;
2903:       double  mal, nz_a, nz_u;

2905:       MatGetInfo(A,MAT_LOCAL,&info);
2906:       mal  = info.mallocs;
2907:       nz_a = info.nz_allocated;
2908: .ve

2910:    Example for Fortran Users:
2911:    Fortran users should declare info as a double precision
2912:    array of dimension MAT_INFO_SIZE, and then extract the parameters
2913:    of interest.  See the file ${PETSC_DIR}/include/petsc/finclude/petscmat.h
2914:    a complete list of parameter names.
2915: .vb
2916:       double  precision info(MAT_INFO_SIZE)
2917:       double  precision mal, nz_a
2918:       Mat     A
2919:       integer ierr

2921:       call MatGetInfo(A,MAT_LOCAL,info,ierr)
2922:       mal = info(MAT_INFO_MALLOCS)
2923:       nz_a = info(MAT_INFO_NZ_ALLOCATED)
2924: .ve

2926:     Level: intermediate

2928:     Developer Note: fortran interface is not autogenerated as the f90
2929:     interface definition cannot be generated correctly [due to MatInfo]

2931: .seealso: MatStashGetInfo()

2933: @*/
2934: PetscErrorCode MatGetInfo(Mat mat,MatInfoType flag,MatInfo *info)
2935: {

2942:   if (!mat->ops->getinfo) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
2943:   MatCheckPreallocated(mat,1);
2944:   (*mat->ops->getinfo)(mat,flag,info);
2945:   return(0);
2946: }

2948: /*
2949:    This is used by external packages where it is not easy to get the info from the actual
2950:    matrix factorization.
2951: */
2952: PetscErrorCode MatGetInfo_External(Mat A,MatInfoType flag,MatInfo *info)
2953: {

2957:   PetscMemzero(info,sizeof(MatInfo));
2958:   return(0);
2959: }

2961: /* ----------------------------------------------------------*/

2963: /*@C
2964:    MatLUFactor - Performs in-place LU factorization of matrix.

2966:    Collective on Mat

2968:    Input Parameters:
2969: +  mat - the matrix
2970: .  row - row permutation
2971: .  col - column permutation
2972: -  info - options for factorization, includes
2973: $          fill - expected fill as ratio of original fill.
2974: $          dtcol - pivot tolerance (0 no pivot, 1 full column pivoting)
2975: $                   Run with the option -info to determine an optimal value to use

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

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

2985:    Level: developer

2987: .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor(),
2988:           MatGetOrdering(), MatSetUnfactored(), MatFactorInfo, MatGetFactor()

2990:     Developer Note: fortran interface is not autogenerated as the f90
2991:     interface definition cannot be generated correctly [due to MatFactorInfo]

2993: @*/
2994: PetscErrorCode MatLUFactor(Mat mat,IS row,IS col,const MatFactorInfo *info)
2995: {
2997:   MatFactorInfo  tinfo;

3005:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3006:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3007:   if (!mat->ops->lufactor) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
3008:   MatCheckPreallocated(mat,1);
3009:   if (!info) {
3010:     MatFactorInfoInitialize(&tinfo);
3011:     info = &tinfo;
3012:   }

3014:   PetscLogEventBegin(MAT_LUFactor,mat,row,col,0);
3015:   (*mat->ops->lufactor)(mat,row,col,info);
3016:   PetscLogEventEnd(MAT_LUFactor,mat,row,col,0);
3017:   PetscObjectStateIncrease((PetscObject)mat);
3018:   return(0);
3019: }

3021: /*@C
3022:    MatILUFactor - Performs in-place ILU factorization of matrix.

3024:    Collective on Mat

3026:    Input Parameters:
3027: +  mat - the matrix
3028: .  row - row permutation
3029: .  col - column permutation
3030: -  info - structure containing
3031: $      levels - number of levels of fill.
3032: $      expected fill - as ratio of original fill.
3033: $      1 or 0 - indicating force fill on diagonal (improves robustness for matrices
3034:                 missing diagonal entries)

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

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

3044:    Level: developer

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

3048:     Developer Note: fortran interface is not autogenerated as the f90
3049:     interface definition cannot be generated correctly [due to MatFactorInfo]

3051: @*/
3052: PetscErrorCode MatILUFactor(Mat mat,IS row,IS col,const MatFactorInfo *info)
3053: {

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

3068:   PetscLogEventBegin(MAT_ILUFactor,mat,row,col,0);
3069:   (*mat->ops->ilufactor)(mat,row,col,info);
3070:   PetscLogEventEnd(MAT_ILUFactor,mat,row,col,0);
3071:   PetscObjectStateIncrease((PetscObject)mat);
3072:   return(0);
3073: }

3075: /*@C
3076:    MatLUFactorSymbolic - Performs symbolic LU factorization of matrix.
3077:    Call this routine before calling MatLUFactorNumeric().

3079:    Collective on Mat

3081:    Input Parameters:
3082: +  fact - the factor matrix obtained with MatGetFactor()
3083: .  mat - the matrix
3084: .  row, col - row and column permutations
3085: -  info - options for factorization, includes
3086: $          fill - expected fill as ratio of original fill.
3087: $          dtcol - pivot tolerance (0 no pivot, 1 full column pivoting)
3088: $                   Run with the option -info to determine an optimal value to use

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

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

3097:    Level: developer

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

3101:     Developer Note: fortran interface is not autogenerated as the f90
3102:     interface definition cannot be generated correctly [due to MatFactorInfo]

3104: @*/
3105: PetscErrorCode MatLUFactorSymbolic(Mat fact,Mat mat,IS row,IS col,const MatFactorInfo *info)
3106: {
3108:   MatFactorInfo  tinfo;

3117:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3118:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3119:   if (!(fact)->ops->lufactorsymbolic) {
3120:     MatSolverType stype;
3121:     MatFactorGetSolverType(fact,&stype);
3122:     SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Matrix type %s symbolic LU using solver package %s",((PetscObject)mat)->type_name,stype);
3123:   }
3124:   MatCheckPreallocated(mat,2);
3125:   if (!info) {
3126:     MatFactorInfoInitialize(&tinfo);
3127:     info = &tinfo;
3128:   }

3130:   if (!fact->trivialsymbolic) {PetscLogEventBegin(MAT_LUFactorSymbolic,mat,row,col,0);}
3131:   (fact->ops->lufactorsymbolic)(fact,mat,row,col,info);
3132:   if (!fact->trivialsymbolic) {PetscLogEventEnd(MAT_LUFactorSymbolic,mat,row,col,0);}
3133:   PetscObjectStateIncrease((PetscObject)fact);
3134:   return(0);
3135: }

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

3141:    Collective on Mat

3143:    Input Parameters:
3144: +  fact - the factor matrix obtained with MatGetFactor()
3145: .  mat - the matrix
3146: -  info - options for factorization

3148:    Notes:
3149:    See MatLUFactor() for in-place factorization.  See
3150:    MatCholeskyFactorNumeric() for the symmetric, positive definite case.

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

3156:    Level: developer

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

3160:     Developer Note: fortran interface is not autogenerated as the f90
3161:     interface definition cannot be generated correctly [due to MatFactorInfo]

3163: @*/
3164: PetscErrorCode MatLUFactorNumeric(Mat fact,Mat mat,const MatFactorInfo *info)
3165: {
3166:   MatFactorInfo  tinfo;

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

3177:   if (!(fact)->ops->lufactornumeric) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s numeric LU",((PetscObject)mat)->type_name);
3178:   MatCheckPreallocated(mat,2);
3179:   if (!info) {
3180:     MatFactorInfoInitialize(&tinfo);
3181:     info = &tinfo;
3182:   }

3184:   if (!fact->trivialsymbolic) {PetscLogEventBegin(MAT_LUFactorNumeric,mat,fact,0,0);}
3185:   else {PetscLogEventBegin(MAT_LUFactor,mat,fact,0,0);}
3186:   (fact->ops->lufactornumeric)(fact,mat,info);
3187:   if (!fact->trivialsymbolic) {PetscLogEventEnd(MAT_LUFactorNumeric,mat,fact,0,0);}
3188:   else {PetscLogEventEnd(MAT_LUFactor,mat,fact,0,0);}
3189:   MatViewFromOptions(fact,NULL,"-mat_factor_view");
3190:   PetscObjectStateIncrease((PetscObject)fact);
3191:   return(0);
3192: }

3194: /*@C
3195:    MatCholeskyFactor - Performs in-place Cholesky factorization of a
3196:    symmetric matrix.

3198:    Collective on Mat

3200:    Input Parameters:
3201: +  mat - the matrix
3202: .  perm - row and column permutations
3203: -  f - expected fill as ratio of original fill

3205:    Notes:
3206:    See MatLUFactor() for the nonsymmetric case.  See also
3207:    MatCholeskyFactorSymbolic(), and MatCholeskyFactorNumeric().

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

3213:    Level: developer

3215: .seealso: MatLUFactor(), MatCholeskyFactorSymbolic(), MatCholeskyFactorNumeric()
3216:           MatGetOrdering()

3218:     Developer Note: fortran interface is not autogenerated as the f90
3219:     interface definition cannot be generated correctly [due to MatFactorInfo]

3221: @*/
3222: PetscErrorCode MatCholeskyFactor(Mat mat,IS perm,const MatFactorInfo *info)
3223: {
3225:   MatFactorInfo  tinfo;

3232:   if (mat->rmap->N != mat->cmap->N) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONG,"Matrix must be square");
3233:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3234:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3235:   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);
3236:   MatCheckPreallocated(mat,1);
3237:   if (!info) {
3238:     MatFactorInfoInitialize(&tinfo);
3239:     info = &tinfo;
3240:   }

3242:   PetscLogEventBegin(MAT_CholeskyFactor,mat,perm,0,0);
3243:   (*mat->ops->choleskyfactor)(mat,perm,info);
3244:   PetscLogEventEnd(MAT_CholeskyFactor,mat,perm,0,0);
3245:   PetscObjectStateIncrease((PetscObject)mat);
3246:   return(0);
3247: }

3249: /*@C
3250:    MatCholeskyFactorSymbolic - Performs symbolic Cholesky factorization
3251:    of a symmetric matrix.

3253:    Collective on Mat

3255:    Input Parameters:
3256: +  fact - the factor matrix obtained with MatGetFactor()
3257: .  mat - the matrix
3258: .  perm - row and column permutations
3259: -  info - options for factorization, includes
3260: $          fill - expected fill as ratio of original fill.
3261: $          dtcol - pivot tolerance (0 no pivot, 1 full column pivoting)
3262: $                   Run with the option -info to determine an optimal value to use

3264:    Notes:
3265:    See MatLUFactorSymbolic() for the nonsymmetric case.  See also
3266:    MatCholeskyFactor() and MatCholeskyFactorNumeric().

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

3272:    Level: developer

3274: .seealso: MatLUFactorSymbolic(), MatCholeskyFactor(), MatCholeskyFactorNumeric()
3275:           MatGetOrdering()

3277:     Developer Note: fortran interface is not autogenerated as the f90
3278:     interface definition cannot be generated correctly [due to MatFactorInfo]

3280: @*/
3281: PetscErrorCode MatCholeskyFactorSymbolic(Mat fact,Mat mat,IS perm,const MatFactorInfo *info)
3282: {
3284:   MatFactorInfo  tinfo;

3292:   if (mat->rmap->N != mat->cmap->N) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONG,"Matrix must be square");
3293:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3294:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3295:   if (!(fact)->ops->choleskyfactorsymbolic) {
3296:     MatSolverType stype;
3297:     MatFactorGetSolverType(fact,&stype);
3298:     SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s symbolic factor Cholesky using solver package %s",((PetscObject)mat)->type_name,stype);
3299:   }
3300:   MatCheckPreallocated(mat,2);
3301:   if (!info) {
3302:     MatFactorInfoInitialize(&tinfo);
3303:     info = &tinfo;
3304:   }

3306:   if (!fact->trivialsymbolic) {PetscLogEventBegin(MAT_CholeskyFactorSymbolic,mat,perm,0,0);}
3307:   (fact->ops->choleskyfactorsymbolic)(fact,mat,perm,info);
3308:   if (!fact->trivialsymbolic) {PetscLogEventEnd(MAT_CholeskyFactorSymbolic,mat,perm,0,0);}
3309:   PetscObjectStateIncrease((PetscObject)fact);
3310:   return(0);
3311: }

3313: /*@C
3314:    MatCholeskyFactorNumeric - Performs numeric Cholesky factorization
3315:    of a symmetric matrix. Call this routine after first calling
3316:    MatCholeskyFactorSymbolic().

3318:    Collective on Mat

3320:    Input Parameters:
3321: +  fact - the factor matrix obtained with MatGetFactor()
3322: .  mat - the initial matrix
3323: .  info - options for factorization
3324: -  fact - the symbolic factor of mat

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

3331:    Level: developer

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

3335:     Developer Note: fortran interface is not autogenerated as the f90
3336:     interface definition cannot be generated correctly [due to MatFactorInfo]

3338: @*/
3339: PetscErrorCode MatCholeskyFactorNumeric(Mat fact,Mat mat,const MatFactorInfo *info)
3340: {
3341:   MatFactorInfo  tinfo;

3349:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3350:   if (!(fact)->ops->choleskyfactornumeric) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s numeric factor Cholesky",((PetscObject)mat)->type_name);
3351:   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);
3352:   MatCheckPreallocated(mat,2);
3353:   if (!info) {
3354:     MatFactorInfoInitialize(&tinfo);
3355:     info = &tinfo;
3356:   }

3358:   if (!fact->trivialsymbolic) {PetscLogEventBegin(MAT_CholeskyFactorNumeric,mat,fact,0,0);}
3359:   else {PetscLogEventBegin(MAT_CholeskyFactor,mat,fact,0,0);}
3360:   (fact->ops->choleskyfactornumeric)(fact,mat,info);
3361:   if (!fact->trivialsymbolic) {PetscLogEventEnd(MAT_CholeskyFactorNumeric,mat,fact,0,0);}
3362:   else {PetscLogEventEnd(MAT_CholeskyFactor,mat,fact,0,0);}
3363:   MatViewFromOptions(fact,NULL,"-mat_factor_view");
3364:   PetscObjectStateIncrease((PetscObject)fact);
3365:   return(0);
3366: }

3368: /*@
3369:    MatQRFactor - Performs in-place QR factorization of matrix.

3371:    Collective on Mat

3373:    Input Parameters:
3374: +  mat - the matrix
3375: .  col - column permutation
3376: -  info - options for factorization, includes
3377: $          fill - expected fill as ratio of original fill.
3378: $          dtcol - pivot tolerance (0 no pivot, 1 full column pivoting)
3379: $                   Run with the option -info to determine an optimal value to use

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

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

3389:    Level: developer

3391: .seealso: MatQRFactorSymbolic(), MatQRFactorNumeric(), MatLUFactor(),
3392:           MatSetUnfactored(), MatFactorInfo, MatGetFactor()

3394:     Developer Note: fortran interface is not autogenerated as the f90
3395:     interface definition cannot be generated correctly [due to MatFactorInfo]

3397: @*/
3398: PetscErrorCode MatQRFactor(Mat mat, IS col, const MatFactorInfo *info)
3399: {

3407:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3408:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3409:   MatCheckPreallocated(mat,1);
3410:   PetscLogEventBegin(MAT_QRFactor,mat,col,0,0);
3411:   PetscUseMethod(mat,"MatQRFactor_C", (Mat,IS,const MatFactorInfo*), (mat, col, info));
3412:   PetscLogEventEnd(MAT_QRFactor,mat,col,0,0);
3413:   PetscObjectStateIncrease((PetscObject)mat);
3414:   return(0);
3415: }

3417: /*@
3418:    MatQRFactorSymbolic - Performs symbolic QR factorization of matrix.
3419:    Call this routine before calling MatQRFactorNumeric().

3421:    Collective on Mat

3423:    Input Parameters:
3424: +  fact - the factor matrix obtained with MatGetFactor()
3425: .  mat - the matrix
3426: .  col - column permutation
3427: -  info - options for factorization, includes
3428: $          fill - expected fill as ratio of original fill.
3429: $          dtcol - pivot tolerance (0 no pivot, 1 full column pivoting)
3430: $                   Run with the option -info to determine an optimal value to use

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

3436:    Level: developer

3438: .seealso: MatQRFactor(), MatQRFactorNumeric(), MatLUFactor(), MatFactorInfo, MatFactorInfoInitialize()

3440:     Developer Note: fortran interface is not autogenerated as the f90
3441:     interface definition cannot be generated correctly [due to MatFactorInfo]

3443: @*/
3444: PetscErrorCode MatQRFactorSymbolic(Mat fact,Mat mat,IS col,const MatFactorInfo *info)
3445: {
3447:   MatFactorInfo  tinfo;

3455:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3456:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3457:   MatCheckPreallocated(mat,2);
3458:   if (!info) {
3459:     MatFactorInfoInitialize(&tinfo);
3460:     info = &tinfo;
3461:   }

3463:   if (!fact->trivialsymbolic) {PetscLogEventBegin(MAT_QRFactorSymbolic,fact,mat,col,0);}
3464:   PetscUseMethod(fact,"MatQRFactorSymbolic_C", (Mat,Mat,IS,const MatFactorInfo*), (fact, mat, col, info));
3465:   if (!fact->trivialsymbolic) {PetscLogEventEnd(MAT_QRFactorSymbolic,fact,mat,col,0);}
3466:   PetscObjectStateIncrease((PetscObject)fact);
3467:   return(0);
3468: }

3470: /*@
3471:    MatQRFactorNumeric - Performs numeric QR factorization of a matrix.
3472:    Call this routine after first calling MatQRFactorSymbolic().

3474:    Collective on Mat

3476:    Input Parameters:
3477: +  fact - the factor matrix obtained with MatGetFactor()
3478: .  mat - the matrix
3479: -  info - options for factorization

3481:    Notes:
3482:    See MatQRFactor() for in-place factorization.

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

3488:    Level: developer

3490: .seealso: MatQRFactorSymbolic(), MatLUFactor()

3492:     Developer Note: fortran interface is not autogenerated as the f90
3493:     interface definition cannot be generated correctly [due to MatFactorInfo]

3495: @*/
3496: PetscErrorCode MatQRFactorNumeric(Mat fact,Mat mat,const MatFactorInfo *info)
3497: {
3498:   MatFactorInfo  tinfo;

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

3509:   MatCheckPreallocated(mat,2);
3510:   if (!info) {
3511:     MatFactorInfoInitialize(&tinfo);
3512:     info = &tinfo;
3513:   }

3515:   if (!fact->trivialsymbolic) {PetscLogEventBegin(MAT_QRFactorNumeric,mat,fact,0,0);}
3516:   else  {PetscLogEventBegin(MAT_QRFactor,mat,fact,0,0);}
3517:   PetscUseMethod(fact,"MatQRFactorNumeric_C", (Mat,Mat,const MatFactorInfo*), (fact, mat, info));
3518:   if (!fact->trivialsymbolic) {PetscLogEventEnd(MAT_QRFactorNumeric,mat,fact,0,0);}
3519:   else {PetscLogEventEnd(MAT_QRFactor,mat,fact,0,0);}
3520:   MatViewFromOptions(fact,NULL,"-mat_factor_view");
3521:   PetscObjectStateIncrease((PetscObject)fact);
3522:   return(0);
3523: }

3525: /* ----------------------------------------------------------------*/
3526: /*@
3527:    MatSolve - Solves A x = b, given a factored matrix.

3529:    Neighbor-wise Collective on Mat

3531:    Input Parameters:
3532: +  mat - the factored matrix
3533: -  b - the right-hand-side vector

3535:    Output Parameter:
3536: .  x - the result vector

3538:    Notes:
3539:    The vectors b and x cannot be the same.  I.e., one cannot
3540:    call MatSolve(A,x,x).

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

3547:    Level: developer

3549: .seealso: MatSolveAdd(), MatSolveTranspose(), MatSolveTransposeAdd()
3550: @*/
3551: PetscErrorCode MatSolve(Mat mat,Vec b,Vec x)
3552: {

3562:   if (x == b) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"x and b must be different vectors");
3563:   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);
3564:   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);
3565:   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);
3566:   if (!mat->rmap->N && !mat->cmap->N) return(0);
3567:   MatCheckPreallocated(mat,1);

3569:   PetscLogEventBegin(MAT_Solve,mat,b,x,0);
3570:   if (mat->factorerrortype) {
3571:     PetscInfo1(mat,"MatFactorError %D\n",mat->factorerrortype);
3572:     VecSetInf(x);
3573:   } else {
3574:     if (!mat->ops->solve) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
3575:     (*mat->ops->solve)(mat,b,x);
3576:   }
3577:   PetscLogEventEnd(MAT_Solve,mat,b,x,0);
3578:   PetscObjectStateIncrease((PetscObject)x);
3579:   return(0);
3580: }

3582: static PetscErrorCode MatMatSolve_Basic(Mat A,Mat B,Mat X,PetscBool trans)
3583: {
3585:   Vec            b,x;
3586:   PetscInt       m,N,i;
3587:   PetscScalar    *bb,*xx;
3588:   PetscErrorCode (*f)(Mat,Vec,Vec);

3591:   if (A->factorerrortype) {
3592:     PetscInfo1(A,"MatFactorError %D\n",A->factorerrortype);
3593:     MatSetInf(X);
3594:     return(0);
3595:   }
3596:   f = trans ? A->ops->solvetranspose : A->ops->solve;
3597:   if (!f) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Mat type %s",((PetscObject)A)->type_name);

3599:   MatDenseGetArrayRead(B,(const PetscScalar**)&bb);
3600:   MatDenseGetArray(X,&xx);
3601:   MatGetLocalSize(B,&m,NULL);  /* number local rows */
3602:   MatGetSize(B,NULL,&N);       /* total columns in dense matrix */
3603:   MatCreateVecs(A,&x,&b);
3604:   for (i=0; i<N; i++) {
3605:     VecPlaceArray(b,bb + i*m);
3606:     VecPlaceArray(x,xx + i*m);
3607:     (*f)(A,b,x);
3608:     VecResetArray(x);
3609:     VecResetArray(b);
3610:   }
3611:   VecDestroy(&b);
3612:   VecDestroy(&x);
3613:   MatDenseRestoreArrayRead(B,(const PetscScalar**)&bb);
3614:   MatDenseRestoreArray(X,&xx);
3615:   return(0);
3616: }

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

3621:    Neighbor-wise Collective on Mat

3623:    Input Parameters:
3624: +  A - the factored matrix
3625: -  B - the right-hand-side matrix MATDENSE (or sparse -- when using MUMPS)

3627:    Output Parameter:
3628: .  X - the result matrix (dense matrix)

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

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

3640:    Level: developer

3642: .seealso: MatMatSolveTranspose(), MatLUFactor(), MatCholeskyFactor()
3643: @*/
3644: PetscErrorCode MatMatSolve(Mat A,Mat B,Mat X)
3645: {

3655:   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);
3656:   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);
3657:   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");
3658:   if (!A->rmap->N && !A->cmap->N) return(0);
3659:   if (!A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
3660:   MatCheckPreallocated(A,1);

3662:   PetscLogEventBegin(MAT_MatSolve,A,B,X,0);
3663:   if (!A->ops->matsolve) {
3664:     PetscInfo1(A,"Mat type %s using basic MatMatSolve\n",((PetscObject)A)->type_name);
3665:     MatMatSolve_Basic(A,B,X,PETSC_FALSE);
3666:   } else {
3667:     (*A->ops->matsolve)(A,B,X);
3668:   }
3669:   PetscLogEventEnd(MAT_MatSolve,A,B,X,0);
3670:   PetscObjectStateIncrease((PetscObject)X);
3671:   return(0);
3672: }

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

3677:    Neighbor-wise Collective on Mat

3679:    Input Parameters:
3680: +  A - the factored matrix
3681: -  B - the right-hand-side matrix  (dense matrix)

3683:    Output Parameter:
3684: .  X - the result matrix (dense matrix)

3686:    Notes:
3687:    The matrices B and X cannot be the same.  I.e., one cannot
3688:    call MatMatSolveTranspose(A,X,X).

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

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

3698:    Level: developer

3700: .seealso: MatMatSolve(), MatLUFactor(), MatCholeskyFactor()
3701: @*/
3702: PetscErrorCode MatMatSolveTranspose(Mat A,Mat B,Mat X)
3703: {

3713:   if (X == B) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_IDN,"X and B must be different matrices");
3714:   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);
3715:   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);
3716:   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);
3717:   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");
3718:   if (!A->rmap->N && !A->cmap->N) return(0);
3719:   if (!A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
3720:   MatCheckPreallocated(A,1);

3722:   PetscLogEventBegin(MAT_MatSolve,A,B,X,0);
3723:   if (!A->ops->matsolvetranspose) {
3724:     PetscInfo1(A,"Mat type %s using basic MatMatSolveTranspose\n",((PetscObject)A)->type_name);
3725:     MatMatSolve_Basic(A,B,X,PETSC_TRUE);
3726:   } else {
3727:     (*A->ops->matsolvetranspose)(A,B,X);
3728:   }
3729:   PetscLogEventEnd(MAT_MatSolve,A,B,X,0);
3730:   PetscObjectStateIncrease((PetscObject)X);
3731:   return(0);
3732: }

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

3737:    Neighbor-wise Collective on Mat

3739:    Input Parameters:
3740: +  A - the factored matrix
3741: -  Bt - the transpose of right-hand-side matrix

3743:    Output Parameter:
3744: .  X - the result matrix (dense matrix)

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

3752:    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().

3754:    Level: developer

3756: .seealso: MatMatSolve(), MatMatSolveTranspose(), MatLUFactor(), MatCholeskyFactor()
3757: @*/
3758: PetscErrorCode MatMatTransposeSolve(Mat A,Mat Bt,Mat X)
3759: {


3770:   if (X == Bt) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_IDN,"X and B must be different matrices");
3771:   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);
3772:   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);
3773:   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");
3774:   if (!A->rmap->N && !A->cmap->N) return(0);
3775:   if (!A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
3776:   MatCheckPreallocated(A,1);

3778:   if (!A->ops->mattransposesolve) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Mat type %s",((PetscObject)A)->type_name);
3779:   PetscLogEventBegin(MAT_MatTrSolve,A,Bt,X,0);
3780:   (*A->ops->mattransposesolve)(A,Bt,X);
3781:   PetscLogEventEnd(MAT_MatTrSolve,A,Bt,X,0);
3782:   PetscObjectStateIncrease((PetscObject)X);
3783:   return(0);
3784: }

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

3790:    Neighbor-wise Collective on Mat

3792:    Input Parameters:
3793: +  mat - the factored matrix
3794: -  b - the right-hand-side vector

3796:    Output Parameter:
3797: .  x - the result vector

3799:    Notes:
3800:    MatSolve() should be used for most applications, as it performs
3801:    a forward solve followed by a backward solve.

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

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

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

3816:    Level: developer

3818: .seealso: MatSolve(), MatBackwardSolve()
3819: @*/
3820: PetscErrorCode MatForwardSolve(Mat mat,Vec b,Vec x)
3821: {

3831:   if (x == b) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"x and b must be different vectors");
3832:   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);
3833:   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);
3834:   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);
3835:   if (!mat->rmap->N && !mat->cmap->N) return(0);
3836:   MatCheckPreallocated(mat,1);

3838:   if (!mat->ops->forwardsolve) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
3839:   PetscLogEventBegin(MAT_ForwardSolve,mat,b,x,0);
3840:   (*mat->ops->forwardsolve)(mat,b,x);
3841:   PetscLogEventEnd(MAT_ForwardSolve,mat,b,x,0);
3842:   PetscObjectStateIncrease((PetscObject)x);
3843:   return(0);
3844: }

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

3850:    Neighbor-wise Collective on Mat

3852:    Input Parameters:
3853: +  mat - the factored matrix
3854: -  b - the right-hand-side vector

3856:    Output Parameter:
3857: .  x - the result vector

3859:    Notes:
3860:    MatSolve() should be used for most applications, as it performs
3861:    a forward solve followed by a backward solve.

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

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

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

3876:    Level: developer

3878: .seealso: MatSolve(), MatForwardSolve()
3879: @*/
3880: PetscErrorCode MatBackwardSolve(Mat mat,Vec b,Vec x)
3881: {

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

3898:   if (!mat->ops->backwardsolve) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
3899:   PetscLogEventBegin(MAT_BackwardSolve,mat,b,x,0);
3900:   (*mat->ops->backwardsolve)(mat,b,x);
3901:   PetscLogEventEnd(MAT_BackwardSolve,mat,b,x,0);
3902:   PetscObjectStateIncrease((PetscObject)x);
3903:   return(0);
3904: }

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

3909:    Neighbor-wise Collective on Mat

3911:    Input Parameters:
3912: +  mat - the factored matrix
3913: .  b - the right-hand-side vector
3914: -  y - the vector to be added to

3916:    Output Parameter:
3917: .  x - the result vector

3919:    Notes:
3920:    The vectors b and x cannot be the same.  I.e., one cannot
3921:    call MatSolveAdd(A,x,y,x).

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

3927:    Level: developer

3929: .seealso: MatSolve(), MatSolveTranspose(), MatSolveTransposeAdd()
3930: @*/
3931: PetscErrorCode MatSolveAdd(Mat mat,Vec b,Vec y,Vec x)
3932: {
3933:   PetscScalar    one = 1.0;
3934:   Vec            tmp;

3946:   if (x == b) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"x and b must be different vectors");
3947:   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);
3948:   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);
3949:   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);
3950:   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);
3951:   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);
3952:   if (!mat->rmap->N && !mat->cmap->N) return(0);
3953:    MatCheckPreallocated(mat,1);

3955:   PetscLogEventBegin(MAT_SolveAdd,mat,b,x,y);
3956:   if (mat->factorerrortype) {
3957:     PetscInfo1(mat,"MatFactorError %D\n",mat->factorerrortype);
3958:     VecSetInf(x);
3959:   } else if (mat->ops->solveadd) {
3960:     (*mat->ops->solveadd)(mat,b,y,x);
3961:   } else {
3962:     /* do the solve then the add manually */
3963:     if (x != y) {
3964:       MatSolve(mat,b,x);
3965:       VecAXPY(x,one,y);
3966:     } else {
3967:       VecDuplicate(x,&tmp);
3968:       PetscLogObjectParent((PetscObject)mat,(PetscObject)tmp);
3969:       VecCopy(x,tmp);
3970:       MatSolve(mat,b,x);
3971:       VecAXPY(x,one,tmp);
3972:       VecDestroy(&tmp);
3973:     }
3974:   }
3975:   PetscLogEventEnd(MAT_SolveAdd,mat,b,x,y);
3976:   PetscObjectStateIncrease((PetscObject)x);
3977:   return(0);
3978: }

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

3983:    Neighbor-wise Collective on Mat

3985:    Input Parameters:
3986: +  mat - the factored matrix
3987: -  b - the right-hand-side vector

3989:    Output Parameter:
3990: .  x - the result vector

3992:    Notes:
3993:    The vectors b and x cannot be the same.  I.e., one cannot
3994:    call MatSolveTranspose(A,x,x).

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

4000:    Level: developer

4002: .seealso: MatSolve(), MatSolveAdd(), MatSolveTransposeAdd()
4003: @*/
4004: PetscErrorCode MatSolveTranspose(Mat mat,Vec b,Vec x)
4005: {

4015:   if (x == b) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"x and b must be different vectors");
4016:   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);
4017:   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);
4018:   if (!mat->rmap->N && !mat->cmap->N) return(0);
4019:   MatCheckPreallocated(mat,1);
4020:   PetscLogEventBegin(MAT_SolveTranspose,mat,b,x,0);
4021:   if (mat->factorerrortype) {
4022:     PetscInfo1(mat,"MatFactorError %D\n",mat->factorerrortype);
4023:     VecSetInf(x);
4024:   } else {
4025:     if (!mat->ops->solvetranspose) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Matrix type %s",((PetscObject)mat)->type_name);
4026:     (*mat->ops->solvetranspose)(mat,b,x);
4027:   }
4028:   PetscLogEventEnd(MAT_SolveTranspose,mat,b,x,0);
4029:   PetscObjectStateIncrease((PetscObject)x);
4030:   return(0);
4031: }

4033: /*@
4034:    MatSolveTransposeAdd - Computes x = y + inv(Transpose(A)) b, given a
4035:                       factored matrix.

4037:    Neighbor-wise Collective on Mat

4039:    Input Parameters:
4040: +  mat - the factored matrix
4041: .  b - the right-hand-side vector
4042: -  y - the vector to be added to

4044:    Output Parameter:
4045: .  x - the result vector

4047:    Notes:
4048:    The vectors b and x cannot be the same.  I.e., one cannot
4049:    call MatSolveTransposeAdd(A,x,y,x).

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

4055:    Level: developer

4057: .seealso: MatSolve(), MatSolveAdd(), MatSolveTranspose()
4058: @*/
4059: PetscErrorCode MatSolveTransposeAdd(Mat mat,Vec b,Vec y,Vec x)
4060: {
4061:   PetscScalar    one = 1.0;
4063:   Vec            tmp;

4074:   if (x == b) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"x and b must be different vectors");
4075:   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);
4076:   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);
4077:   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);
4078:   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);
4079:   if (!mat->rmap->N && !mat->cmap->N) return(0);
4080:    MatCheckPreallocated(mat,1);

4082:   PetscLogEventBegin(MAT_SolveTransposeAdd,mat,b,x,y);
4083:   if (mat->factorerrortype) {
4084:     PetscInfo1(mat,"MatFactorError %D\n",mat->factorerrortype);
4085:     VecSetInf(x);
4086:   } else if (mat->ops->solvetransposeadd) {
4087:     (*mat->ops->solvetransposeadd)(mat,b,y,x);
4088:   } else {
4089:     /* do the solve then the add manually */
4090:     if (x != y) {
4091:       MatSolveTranspose(mat,b,x);
4092:       VecAXPY(x,one,y);
4093:     } else {
4094:       VecDuplicate(x,&tmp);
4095:       PetscLogObjectParent((PetscObject)mat,(PetscObject)tmp);
4096:       VecCopy(x,tmp);
4097:       MatSolveTranspose(mat,b,x);
4098:       VecAXPY(x,one,tmp);
4099:       VecDestroy(&tmp);
4100:     }
4101:   }
4102:   PetscLogEventEnd(MAT_SolveTransposeAdd,mat,b,x,y);
4103:   PetscObjectStateIncrease((PetscObject)x);
4104:   return(0);
4105: }
4106: /* ----------------------------------------------------------------*/

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

4111:    Neighbor-wise Collective on Mat

4113:    Input Parameters:
4114: +  mat - the matrix
4115: .  b - the right hand side
4116: .  omega - the relaxation factor
4117: .  flag - flag indicating the type of SOR (see below)
4118: .  shift -  diagonal shift
4119: .  its - the number of iterations
4120: -  lits - the number of local iterations

4122:    Output Parameter:
4123: .  x - the solution (can contain an initial guess, use option SOR_ZERO_INITIAL_GUESS to indicate no guess)

4125:    SOR Flags:
4126: +     SOR_FORWARD_SWEEP - forward SOR
4127: .     SOR_BACKWARD_SWEEP - backward SOR
4128: .     SOR_SYMMETRIC_SWEEP - SSOR (symmetric SOR)
4129: .     SOR_LOCAL_FORWARD_SWEEP - local forward SOR
4130: .     SOR_LOCAL_BACKWARD_SWEEP - local forward SOR
4131: .     SOR_LOCAL_SYMMETRIC_SWEEP - local SSOR
4132: .     SOR_APPLY_UPPER, SOR_APPLY_LOWER - applies
4133:          upper/lower triangular part of matrix to
4134:          vector (with omega)
4135: -     SOR_ZERO_INITIAL_GUESS - zero initial guess

4137:    Notes:
4138:    SOR_LOCAL_FORWARD_SWEEP, SOR_LOCAL_BACKWARD_SWEEP, and
4139:    SOR_LOCAL_SYMMETRIC_SWEEP perform separate independent smoothings
4140:    on each processor.

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

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

4148:    Notes for Advanced Users:
4149:    The flags are implemented as bitwise inclusive or operations.
4150:    For example, use (SOR_ZERO_INITIAL_GUESS | SOR_SYMMETRIC_SWEEP)
4151:    to specify a zero initial guess for SSOR.

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

4157:    Vectors x and b CANNOT be the same

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

4161:    Level: developer

4163: @*/
4164: PetscErrorCode MatSOR(Mat mat,Vec b,PetscReal omega,MatSORType flag,PetscReal shift,PetscInt its,PetscInt lits,Vec x)
4165: {

4175:   if (!mat->ops->sor) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
4176:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
4177:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
4178:   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);
4179:   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);
4180:   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);
4181:   if (its <= 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Relaxation requires global its %D positive",its);
4182:   if (lits <= 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Relaxation requires local its %D positive",lits);
4183:   if (b == x) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"b and x vector cannot be the same");

4185:   MatCheckPreallocated(mat,1);
4186:   PetscLogEventBegin(MAT_SOR,mat,b,x,0);
4187:   ierr =(*mat->ops->sor)(mat,b,omega,flag,shift,its,lits,x);
4188:   PetscLogEventEnd(MAT_SOR,mat,b,x,0);
4189:   PetscObjectStateIncrease((PetscObject)x);
4190:   return(0);
4191: }

4193: /*
4194:       Default matrix copy routine.
4195: */
4196: PetscErrorCode MatCopy_Basic(Mat A,Mat B,MatStructure str)
4197: {
4198:   PetscErrorCode    ierr;
4199:   PetscInt          i,rstart = 0,rend = 0,nz;
4200:   const PetscInt    *cwork;
4201:   const PetscScalar *vwork;

4204:   if (B->assembled) {
4205:     MatZeroEntries(B);
4206:   }
4207:   if (str == SAME_NONZERO_PATTERN) {
4208:     MatGetOwnershipRange(A,&rstart,&rend);
4209:     for (i=rstart; i<rend; i++) {
4210:       MatGetRow(A,i,&nz,&cwork,&vwork);
4211:       MatSetValues(B,1,&i,nz,cwork,vwork,INSERT_VALUES);
4212:       MatRestoreRow(A,i,&nz,&cwork,&vwork);
4213:     }
4214:   } else {
4215:     MatAYPX(B,0.0,A,str);
4216:   }
4217:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
4218:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
4219:   return(0);
4220: }

4222: /*@
4223:    MatCopy - Copies a matrix to another matrix.

4225:    Collective on Mat

4227:    Input Parameters:
4228: +  A - the matrix
4229: -  str - SAME_NONZERO_PATTERN or DIFFERENT_NONZERO_PATTERN

4231:    Output Parameter:
4232: .  B - where the copy is put

4234:    Notes:
4235:    If you use SAME_NONZERO_PATTERN then the two matrices must have the same nonzero pattern or the routine will crash.

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

4241:    Level: intermediate

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

4245: @*/
4246: PetscErrorCode MatCopy(Mat A,Mat B,MatStructure str)
4247: {
4249:   PetscInt       i;

4257:   MatCheckPreallocated(B,2);
4258:   if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
4259:   if (A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
4260:   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);
4261:   MatCheckPreallocated(A,1);
4262:   if (A == B) return(0);

4264:   PetscLogEventBegin(MAT_Copy,A,B,0,0);
4265:   if (A->ops->copy) {
4266:     (*A->ops->copy)(A,B,str);
4267:   } else { /* generic conversion */
4268:     MatCopy_Basic(A,B,str);
4269:   }

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

4278:   PetscLogEventEnd(MAT_Copy,A,B,0,0);
4279:   PetscObjectStateIncrease((PetscObject)B);
4280:   return(0);
4281: }

4283: /*@C
4284:    MatConvert - Converts a matrix to another matrix, either of the same
4285:    or different type.

4287:    Collective on Mat

4289:    Input Parameters:
4290: +  mat - the matrix
4291: .  newtype - new matrix type.  Use MATSAME to create a new matrix of the
4292:    same type as the original matrix.
4293: -  reuse - denotes if the destination matrix is to be created or reused.
4294:    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
4295:    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).

4297:    Output Parameter:
4298: .  M - pointer to place new matrix

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

4305:    Cannot be used to convert a sequential matrix to parallel or parallel to sequential,
4306:    the MPI communicator of the generated matrix is always the same as the communicator
4307:    of the input matrix.

4309:    Level: intermediate

4311: .seealso: MatCopy(), MatDuplicate()
4312: @*/
4313: PetscErrorCode MatConvert(Mat mat, MatType newtype,MatReuse reuse,Mat *M)
4314: {
4316:   PetscBool      sametype,issame,flg,issymmetric,ishermitian;
4317:   char           convname[256],mtype[256];
4318:   Mat            B;

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

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

4331:   PetscObjectTypeCompare((PetscObject)mat,newtype,&sametype);
4332:   PetscStrcmp(newtype,"same",&issame);
4333:   if ((reuse == MAT_INPLACE_MATRIX) && (mat != *M)) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"MAT_INPLACE_MATRIX requires same input and output matrix");
4334:   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");

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

4341:   /* Cache Mat options because some converter use MatHeaderReplace  */
4342:   issymmetric = mat->symmetric;
4343:   ishermitian = mat->hermitian;

4345:   if ((sametype || issame) && (reuse==MAT_INITIAL_MATRIX) && mat->ops->duplicate) {
4346:     PetscInfo3(mat,"Calling duplicate for initial matrix %s %d %d\n",((PetscObject)mat)->type_name,sametype,issame);
4347:     (*mat->ops->duplicate)(mat,MAT_COPY_VALUES,M);
4348:   } else {
4349:     PetscErrorCode (*conv)(Mat, MatType,MatReuse,Mat*)=NULL;
4350:     const char     *prefix[3] = {"seq","mpi",""};
4351:     PetscInt       i;
4352:     /*
4353:        Order of precedence:
4354:        0) See if newtype is a superclass of the current matrix.
4355:        1) See if a specialized converter is known to the current matrix.
4356:        2) See if a specialized converter is known to the desired matrix class.
4357:        3) See if a good general converter is registered for the desired class
4358:           (as of 6/27/03 only MATMPIADJ falls into this category).
4359:        4) See if a good general converter is known for the current matrix.
4360:        5) Use a really basic converter.
4361:     */

4363:     /* 0) See if newtype is a superclass of the current matrix.
4364:           i.e mat is mpiaij and newtype is aij */
4365:     for (i=0; i<2; i++) {
4366:       PetscStrncpy(convname,prefix[i],sizeof(convname));
4367:       PetscStrlcat(convname,newtype,sizeof(convname));
4368:       PetscStrcmp(convname,((PetscObject)mat)->type_name,&flg);
4369:       PetscInfo3(mat,"Check superclass %s %s -> %d\n",convname,((PetscObject)mat)->type_name,flg);
4370:       if (flg) {
4371:         if (reuse == MAT_INPLACE_MATRIX) {
4372:           PetscInfo(mat,"Early return\n");
4373:           return(0);
4374:         } else if (reuse == MAT_INITIAL_MATRIX && mat->ops->duplicate) {
4375:           PetscInfo(mat,"Calling MatDuplicate\n");
4376:           (*mat->ops->duplicate)(mat,MAT_COPY_VALUES,M);
4377:           return(0);
4378:         } else if (reuse == MAT_REUSE_MATRIX && mat->ops->copy) {
4379:           PetscInfo(mat,"Calling MatCopy\n");
4380:           MatCopy(mat,*M,SAME_NONZERO_PATTERN);
4381:           return(0);
4382:         }
4383:       }
4384:     }
4385:     /* 1) See if a specialized converter is known to the current matrix and the desired class */
4386:     for (i=0; i<3; i++) {
4387:       PetscStrncpy(convname,"MatConvert_",sizeof(convname));
4388:       PetscStrlcat(convname,((PetscObject)mat)->type_name,sizeof(convname));
4389:       PetscStrlcat(convname,"_",sizeof(convname));
4390:       PetscStrlcat(convname,prefix[i],sizeof(convname));
4391:       PetscStrlcat(convname,issame ? ((PetscObject)mat)->type_name : newtype,sizeof(convname));
4392:       PetscStrlcat(convname,"_C",sizeof(convname));
4393:       PetscObjectQueryFunction((PetscObject)mat,convname,&conv);
4394:       PetscInfo3(mat,"Check specialized (1) %s (%s) -> %d\n",convname,((PetscObject)mat)->type_name,!!conv);
4395:       if (conv) goto foundconv;
4396:     }

4398:     /* 2)  See if a specialized converter is known to the desired matrix class. */
4399:     MatCreate(PetscObjectComm((PetscObject)mat),&B);
4400:     MatSetSizes(B,mat->rmap->n,mat->cmap->n,mat->rmap->N,mat->cmap->N);
4401:     MatSetType(B,newtype);
4402:     for (i=0; i<3; i++) {
4403:       PetscStrncpy(convname,"MatConvert_",sizeof(convname));
4404:       PetscStrlcat(convname,((PetscObject)mat)->type_name,sizeof(convname));
4405:       PetscStrlcat(convname,"_",sizeof(convname));
4406:       PetscStrlcat(convname,prefix[i],sizeof(convname));
4407:       PetscStrlcat(convname,newtype,sizeof(convname));
4408:       PetscStrlcat(convname,"_C",sizeof(convname));
4409:       PetscObjectQueryFunction((PetscObject)B,convname,&conv);
4410:       PetscInfo3(mat,"Check specialized (2) %s (%s) -> %d\n",convname,((PetscObject)B)->type_name,!!conv);
4411:       if (conv) {
4412:         MatDestroy(&B);
4413:         goto foundconv;
4414:       }
4415:     }

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

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

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

4429:     /* 5) Use a really basic converter. */
4430:     PetscInfo(mat,"Using MatConvert_Basic\n");
4431:     conv = MatConvert_Basic;

4433: foundconv:
4434:     PetscLogEventBegin(MAT_Convert,mat,0,0,0);
4435:     (*conv)(mat,newtype,reuse,M);
4436:     if (mat->rmap->mapping && mat->cmap->mapping && !(*M)->rmap->mapping && !(*M)->cmap->mapping) {
4437:       /* the block sizes must be same if the mappings are copied over */
4438:       (*M)->rmap->bs = mat->rmap->bs;
4439:       (*M)->cmap->bs = mat->cmap->bs;
4440:       PetscObjectReference((PetscObject)mat->rmap->mapping);
4441:       PetscObjectReference((PetscObject)mat->cmap->mapping);
4442:       (*M)->rmap->mapping = mat->rmap->mapping;
4443:       (*M)->cmap->mapping = mat->cmap->mapping;
4444:     }
4445:     (*M)->stencil.dim = mat->stencil.dim;
4446:     (*M)->stencil.noc = mat->stencil.noc;
4447:     for (i=0; i<=mat->stencil.dim; i++) {
4448:       (*M)->stencil.dims[i]   = mat->stencil.dims[i];
4449:       (*M)->stencil.starts[i] = mat->stencil.starts[i];
4450:     }
4451:     PetscLogEventEnd(MAT_Convert,mat,0,0,0);
4452:   }
4453:   PetscObjectStateIncrease((PetscObject)*M);

4455:   /* Copy Mat options */
4456:   if (issymmetric) {
4457:     MatSetOption(*M,MAT_SYMMETRIC,PETSC_TRUE);
4458:   }
4459:   if (ishermitian) {
4460:     MatSetOption(*M,MAT_HERMITIAN,PETSC_TRUE);
4461:   }
4462:   return(0);
4463: }

4465: /*@C
4466:    MatFactorGetSolverType - Returns name of the package providing the factorization routines

4468:    Not Collective

4470:    Input Parameter:
4471: .  mat - the matrix, must be a factored matrix

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

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

4480:    Level: intermediate

4482: .seealso: MatCopy(), MatDuplicate(), MatGetFactorAvailable(), MatGetFactor()
4483: @*/
4484: PetscErrorCode MatFactorGetSolverType(Mat mat, MatSolverType *type)
4485: {
4486:   PetscErrorCode ierr, (*conv)(Mat,MatSolverType*);

4491:   if (!mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Only for factored matrix");
4492:   PetscObjectQueryFunction((PetscObject)mat,"MatFactorGetSolverType_C",&conv);
4493:   if (!conv) {
4494:     *type = MATSOLVERPETSC;
4495:   } else {
4496:     (*conv)(mat,type);
4497:   }
4498:   return(0);
4499: }

4501: typedef struct _MatSolverTypeForSpecifcType* MatSolverTypeForSpecifcType;
4502: struct _MatSolverTypeForSpecifcType {
4503:   MatType                        mtype;
4504:   /* no entry for MAT_FACTOR_NONE */
4505:   PetscErrorCode                 (*createfactor[MAT_FACTOR_NUM_TYPES-1])(Mat,MatFactorType,Mat*);
4506:   MatSolverTypeForSpecifcType next;
4507: };

4509: typedef struct _MatSolverTypeHolder* MatSolverTypeHolder;
4510: struct _MatSolverTypeHolder {
4511:   char                        *name;
4512:   MatSolverTypeForSpecifcType handlers;
4513:   MatSolverTypeHolder         next;
4514: };

4516: static MatSolverTypeHolder MatSolverTypeHolders = NULL;

4518: /*@C
4519:    MatSolverTypeRegister - Registers a MatSolverType that works for a particular matrix type

4521:    Input Parameters:
4522: +    package - name of the package, for example petsc or superlu
4523: .    mtype - the matrix type that works with this package
4524: .    ftype - the type of factorization supported by the package
4525: -    createfactor - routine that will create the factored matrix ready to be used

4527:     Level: intermediate

4529: .seealso: MatCopy(), MatDuplicate(), MatGetFactorAvailable(), MatGetFactor()
4530: @*/
4531: PetscErrorCode MatSolverTypeRegister(MatSolverType package,MatType mtype,MatFactorType ftype,PetscErrorCode (*createfactor)(Mat,MatFactorType,Mat*))
4532: {
4533:   PetscErrorCode              ierr;
4534:   MatSolverTypeHolder         next = MatSolverTypeHolders,prev = NULL;
4535:   PetscBool                   flg;
4536:   MatSolverTypeForSpecifcType inext,iprev = NULL;

4539:   MatInitializePackage();
4540:   if (!next) {
4541:     PetscNew(&MatSolverTypeHolders);
4542:     PetscStrallocpy(package,&MatSolverTypeHolders->name);
4543:     PetscNew(&MatSolverTypeHolders->handlers);
4544:     PetscStrallocpy(mtype,(char **)&MatSolverTypeHolders->handlers->mtype);
4545:     MatSolverTypeHolders->handlers->createfactor[(int)ftype-1] = createfactor;
4546:     return(0);
4547:   }
4548:   while (next) {
4549:     PetscStrcasecmp(package,next->name,&flg);
4550:     if (flg) {
4551:       if (!next->handlers) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"MatSolverTypeHolder is missing handlers");
4552:       inext = next->handlers;
4553:       while (inext) {
4554:         PetscStrcasecmp(mtype,inext->mtype,&flg);
4555:         if (flg) {
4556:           inext->createfactor[(int)ftype-1] = createfactor;
4557:           return(0);
4558:         }
4559:         iprev = inext;
4560:         inext = inext->next;
4561:       }
4562:       PetscNew(&iprev->next);
4563:       PetscStrallocpy(mtype,(char **)&iprev->next->mtype);
4564:       iprev->next->createfactor[(int)ftype-1] = createfactor;
4565:       return(0);
4566:     }
4567:     prev = next;
4568:     next = next->next;
4569:   }
4570:   PetscNew(&prev->next);
4571:   PetscStrallocpy(package,&prev->next->name);
4572:   PetscNew(&prev->next->handlers);
4573:   PetscStrallocpy(mtype,(char **)&prev->next->handlers->mtype);
4574:   prev->next->handlers->createfactor[(int)ftype-1] = createfactor;
4575:   return(0);
4576: }

4578: /*@C
4579:    MatSolverTypeGet - Gets the function that creates the factor matrix if it exist

4581:    Input Parameters:
4582: +    type - name of the package, for example petsc or superlu
4583: .    ftype - the type of factorization supported by the type
4584: -    mtype - the matrix type that works with this type

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

4591:     Level: intermediate

4593: .seealso: MatCopy(), MatDuplicate(), MatGetFactorAvailable(), MatSolverTypeRegister(), MatGetFactor()
4594: @*/
4595: PetscErrorCode MatSolverTypeGet(MatSolverType type,MatType mtype,MatFactorType ftype,PetscBool *foundtype,PetscBool *foundmtype,PetscErrorCode (**createfactor)(Mat,MatFactorType,Mat*))
4596: {
4597:   PetscErrorCode              ierr;
4598:   MatSolverTypeHolder         next = MatSolverTypeHolders;
4599:   PetscBool                   flg;
4600:   MatSolverTypeForSpecifcType inext;

4603:   if (foundtype) *foundtype = PETSC_FALSE;
4604:   if (foundmtype)   *foundmtype   = PETSC_FALSE;
4605:   if (createfactor) *createfactor    = NULL;

4607:   if (type) {
4608:     while (next) {
4609:       PetscStrcasecmp(type,next->name,&flg);
4610:       if (flg) {
4611:         if (foundtype) *foundtype = PETSC_TRUE;
4612:         inext = next->handlers;
4613:         while (inext) {
4614:           PetscStrbeginswith(mtype,inext->mtype,&flg);
4615:           if (flg) {
4616:             if (foundmtype) *foundmtype = PETSC_TRUE;
4617:             if (createfactor)  *createfactor  = inext->createfactor[(int)ftype-1];
4618:             return(0);
4619:           }
4620:           inext = inext->next;
4621:         }
4622:       }
4623:       next = next->next;
4624:     }
4625:   } else {
4626:     while (next) {
4627:       inext = next->handlers;
4628:       while (inext) {
4629:         PetscStrcmp(mtype,inext->mtype,&flg);
4630:         if (flg && inext->createfactor[(int)ftype-1]) {
4631:           if (foundtype) *foundtype = PETSC_TRUE;
4632:           if (foundmtype)   *foundmtype   = PETSC_TRUE;
4633:           if (createfactor) *createfactor = inext->createfactor[(int)ftype-1];
4634:           return(0);
4635:         }
4636:         inext = inext->next;
4637:       }
4638:       next = next->next;
4639:     }
4640:     /* try with base classes inext->mtype */
4641:     next = MatSolverTypeHolders;
4642:     while (next) {
4643:       inext = next->handlers;
4644:       while (inext) {
4645:         PetscStrbeginswith(mtype,inext->mtype,&flg);
4646:         if (flg && inext->createfactor[(int)ftype-1]) {
4647:           if (foundtype) *foundtype = PETSC_TRUE;
4648:           if (foundmtype)   *foundmtype   = PETSC_TRUE;
4649:           if (createfactor) *createfactor = inext->createfactor[(int)ftype-1];
4650:           return(0);
4651:         }
4652:         inext = inext->next;
4653:       }
4654:       next = next->next;
4655:     }
4656:   }
4657:   return(0);
4658: }

4660: PetscErrorCode MatSolverTypeDestroy(void)
4661: {
4662:   PetscErrorCode              ierr;
4663:   MatSolverTypeHolder         next = MatSolverTypeHolders,prev;
4664:   MatSolverTypeForSpecifcType inext,iprev;

4667:   while (next) {
4668:     PetscFree(next->name);
4669:     inext = next->handlers;
4670:     while (inext) {
4671:       PetscFree(inext->mtype);
4672:       iprev = inext;
4673:       inext = inext->next;
4674:       PetscFree(iprev);
4675:     }
4676:     prev = next;
4677:     next = next->next;
4678:     PetscFree(prev);
4679:   }
4680:   MatSolverTypeHolders = NULL;
4681:   return(0);
4682: }

4684: /*@C
4685:    MatFactorGetCanUseOrdering - Indicates if the factorization can use the ordering provided in MatLUFactorSymbolic(), MatCholeskyFactorSymbolic()

4687:    Logically Collective on Mat

4689:    Input Parameters:
4690: .  mat - the matrix

4692:    Output Parameters:
4693: .  flg - PETSC_TRUE if uses the ordering

4695:    Notes:
4696:       Most internal PETSc factorizations use the ordering passed to the factorization routine but external
4697:       packages do not, thus we want to skip generating the ordering when it is not needed or used.

4699:    Level: developer

4701: .seealso: MatCopy(), MatDuplicate(), MatGetFactorAvailable(), MatGetFactor(), MatLUFactorSymbolic(), MatCholeskyFactorSymbolic()
4702: @*/
4703: PetscErrorCode MatFactorGetCanUseOrdering(Mat mat, PetscBool *flg)
4704: {
4706:   *flg = mat->canuseordering;
4707:   return(0);
4708: }

4710: /*@C
4711:    MatFactorGetPreferredOrdering - The preferred ordering for a particular matrix factor object

4713:    Logically Collective on Mat

4715:    Input Parameters:
4716: .  mat - the matrix

4718:    Output Parameters:
4719: .  otype - the preferred type

4721:    Level: developer

4723: .seealso: MatCopy(), MatDuplicate(), MatGetFactorAvailable(), MatGetFactor(), MatLUFactorSymbolic(), MatCholeskyFactorSymbolic()
4724: @*/
4725: PetscErrorCode MatFactorGetPreferredOrdering(Mat mat, MatFactorType ftype, MatOrderingType *otype)
4726: {
4728:   *otype = mat->preferredordering[ftype];
4729:   if (!*otype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"MatFactor did not have a preferred ordering");
4730:   return(0);
4731: }

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

4736:    Collective on Mat

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

4743:    Output Parameters:
4744: .  f - the factor matrix used with MatXXFactorSymbolic() calls

4746:    Notes:
4747:       Some PETSc matrix formats have alternative solvers available that are contained in alternative packages
4748:      such as pastix, superlu, mumps etc.

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

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

4755:    Level: intermediate

4757: .seealso: MatCopy(), MatDuplicate(), MatGetFactorAvailable(), MatFactorGetCanUseOrdering(), MatSolverTypeRegister()
4758: @*/
4759: PetscErrorCode MatGetFactor(Mat mat, MatSolverType type,MatFactorType ftype,Mat *f)
4760: {
4761:   PetscErrorCode ierr,(*conv)(Mat,MatFactorType,Mat*);
4762:   PetscBool      foundtype,foundmtype;


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

4771:   MatSolverTypeGet(type,((PetscObject)mat)->type_name,ftype,&foundtype,&foundmtype,&conv);
4772:   if (!foundtype) {
4773:     if (type) {
4774:       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);
4775:     } else {
4776:       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);
4777:     }
4778:   }
4779:   if (!foundmtype) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_MISSING_FACTOR,"MatSolverType %s does not support matrix type %s",type,((PetscObject)mat)->type_name);
4780:   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);

4782:   (*conv)(mat,ftype,f);
4783:   return(0);
4784: }

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

4789:    Not Collective

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

4796:    Output Parameter:
4797: .    flg - PETSC_TRUE if the factorization is available

4799:    Notes:
4800:       Some PETSc matrix formats have alternative solvers available that are contained in alternative packages
4801:      such as pastix, superlu, mumps etc.

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

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

4808:    Level: intermediate

4810: .seealso: MatCopy(), MatDuplicate(), MatGetFactor(), MatSolverTypeRegister()
4811: @*/
4812: PetscErrorCode MatGetFactorAvailable(Mat mat, MatSolverType type,MatFactorType ftype,PetscBool  *flg)
4813: {
4814:   PetscErrorCode ierr, (*gconv)(Mat,MatFactorType,Mat*);


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

4823:   *flg = PETSC_FALSE;
4824:   MatSolverTypeGet(type,((PetscObject)mat)->type_name,ftype,NULL,NULL,&gconv);
4825:   if (gconv) {
4826:     *flg = PETSC_TRUE;
4827:   }
4828:   return(0);
4829: }

4831: #include <petscdmtypes.h>

4833: /*@
4834:    MatDuplicate - Duplicates a matrix including the non-zero structure.

4836:    Collective on Mat

4838:    Input Parameters:
4839: +  mat - the matrix
4840: -  op - One of MAT_DO_NOT_COPY_VALUES, MAT_COPY_VALUES, or MAT_SHARE_NONZERO_PATTERN.
4841:         See the manual page for MatDuplicateOption for an explanation of these options.

4843:    Output Parameter:
4844: .  M - pointer to place new matrix

4846:    Level: intermediate

4848:    Notes:
4849:     You cannot change the nonzero pattern for the parent or child matrix if you use MAT_SHARE_NONZERO_PATTERN.
4850:     May be called with an unassembled input Mat if MAT_DO_NOT_COPY_VALUES is used, in which case the output Mat is unassembled as well.
4851:     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.

4853: .seealso: MatCopy(), MatConvert(), MatDuplicateOption
4854: @*/
4855: PetscErrorCode MatDuplicate(Mat mat,MatDuplicateOption op,Mat *M)
4856: {
4858:   Mat            B;
4859:   PetscInt       i;
4860:   DM             dm;
4861:   void           (*viewf)(void);

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

4871:   *M = NULL;
4872:   if (!mat->ops->duplicate) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Not written for matrix type %s\n",((PetscObject)mat)->type_name);
4873:   PetscLogEventBegin(MAT_Convert,mat,0,0,0);
4874:   (*mat->ops->duplicate)(mat,op,M);
4875:   B    = *M;

4877:   MatGetOperation(mat,MATOP_VIEW,&viewf);
4878:   if (viewf) {
4879:     MatSetOperation(B,MATOP_VIEW,viewf);
4880:   }

4882:   B->stencil.dim = mat->stencil.dim;
4883:   B->stencil.noc = mat->stencil.noc;
4884:   for (i=0; i<=mat->stencil.dim; i++) {
4885:     B->stencil.dims[i]   = mat->stencil.dims[i];
4886:     B->stencil.starts[i] = mat->stencil.starts[i];
4887:   }

4889:   B->nooffproczerorows = mat->nooffproczerorows;
4890:   B->nooffprocentries  = mat->nooffprocentries;

4892:   PetscObjectQuery((PetscObject) mat, "__PETSc_dm", (PetscObject*) &dm);
4893:   if (dm) {
4894:     PetscObjectCompose((PetscObject) B, "__PETSc_dm", (PetscObject) dm);
4895:   }
4896:   PetscLogEventEnd(MAT_Convert,mat,0,0,0);
4897:   PetscObjectStateIncrease((PetscObject)B);
4898:   return(0);
4899: }

4901: /*@
4902:    MatGetDiagonal - Gets the diagonal of a matrix.

4904:    Logically Collective on Mat

4906:    Input Parameters:
4907: +  mat - the matrix
4908: -  v - the vector for storing the diagonal

4910:    Output Parameter:
4911: .  v - the diagonal of the matrix

4913:    Level: intermediate

4915:    Note:
4916:    Currently only correct in parallel for square matrices.

4918: .seealso: MatGetRow(), MatCreateSubMatrices(), MatCreateSubMatrix(), MatGetRowMaxAbs()
4919: @*/
4920: PetscErrorCode MatGetDiagonal(Mat mat,Vec v)
4921: {

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

4932:   (*mat->ops->getdiagonal)(mat,v);
4933:   PetscObjectStateIncrease((PetscObject)v);
4934:   return(0);
4935: }

4937: /*@C
4938:    MatGetRowMin - Gets the minimum value (of the real part) of each
4939:         row of the matrix

4941:    Logically Collective on Mat

4943:    Input Parameter:
4944: .  mat - the matrix

4946:    Output Parameters:
4947: +  v - the vector for storing the maximums
4948: -  idx - the indices of the column found for each row (optional)

4950:    Level: intermediate

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

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

4958: .seealso: MatGetDiagonal(), MatCreateSubMatrices(), MatCreateSubMatrix(), MatGetRowMaxAbs(),
4959:           MatGetRowMax()
4960: @*/
4961: PetscErrorCode MatGetRowMin(Mat mat,Vec v,PetscInt idx[])
4962: {

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

4971:   if (!mat->cmap->N) {
4972:     VecSet(v,PETSC_MAX_REAL);
4973:     if (idx) {
4974:       PetscInt i,m = mat->rmap->n;
4975:       for (i=0; i<m; i++) idx[i] = -1;
4976:     }
4977:   } else {
4978:     if (!mat->ops->getrowmin) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
4979:     MatCheckPreallocated(mat,1);
4980:   }
4981:   (*mat->ops->getrowmin)(mat,v,idx);
4982:   PetscObjectStateIncrease((PetscObject)v);
4983:   return(0);
4984: }

4986: /*@C
4987:    MatGetRowMinAbs - Gets the minimum value (in absolute value) of each
4988:         row of the matrix

4990:    Logically Collective on Mat

4992:    Input Parameter:
4993: .  mat - the matrix

4995:    Output Parameters:
4996: +  v - the vector for storing the minimums
4997: -  idx - the indices of the column found for each row (or NULL if not needed)

4999:    Level: intermediate

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

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

5007: .seealso: MatGetDiagonal(), MatCreateSubMatrices(), MatCreateSubMatrix(), MatGetRowMax(), MatGetRowMaxAbs(), MatGetRowMin()
5008: @*/
5009: PetscErrorCode MatGetRowMinAbs(Mat mat,Vec v,PetscInt idx[])
5010: {

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

5020:   if (!mat->cmap->N) {
5021:     VecSet(v,0.0);
5022:     if (idx) {
5023:       PetscInt i,m = mat->rmap->n;
5024:       for (i=0; i<m; i++) idx[i] = -1;
5025:     }
5026:   } else {
5027:     if (!mat->ops->getrowminabs) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
5028:     MatCheckPreallocated(mat,1);
5029:     if (idx) {PetscArrayzero(idx,mat->rmap->n);}
5030:     (*mat->ops->getrowminabs)(mat,v,idx);
5031:   }
5032:   PetscObjectStateIncrease((PetscObject)v);
5033:   return(0);
5034: }

5036: /*@C
5037:    MatGetRowMax - Gets the maximum value (of the real part) of each
5038:         row of the matrix

5040:    Logically Collective on Mat

5042:    Input Parameter:
5043: .  mat - the matrix

5045:    Output Parameters:
5046: +  v - the vector for storing the maximums
5047: -  idx - the indices of the column found for each row (optional)

5049:    Level: intermediate

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

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

5057: .seealso: MatGetDiagonal(), MatCreateSubMatrices(), MatCreateSubMatrix(), MatGetRowMaxAbs(), MatGetRowMin()
5058: @*/
5059: PetscErrorCode MatGetRowMax(Mat mat,Vec v,PetscInt idx[])
5060: {

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

5069:   if (!mat->cmap->N) {
5070:     VecSet(v,PETSC_MIN_REAL);
5071:     if (idx) {
5072:       PetscInt i,m = mat->rmap->n;
5073:       for (i=0; i<m; i++) idx[i] = -1;
5074:     }
5075:   } else {
5076:     if (!mat->ops->getrowmax) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
5077:     MatCheckPreallocated(mat,1);
5078:     (*mat->ops->getrowmax)(mat,v,idx);
5079:   }
5080:   PetscObjectStateIncrease((PetscObject)v);
5081:   return(0);
5082: }

5084: /*@C
5085:    MatGetRowMaxAbs - Gets the maximum value (in absolute value) of each
5086:         row of the matrix

5088:    Logically Collective on Mat

5090:    Input Parameter:
5091: .  mat - the matrix

5093:    Output Parameters:
5094: +  v - the vector for storing the maximums
5095: -  idx - the indices of the column found for each row (or NULL if not needed)

5097:    Level: intermediate

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

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

5105: .seealso: MatGetDiagonal(), MatCreateSubMatrices(), MatCreateSubMatrix(), MatGetRowMax(), MatGetRowMin()
5106: @*/
5107: PetscErrorCode MatGetRowMaxAbs(Mat mat,Vec v,PetscInt idx[])
5108: {

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

5117:   if (!mat->cmap->N) {
5118:     VecSet(v,0.0);
5119:     if (idx) {
5120:       PetscInt i,m = mat->rmap->n;
5121:       for (i=0; i<m; i++) idx[i] = -1;
5122:     }
5123:   } else {
5124:     if (!mat->ops->getrowmaxabs) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
5125:     MatCheckPreallocated(mat,1);
5126:     if (idx) {PetscArrayzero(idx,mat->rmap->n);}
5127:     (*mat->ops->getrowmaxabs)(mat,v,idx);
5128:   }
5129:   PetscObjectStateIncrease((PetscObject)v);
5130:   return(0);
5131: }

5133: /*@
5134:    MatGetRowSum - Gets the sum of each row of the matrix

5136:    Logically or Neighborhood Collective on Mat

5138:    Input Parameters:
5139: .  mat - the matrix

5141:    Output Parameter:
5142: .  v - the vector for storing the sum of rows

5144:    Level: intermediate

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

5149: .seealso: MatGetDiagonal(), MatCreateSubMatrices(), MatCreateSubMatrix(), MatGetRowMax(), MatGetRowMin()
5150: @*/
5151: PetscErrorCode MatGetRowSum(Mat mat, Vec v)
5152: {
5153:   Vec            ones;

5160:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
5161:   MatCheckPreallocated(mat,1);
5162:   MatCreateVecs(mat,&ones,NULL);
5163:   VecSet(ones,1.);
5164:   MatMult(mat,ones,v);
5165:   VecDestroy(&ones);
5166:   return(0);
5167: }

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

5172:    Collective on Mat

5174:    Input Parameters:
5175: +  mat - the matrix to transpose
5176: -  reuse - either MAT_INITIAL_MATRIX, MAT_REUSE_MATRIX, or MAT_INPLACE_MATRIX

5178:    Output Parameter:
5179: .  B - the transpose

5181:    Notes:
5182:      If you use MAT_INPLACE_MATRIX then you must pass in &mat for B

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

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

5188:    Level: intermediate

5190: .seealso: MatMultTranspose(), MatMultTransposeAdd(), MatIsTranspose(), MatReuse
5191: @*/
5192: PetscErrorCode MatTranspose(Mat mat,MatReuse reuse,Mat *B)
5193: {

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

5206:   PetscLogEventBegin(MAT_Transpose,mat,0,0,0);
5207:   (*mat->ops->transpose)(mat,reuse,B);
5208:   PetscLogEventEnd(MAT_Transpose,mat,0,0,0);
5209:   if (B) {PetscObjectStateIncrease((PetscObject)*B);}
5210:   return(0);
5211: }

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

5217:    Collective on Mat

5219:    Input Parameters:
5220: +  A - the matrix to test
5221: -  B - the matrix to test against, this can equal the first parameter

5223:    Output Parameters:
5224: .  flg - the result

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

5231:    Level: intermediate

5233: .seealso: MatTranspose(), MatIsSymmetric(), MatIsHermitian()
5234: @*/
5235: PetscErrorCode MatIsTranspose(Mat A,Mat B,PetscReal tol,PetscBool  *flg)
5236: {
5237:   PetscErrorCode ierr,(*f)(Mat,Mat,PetscReal,PetscBool*),(*g)(Mat,Mat,PetscReal,PetscBool*);

5243:   PetscObjectQueryFunction((PetscObject)A,"MatIsTranspose_C",&f);
5244:   PetscObjectQueryFunction((PetscObject)B,"MatIsTranspose_C",&g);
5245:   *flg = PETSC_FALSE;
5246:   if (f && g) {
5247:     if (f == g) {
5248:       (*f)(A,B,tol,flg);
5249:     } else SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_NOTSAMETYPE,"Matrices do not have the same comparator for symmetry test");
5250:   } else {
5251:     MatType mattype;
5252:     if (!f) {
5253:       MatGetType(A,&mattype);
5254:     } else {
5255:       MatGetType(B,&mattype);
5256:     }
5257:     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type %s does not support checking for transpose",mattype);
5258:   }
5259:   return(0);
5260: }

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

5265:    Collective on Mat

5267:    Input Parameters:
5268: +  mat - the matrix to transpose and complex conjugate
5269: -  reuse - either MAT_INITIAL_MATRIX, MAT_REUSE_MATRIX, or MAT_INPLACE_MATRIX

5271:    Output Parameter:
5272: .  B - the Hermitian

5274:    Level: intermediate

5276: .seealso: MatTranspose(), MatMultTranspose(), MatMultTransposeAdd(), MatIsTranspose(), MatReuse
5277: @*/
5278: PetscErrorCode MatHermitianTranspose(Mat mat,MatReuse reuse,Mat *B)
5279: {

5283:   MatTranspose(mat,reuse,B);
5284: #if defined(PETSC_USE_COMPLEX)
5285:   MatConjugate(*B);
5286: #endif
5287:   return(0);
5288: }

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

5293:    Collective on Mat

5295:    Input Parameters:
5296: +  A - the matrix to test
5297: -  B - the matrix to test against, this can equal the first parameter

5299:    Output Parameters:
5300: .  flg - the result

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

5307:    Level: intermediate

5309: .seealso: MatTranspose(), MatIsSymmetric(), MatIsHermitian(), MatIsTranspose()
5310: @*/
5311: PetscErrorCode MatIsHermitianTranspose(Mat A,Mat B,PetscReal tol,PetscBool  *flg)
5312: {
5313:   PetscErrorCode ierr,(*f)(Mat,Mat,PetscReal,PetscBool*),(*g)(Mat,Mat,PetscReal,PetscBool*);

5319:   PetscObjectQueryFunction((PetscObject)A,"MatIsHermitianTranspose_C",&f);
5320:   PetscObjectQueryFunction((PetscObject)B,"MatIsHermitianTranspose_C",&g);
5321:   if (f && g) {
5322:     if (f==g) {
5323:       (*f)(A,B,tol,flg);
5324:     } else SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_NOTSAMETYPE,"Matrices do not have the same comparator for Hermitian test");
5325:   }
5326:   return(0);
5327: }

5329: /*@
5330:    MatPermute - Creates a new matrix with rows and columns permuted from the
5331:    original.

5333:    Collective on Mat

5335:    Input Parameters:
5336: +  mat - the matrix to permute
5337: .  row - row permutation, each processor supplies only the permutation for its rows
5338: -  col - column permutation, each processor supplies only the permutation for its columns

5340:    Output Parameters:
5341: .  B - the permuted matrix

5343:    Level: advanced

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

5349:    Developer Note:
5350:      If you want to implement MatPermute for a matrix type, and your approach doesn't
5351:      exploit the fact that row and col are permutations, consider implementing the
5352:      more general MatCreateSubMatrix() instead.

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

5356: @*/
5357: PetscErrorCode MatPermute(Mat mat,IS row,IS col,Mat *B)
5358: {

5369:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
5370:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
5371:   if (!mat->ops->permute && !mat->ops->createsubmatrix) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"MatPermute not available for Mat type %s",((PetscObject)mat)->type_name);
5372:   MatCheckPreallocated(mat,1);

5374:   if (mat->ops->permute) {
5375:     (*mat->ops->permute)(mat,row,col,B);
5376:     PetscObjectStateIncrease((PetscObject)*B);
5377:   } else {
5378:     MatCreateSubMatrix(mat, row, col, MAT_INITIAL_MATRIX, B);
5379:   }
5380:   return(0);
5381: }

5383: /*@
5384:    MatEqual - Compares two matrices.

5386:    Collective on Mat

5388:    Input Parameters:
5389: +  A - the first matrix
5390: -  B - the second matrix

5392:    Output Parameter:
5393: .  flg - PETSC_TRUE if the matrices are equal; PETSC_FALSE otherwise.

5395:    Level: intermediate

5397: @*/
5398: PetscErrorCode MatEqual(Mat A,Mat B,PetscBool  *flg)
5399: {

5409:   MatCheckPreallocated(B,2);
5410:   if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
5411:   if (!B->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
5412:   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);
5413:   if (!A->ops->equal) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Mat type %s",((PetscObject)A)->type_name);
5414:   if (!B->ops->equal) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Mat type %s",((PetscObject)B)->type_name);
5415:   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);
5416:   MatCheckPreallocated(A,1);

5418:   (*A->ops->equal)(A,B,flg);
5419:   return(0);
5420: }

5422: /*@
5423:    MatDiagonalScale - Scales a matrix on the left and right by diagonal
5424:    matrices that are stored as vectors.  Either of the two scaling
5425:    matrices can be NULL.

5427:    Collective on Mat

5429:    Input Parameters:
5430: +  mat - the matrix to be scaled
5431: .  l - the left scaling vector (or NULL)
5432: -  r - the right scaling vector (or NULL)

5434:    Notes:
5435:    MatDiagonalScale() computes A = LAR, where
5436:    L = a diagonal matrix (stored as a vector), R = a diagonal matrix (stored as a vector)
5437:    The L scales the rows of the matrix, the R scales the columns of the matrix.

5439:    Level: intermediate

5441: .seealso: MatScale(), MatShift(), MatDiagonalSet()
5442: @*/
5443: PetscErrorCode MatDiagonalScale(Mat mat,Vec l,Vec r)
5444: {

5452:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
5453:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
5454:   MatCheckPreallocated(mat,1);
5455:   if (!l && !r) return(0);

5457:   if (!mat->ops->diagonalscale) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
5458:   PetscLogEventBegin(MAT_Scale,mat,0,0,0);
5459:   (*mat->ops->diagonalscale)(mat,l,r);
5460:   PetscLogEventEnd(MAT_Scale,mat,0,0,0);
5461:   PetscObjectStateIncrease((PetscObject)mat);
5462:   return(0);
5463: }

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

5468:     Logically Collective on Mat

5470:     Input Parameters:
5471: +   mat - the matrix to be scaled
5472: -   a  - the scaling value

5474:     Output Parameter:
5475: .   mat - the scaled matrix

5477:     Level: intermediate

5479: .seealso: MatDiagonalScale()
5480: @*/
5481: PetscErrorCode MatScale(Mat mat,PetscScalar a)
5482: {

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

5494:   PetscLogEventBegin(MAT_Scale,mat,0,0,0);
5495:   if (a != (PetscScalar)1.0) {
5496:     (*mat->ops->scale)(mat,a);
5497:     PetscObjectStateIncrease((PetscObject)mat);
5498:   }
5499:   PetscLogEventEnd(MAT_Scale,mat,0,0,0);
5500:   return(0);
5501: }

5503: /*@
5504:    MatNorm - Calculates various norms of a matrix.

5506:    Collective on Mat

5508:    Input Parameters:
5509: +  mat - the matrix
5510: -  type - the type of norm, NORM_1, NORM_FROBENIUS, NORM_INFINITY

5512:    Output Parameter:
5513: .  nrm - the resulting norm

5515:    Level: intermediate

5517: @*/
5518: PetscErrorCode MatNorm(Mat mat,NormType type,PetscReal *nrm)
5519: {


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

5532:   (*mat->ops->norm)(mat,type,nrm);
5533:   return(0);
5534: }

5536: /*
5537:      This variable is used to prevent counting of MatAssemblyBegin() that
5538:    are called from within a MatAssemblyEnd().
5539: */
5540: static PetscInt MatAssemblyEnd_InUse = 0;
5541: /*@
5542:    MatAssemblyBegin - Begins assembling the matrix.  This routine should
5543:    be called after completing all calls to MatSetValues().

5545:    Collective on Mat

5547:    Input Parameters:
5548: +  mat - the matrix
5549: -  type - type of assembly, either MAT_FLUSH_ASSEMBLY or MAT_FINAL_ASSEMBLY

5551:    Notes:
5552:    MatSetValues() generally caches the values.  The matrix is ready to
5553:    use only after MatAssemblyBegin() and MatAssemblyEnd() have been called.
5554:    Use MAT_FLUSH_ASSEMBLY when switching between ADD_VALUES and INSERT_VALUES
5555:    in MatSetValues(); use MAT_FINAL_ASSEMBLY for the final assembly before
5556:    using the matrix.

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

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

5566:    Level: beginner

5568: .seealso: MatAssemblyEnd(), MatSetValues(), MatAssembled()
5569: @*/
5570: PetscErrorCode MatAssemblyBegin(Mat mat,MatAssemblyType type)
5571: {

5577:   MatCheckPreallocated(mat,1);
5578:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix.\nDid you forget to call MatSetUnfactored()?");
5579:   if (mat->assembled) {
5580:     mat->was_assembled = PETSC_TRUE;
5581:     mat->assembled     = PETSC_FALSE;
5582:   }

5584:   if (!MatAssemblyEnd_InUse) {
5585:     PetscLogEventBegin(MAT_AssemblyBegin,mat,0,0,0);
5586:     if (mat->ops->assemblybegin) {(*mat->ops->assemblybegin)(mat,type);}
5587:     PetscLogEventEnd(MAT_AssemblyBegin,mat,0,0,0);
5588:   } else if (mat->ops->assemblybegin) {
5589:     (*mat->ops->assemblybegin)(mat,type);
5590:   }
5591:   return(0);
5592: }

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

5598:    Not Collective

5600:    Input Parameter:
5601: .  mat - the matrix

5603:    Output Parameter:
5604: .  assembled - PETSC_TRUE or PETSC_FALSE

5606:    Level: advanced

5608: .seealso: MatAssemblyEnd(), MatSetValues(), MatAssemblyBegin()
5609: @*/
5610: PetscErrorCode MatAssembled(Mat mat,PetscBool  *assembled)
5611: {
5615:   *assembled = mat->assembled;
5616:   return(0);
5617: }

5619: /*@
5620:    MatAssemblyEnd - Completes assembling the matrix.  This routine should
5621:    be called after MatAssemblyBegin().

5623:    Collective on Mat

5625:    Input Parameters:
5626: +  mat - the matrix
5627: -  type - type of assembly, either MAT_FLUSH_ASSEMBLY or MAT_FINAL_ASSEMBLY

5629:    Options Database Keys:
5630: +  -mat_view ::ascii_info - Prints info on matrix at conclusion of MatEndAssembly()
5631: .  -mat_view ::ascii_info_detail - Prints more detailed info
5632: .  -mat_view - Prints matrix in ASCII format
5633: .  -mat_view ::ascii_matlab - Prints matrix in Matlab format
5634: .  -mat_view draw - PetscDraws nonzero structure of matrix, using MatView() and PetscDrawOpenX().
5635: .  -display <name> - Sets display name (default is host)
5636: .  -draw_pause <sec> - Sets number of seconds to pause after display
5637: .  -mat_view socket - Sends matrix to socket, can be accessed from Matlab (See Users-Manual: ch_matlab)
5638: .  -viewer_socket_machine <machine> - Machine to use for socket
5639: .  -viewer_socket_port <port> - Port number to use for socket
5640: -  -mat_view binary:filename[:append] - Save matrix to file in binary format

5642:    Notes:
5643:    MatSetValues() generally caches the values.  The matrix is ready to
5644:    use only after MatAssemblyBegin() and MatAssemblyEnd() have been called.
5645:    Use MAT_FLUSH_ASSEMBLY when switching between ADD_VALUES and INSERT_VALUES
5646:    in MatSetValues(); use MAT_FINAL_ASSEMBLY for the final assembly before
5647:    using the matrix.

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

5653:    Level: beginner

5655: .seealso: MatAssemblyBegin(), MatSetValues(), PetscDrawOpenX(), PetscDrawCreate(), MatView(), MatAssembled(), PetscViewerSocketOpen()
5656: @*/
5657: PetscErrorCode MatAssemblyEnd(Mat mat,MatAssemblyType type)
5658: {
5659:   PetscErrorCode  ierr;
5660:   static PetscInt inassm = 0;
5661:   PetscBool       flg    = PETSC_FALSE;


5667:   inassm++;
5668:   MatAssemblyEnd_InUse++;
5669:   if (MatAssemblyEnd_InUse == 1) { /* Do the logging only the first time through */
5670:     PetscLogEventBegin(MAT_AssemblyEnd,mat,0,0,0);
5671:     if (mat->ops->assemblyend) {
5672:       (*mat->ops->assemblyend)(mat,type);
5673:     }
5674:     PetscLogEventEnd(MAT_AssemblyEnd,mat,0,0,0);
5675:   } else if (mat->ops->assemblyend) {
5676:     (*mat->ops->assemblyend)(mat,type);
5677:   }

5679:   /* Flush assembly is not a true assembly */
5680:   if (type != MAT_FLUSH_ASSEMBLY) {
5681:     mat->num_ass++;
5682:     mat->assembled        = PETSC_TRUE;
5683:     mat->ass_nonzerostate = mat->nonzerostate;
5684:   }

5686:   mat->insertmode = NOT_SET_VALUES;
5687:   MatAssemblyEnd_InUse--;
5688:   PetscObjectStateIncrease((PetscObject)mat);
5689:   if (!mat->symmetric_eternal) {
5690:     mat->symmetric_set              = PETSC_FALSE;
5691:     mat->hermitian_set              = PETSC_FALSE;
5692:     mat->structurally_symmetric_set = PETSC_FALSE;
5693:   }
5694:   if (inassm == 1 && type != MAT_FLUSH_ASSEMBLY) {
5695:     MatViewFromOptions(mat,NULL,"-mat_view");

5697:     if (mat->checksymmetryonassembly) {
5698:       MatIsSymmetric(mat,mat->checksymmetrytol,&flg);
5699:       if (flg) {
5700:         PetscPrintf(PetscObjectComm((PetscObject)mat),"Matrix is symmetric (tolerance %g)\n",(double)mat->checksymmetrytol);
5701:       } else {
5702:         PetscPrintf(PetscObjectComm((PetscObject)mat),"Matrix is not symmetric (tolerance %g)\n",(double)mat->checksymmetrytol);
5703:       }
5704:     }
5705:     if (mat->nullsp && mat->checknullspaceonassembly) {
5706:       MatNullSpaceTest(mat->nullsp,mat,NULL);
5707:     }
5708:   }
5709:   inassm--;
5710:   return(0);
5711: }

5713: /*@
5714:    MatSetOption - Sets a parameter option for a matrix. Some options
5715:    may be specific to certain storage formats.  Some options
5716:    determine how values will be inserted (or added). Sorted,
5717:    row-oriented input will generally assemble the fastest. The default
5718:    is row-oriented.

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

5722:    Input Parameters:
5723: +  mat - the matrix
5724: .  option - the option, one of those listed below (and possibly others),
5725: -  flg - turn the option on (PETSC_TRUE) or off (PETSC_FALSE)

5727:   Options Describing Matrix Structure:
5728: +    MAT_SPD - symmetric positive definite
5729: .    MAT_SYMMETRIC - symmetric in terms of both structure and value
5730: .    MAT_HERMITIAN - transpose is the complex conjugation
5731: .    MAT_STRUCTURALLY_SYMMETRIC - symmetric nonzero structure
5732: -    MAT_SYMMETRY_ETERNAL - if you would like the symmetry/Hermitian flag
5733:                             you set to be kept with all future use of the matrix
5734:                             including after MatAssemblyBegin/End() which could
5735:                             potentially change the symmetry structure, i.e. you
5736:                             KNOW the matrix will ALWAYS have the property you set.
5737:                             Note that setting this flag alone implies nothing about whether the matrix is symmetric/Hermitian;
5738:                             the relevant flags must be set independently.

5740:    Options For Use with MatSetValues():
5741:    Insert a logically dense subblock, which can be
5742: .    MAT_ROW_ORIENTED - row-oriented (default)

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

5748:    When (re)assembling a matrix, we can restrict the input for
5749:    efficiency/debugging purposes.  These options include
5750: +    MAT_NEW_NONZERO_LOCATIONS - additional insertions will be allowed if they generate a new nonzero (slow)
5751: .    MAT_FORCE_DIAGONAL_ENTRIES - forces diagonal entries to be allocated
5752: .    MAT_IGNORE_OFF_PROC_ENTRIES - drops off-processor entries
5753: .    MAT_NEW_NONZERO_LOCATION_ERR - generates an error for new matrix entry
5754: .    MAT_USE_HASH_TABLE - uses a hash table to speed up matrix assembly
5755: .    MAT_NO_OFF_PROC_ENTRIES - you know each process will only set values for its own rows, will generate an error if
5756:         any process sets values for another process. This avoids all reductions in the MatAssembly routines and thus improves
5757:         performance for very large process counts.
5758: -    MAT_SUBSET_OFF_PROC_ENTRIES - you know that the first assembly after setting this flag will set a superset
5759:         of the off-process entries required for all subsequent assemblies. This avoids a rendezvous step in the MatAssembly
5760:         functions, instead sending only neighbor messages.

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

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

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

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

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

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

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

5796:    MAT_USE_HASH_TABLE indicates that a hash table be used to improve the
5797:    searches during matrix assembly. When this flag is set, the hash table
5798:    is created during the first Matrix Assembly. This hash table is
5799:    used the next time through, during MatSetVaules()/MatSetVaulesBlocked()
5800:    to improve the searching of indices. MAT_NEW_NONZERO_LOCATIONS flag
5801:    should be used with MAT_USE_HASH_TABLE flag. This option is currently
5802:    supported by MATMPIBAIJ format only.

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

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

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

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

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

5818:    MAT_SORTED_FULL - each process provides exactly its local rows; all column indices for a given row are passed in a
5819:                      single call to MatSetValues(), preallocation is perfect, row oriented, INSERT_VALUES is used. Common
5820:                      with finite difference schemes with non-periodic boundary conditions.

5822:    Level: intermediate

5824: .seealso:  MatOption, Mat

5826: @*/
5827: PetscErrorCode MatSetOption(Mat mat,MatOption op,PetscBool flg)
5828: {

5833:   if (op > 0) {
5836:   }

5838:   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);

5840:   switch (op) {
5841:   case MAT_FORCE_DIAGONAL_ENTRIES:
5842:     mat->force_diagonals = flg;
5843:     return(0);
5844:   case MAT_NO_OFF_PROC_ENTRIES:
5845:     mat->nooffprocentries = flg;
5846:     return(0);
5847:   case MAT_SUBSET_OFF_PROC_ENTRIES:
5848:     mat->assembly_subset = flg;
5849:     if (!mat->assembly_subset) { /* See the same logic in VecAssembly wrt VEC_SUBSET_OFF_PROC_ENTRIES */
5850: #if !defined(PETSC_HAVE_MPIUNI)
5851:       MatStashScatterDestroy_BTS(&mat->stash);
5852: #endif
5853:       mat->stash.first_assembly_done = PETSC_FALSE;
5854:     }
5855:     return(0);
5856:   case MAT_NO_OFF_PROC_ZERO_ROWS:
5857:     mat->nooffproczerorows = flg;
5858:     return(0);
5859:   case MAT_SPD:
5860:     mat->spd_set = PETSC_TRUE;
5861:     mat->spd     = flg;
5862:     if (flg) {
5863:       mat->symmetric                  = PETSC_TRUE;
5864:       mat->structurally_symmetric     = PETSC_TRUE;
5865:       mat->symmetric_set              = PETSC_TRUE;
5866:       mat->structurally_symmetric_set = PETSC_TRUE;
5867:     }
5868:     break;
5869:   case MAT_SYMMETRIC:
5870:     mat->symmetric = flg;
5871:     if (flg) mat->structurally_symmetric = PETSC_TRUE;
5872:     mat->symmetric_set              = PETSC_TRUE;
5873:     mat->structurally_symmetric_set = flg;
5874: #if !defined(PETSC_USE_COMPLEX)
5875:     mat->hermitian     = flg;
5876:     mat->hermitian_set = PETSC_TRUE;
5877: #endif
5878:     break;
5879:   case MAT_HERMITIAN:
5880:     mat->hermitian = flg;
5881:     if (flg) mat->structurally_symmetric = PETSC_TRUE;
5882:     mat->hermitian_set              = PETSC_TRUE;
5883:     mat->structurally_symmetric_set = flg;
5884: #if !defined(PETSC_USE_COMPLEX)
5885:     mat->symmetric     = flg;
5886:     mat->symmetric_set = PETSC_TRUE;
5887: #endif
5888:     break;
5889:   case MAT_STRUCTURALLY_SYMMETRIC:
5890:     mat->structurally_symmetric     = flg;
5891:     mat->structurally_symmetric_set = PETSC_TRUE;
5892:     break;
5893:   case MAT_SYMMETRY_ETERNAL:
5894:     mat->symmetric_eternal = flg;
5895:     break;
5896:   case MAT_STRUCTURE_ONLY:
5897:     mat->structure_only = flg;
5898:     break;
5899:   case MAT_SORTED_FULL:
5900:     mat->sortedfull = flg;
5901:     break;
5902:   default:
5903:     break;
5904:   }
5905:   if (mat->ops->setoption) {
5906:     (*mat->ops->setoption)(mat,op,flg);
5907:   }
5908:   return(0);
5909: }

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

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

5916:    Input Parameters:
5917: +  mat - the matrix
5918: -  option - the option, this only responds to certain options, check the code for which ones

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

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

5926:    Level: intermediate

5928: .seealso:  MatOption, MatSetOption()

5930: @*/
5931: PetscErrorCode MatGetOption(Mat mat,MatOption op,PetscBool *flg)
5932: {

5937:   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);
5938:   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()");

5940:   switch (op) {
5941:   case MAT_NO_OFF_PROC_ENTRIES:
5942:     *flg = mat->nooffprocentries;
5943:     break;
5944:   case MAT_NO_OFF_PROC_ZERO_ROWS:
5945:     *flg = mat->nooffproczerorows;
5946:     break;
5947:   case MAT_SYMMETRIC:
5948:     *flg = mat->symmetric;
5949:     break;
5950:   case MAT_HERMITIAN:
5951:     *flg = mat->hermitian;
5952:     break;
5953:   case MAT_STRUCTURALLY_SYMMETRIC:
5954:     *flg = mat->structurally_symmetric;
5955:     break;
5956:   case MAT_SYMMETRY_ETERNAL:
5957:     *flg = mat->symmetric_eternal;
5958:     break;
5959:   case MAT_SPD:
5960:     *flg = mat->spd;
5961:     break;
5962:   default:
5963:     break;
5964:   }
5965:   return(0);
5966: }

5968: /*@
5969:    MatZeroEntries - Zeros all entries of a matrix.  For sparse matrices
5970:    this routine retains the old nonzero structure.

5972:    Logically Collective on Mat

5974:    Input Parameters:
5975: .  mat - the matrix

5977:    Level: intermediate

5979:    Notes:
5980:     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.
5981:    See the Performance chapter of the users manual for information on preallocating matrices.

5983: .seealso: MatZeroRows()
5984: @*/
5985: PetscErrorCode MatZeroEntries(Mat mat)
5986: {

5992:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
5993:   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");
5994:   if (!mat->ops->zeroentries) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
5995:   MatCheckPreallocated(mat,1);

5997:   PetscLogEventBegin(MAT_ZeroEntries,mat,0,0,0);
5998:   (*mat->ops->zeroentries)(mat);
5999:   PetscLogEventEnd(MAT_ZeroEntries,mat,0,0,0);
6000:   PetscObjectStateIncrease((PetscObject)mat);
6001:   return(0);
6002: }

6004: /*@
6005:    MatZeroRowsColumns - Zeros all entries (except possibly the main diagonal)
6006:    of a set of rows and columns of a matrix.

6008:    Collective on Mat

6010:    Input Parameters:
6011: +  mat - the matrix
6012: .  numRows - the number of rows to remove
6013: .  rows - the global row indices
6014: .  diag - value put in all diagonals of eliminated rows (0.0 will even eliminate diagonal entry)
6015: .  x - optional vector of solutions for zeroed rows (other entries in vector are not used)
6016: -  b - optional vector of right hand side, that will be adjusted by provided solution

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

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

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

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

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

6035:    Level: intermediate

6037: .seealso: MatZeroRowsIS(), MatZeroRows(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(),
6038:           MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil()
6039: @*/
6040: PetscErrorCode MatZeroRowsColumns(Mat mat,PetscInt numRows,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
6041: {

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

6053:   (*mat->ops->zerorowscolumns)(mat,numRows,rows,diag,x,b);
6054:   MatViewFromOptions(mat,NULL,"-mat_view");
6055:   PetscObjectStateIncrease((PetscObject)mat);
6056:   return(0);
6057: }

6059: /*@
6060:    MatZeroRowsColumnsIS - Zeros all entries (except possibly the main diagonal)
6061:    of a set of rows and columns of a matrix.

6063:    Collective on Mat

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

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

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

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

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

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

6089:    Level: intermediate

6091: .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(),
6092:           MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRows(), MatZeroRowsColumnsStencil()
6093: @*/
6094: PetscErrorCode MatZeroRowsColumnsIS(Mat mat,IS is,PetscScalar diag,Vec x,Vec b)
6095: {
6097:   PetscInt       numRows;
6098:   const PetscInt *rows;

6105:   ISGetLocalSize(is,&numRows);
6106:   ISGetIndices(is,&rows);
6107:   MatZeroRowsColumns(mat,numRows,rows,diag,x,b);
6108:   ISRestoreIndices(is,&rows);
6109:   return(0);
6110: }

6112: /*@
6113:    MatZeroRows - Zeros all entries (except possibly the main diagonal)
6114:    of a set of rows of a matrix.

6116:    Collective on Mat

6118:    Input Parameters:
6119: +  mat - the matrix
6120: .  numRows - the number of rows to remove
6121: .  rows - the global row indices
6122: .  diag - value put in all diagonals of eliminated rows (0.0 will even eliminate diagonal entry)
6123: .  x - optional vector of solutions for zeroed rows (other entries in vector are not used)
6124: -  b - optional vector of right hand side, that will be adjusted by provided solution

6126:    Notes:
6127:    For the AIJ and BAIJ matrix formats this removes the old nonzero structure,
6128:    but does not release memory.  For the dense and block diagonal
6129:    formats this does not alter the nonzero structure.

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

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

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

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

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

6150:    Level: intermediate

6152: .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(),
6153:           MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil()
6154: @*/
6155: PetscErrorCode MatZeroRows(Mat mat,PetscInt numRows,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
6156: {

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

6168:   (*mat->ops->zerorows)(mat,numRows,rows,diag,x,b);
6169:   MatViewFromOptions(mat,NULL,"-mat_view");
6170:   PetscObjectStateIncrease((PetscObject)mat);
6171:   return(0);
6172: }

6174: /*@
6175:    MatZeroRowsIS - Zeros all entries (except possibly the main diagonal)
6176:    of a set of rows of a matrix.

6178:    Collective on Mat

6180:    Input Parameters:
6181: +  mat - the matrix
6182: .  is - index set of rows to remove (if NULL then no row is removed)
6183: .  diag - value put in all diagonals of eliminated rows
6184: .  x - optional vector of solutions for zeroed rows (other entries in vector are not used)
6185: -  b - optional vector of right hand side, that will be adjusted by provided solution

6187:    Notes:
6188:    For the AIJ and BAIJ matrix formats this removes the old nonzero structure,
6189:    but does not release memory.  For the dense and block diagonal
6190:    formats this does not alter the nonzero structure.

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

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

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

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

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

6211:    Level: intermediate

6213: .seealso: MatZeroRows(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(),
6214:           MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil()
6215: @*/
6216: PetscErrorCode MatZeroRowsIS(Mat mat,IS is,PetscScalar diag,Vec x,Vec b)
6217: {
6218:   PetscInt       numRows = 0;
6219:   const PetscInt *rows = NULL;

6225:   if (is) {
6227:     ISGetLocalSize(is,&numRows);
6228:     ISGetIndices(is,&rows);
6229:   }
6230:   MatZeroRows(mat,numRows,rows,diag,x,b);
6231:   if (is) {
6232:     ISRestoreIndices(is,&rows);
6233:   }
6234:   return(0);
6235: }

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

6241:    Collective on Mat

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

6251:    Notes:
6252:    For the AIJ and BAIJ matrix formats this removes the old nonzero structure,
6253:    but does not release memory.  For the dense and block diagonal
6254:    formats this does not alter the nonzero structure.

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

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

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

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

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

6274:    In Fortran idxm and idxn should be declared as
6275: $     MatStencil idxm(4,m)
6276:    and the values inserted using
6277: $    idxm(MatStencil_i,1) = i
6278: $    idxm(MatStencil_j,1) = j
6279: $    idxm(MatStencil_k,1) = k
6280: $    idxm(MatStencil_c,1) = c
6281:    etc

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

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

6291:    Level: intermediate

6293: .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsl(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(),
6294:           MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil()
6295: @*/
6296: PetscErrorCode MatZeroRowsStencil(Mat mat,PetscInt numRows,const MatStencil rows[],PetscScalar diag,Vec x,Vec b)
6297: {
6298:   PetscInt       dim     = mat->stencil.dim;
6299:   PetscInt       sdim    = dim - (1 - (PetscInt) mat->stencil.noc);
6300:   PetscInt       *dims   = mat->stencil.dims+1;
6301:   PetscInt       *starts = mat->stencil.starts;
6302:   PetscInt       *dxm    = (PetscInt*) rows;
6303:   PetscInt       *jdxm, i, j, tmp, numNewRows = 0;


6311:   PetscMalloc1(numRows, &jdxm);
6312:   for (i = 0; i < numRows; ++i) {
6313:     /* Skip unused dimensions (they are ordered k, j, i, c) */
6314:     for (j = 0; j < 3-sdim; ++j) dxm++;
6315:     /* Local index in X dir */
6316:     tmp = *dxm++ - starts[0];
6317:     /* Loop over remaining dimensions */
6318:     for (j = 0; j < dim-1; ++j) {
6319:       /* If nonlocal, set index to be negative */
6320:       if ((*dxm++ - starts[j+1]) < 0 || tmp < 0) tmp = PETSC_MIN_INT;
6321:       /* Update local index */
6322:       else tmp = tmp*dims[j] + *(dxm-1) - starts[j+1];
6323:     }
6324:     /* Skip component slot if necessary */
6325:     if (mat->stencil.noc) dxm++;
6326:     /* Local row number */
6327:     if (tmp >= 0) {
6328:       jdxm[numNewRows++] = tmp;
6329:     }
6330:   }
6331:   MatZeroRowsLocal(mat,numNewRows,jdxm,diag,x,b);
6332:   PetscFree(jdxm);
6333:   return(0);
6334: }

6336: /*@
6337:    MatZeroRowsColumnsStencil - Zeros all row and column entries (except possibly the main diagonal)
6338:    of a set of rows and columns of a matrix.

6340:    Collective on Mat

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

6350:    Notes:
6351:    For the AIJ and BAIJ matrix formats this removes the old nonzero structure,
6352:    but does not release memory.  For the dense and block diagonal
6353:    formats this does not alter the nonzero structure.

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

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

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

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

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

6373:    In Fortran idxm and idxn should be declared as
6374: $     MatStencil idxm(4,m)
6375:    and the values inserted using
6376: $    idxm(MatStencil_i,1) = i
6377: $    idxm(MatStencil_j,1) = j
6378: $    idxm(MatStencil_k,1) = k
6379: $    idxm(MatStencil_c,1) = c
6380:    etc

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

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

6390:    Level: intermediate

6392: .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(),
6393:           MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRows()
6394: @*/
6395: PetscErrorCode MatZeroRowsColumnsStencil(Mat mat,PetscInt numRows,const MatStencil rows[],PetscScalar diag,Vec x,Vec b)
6396: {
6397:   PetscInt       dim     = mat->stencil.dim;
6398:   PetscInt       sdim    = dim - (1 - (PetscInt) mat->stencil.noc);
6399:   PetscInt       *dims   = mat->stencil.dims+1;
6400:   PetscInt       *starts = mat->stencil.starts;
6401:   PetscInt       *dxm    = (PetscInt*) rows;
6402:   PetscInt       *jdxm, i, j, tmp, numNewRows = 0;


6410:   PetscMalloc1(numRows, &jdxm);
6411:   for (i = 0; i < numRows; ++i) {
6412:     /* Skip unused dimensions (they are ordered k, j, i, c) */
6413:     for (j = 0; j < 3-sdim; ++j) dxm++;
6414:     /* Local index in X dir */
6415:     tmp = *dxm++ - starts[0];
6416:     /* Loop over remaining dimensions */
6417:     for (j = 0; j < dim-1; ++j) {
6418:       /* If nonlocal, set index to be negative */
6419:       if ((*dxm++ - starts[j+1]) < 0 || tmp < 0) tmp = PETSC_MIN_INT;
6420:       /* Update local index */
6421:       else tmp = tmp*dims[j] + *(dxm-1) - starts[j+1];
6422:     }
6423:     /* Skip component slot if necessary */
6424:     if (mat->stencil.noc) dxm++;
6425:     /* Local row number */
6426:     if (tmp >= 0) {
6427:       jdxm[numNewRows++] = tmp;
6428:     }
6429:   }
6430:   MatZeroRowsColumnsLocal(mat,numNewRows,jdxm,diag,x,b);
6431:   PetscFree(jdxm);
6432:   return(0);
6433: }

6435: /*@C
6436:    MatZeroRowsLocal - Zeros all entries (except possibly the main diagonal)
6437:    of a set of rows of a matrix; using local numbering of rows.

6439:    Collective on Mat

6441:    Input Parameters:
6442: +  mat - the matrix
6443: .  numRows - the number of rows to remove
6444: .  rows - the local row indices
6445: .  diag - value put in all diagonals of eliminated rows
6446: .  x - optional vector of solutions for zeroed rows (other entries in vector are not used)
6447: -  b - optional vector of right hand side, that will be adjusted by provided solution

6449:    Notes:
6450:    Before calling MatZeroRowsLocal(), the user must first set the
6451:    local-to-global mapping by calling MatSetLocalToGlobalMapping().

6453:    For the AIJ matrix formats this removes the old nonzero structure,
6454:    but does not release memory.  For the dense and block diagonal
6455:    formats this does not alter the nonzero structure.

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

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

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

6468:    Level: intermediate

6470: .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRows(), MatSetOption(),
6471:           MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil()
6472: @*/
6473: PetscErrorCode MatZeroRowsLocal(Mat mat,PetscInt numRows,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
6474: {

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

6485:   if (mat->ops->zerorowslocal) {
6486:     (*mat->ops->zerorowslocal)(mat,numRows,rows,diag,x,b);
6487:   } else {
6488:     IS             is, newis;
6489:     const PetscInt *newRows;

6491:     if (!mat->rmap->mapping) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Need to provide local to global mapping to matrix first");
6492:     ISCreateGeneral(PETSC_COMM_SELF,numRows,rows,PETSC_COPY_VALUES,&is);
6493:     ISLocalToGlobalMappingApplyIS(mat->rmap->mapping,is,&newis);
6494:     ISGetIndices(newis,&newRows);
6495:     (*mat->ops->zerorows)(mat,numRows,newRows,diag,x,b);
6496:     ISRestoreIndices(newis,&newRows);
6497:     ISDestroy(&newis);
6498:     ISDestroy(&is);
6499:   }
6500:   PetscObjectStateIncrease((PetscObject)mat);
6501:   return(0);
6502: }

6504: /*@
6505:    MatZeroRowsLocalIS - Zeros all entries (except possibly the main diagonal)
6506:    of a set of rows of a matrix; using local numbering of rows.

6508:    Collective on Mat

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

6517:    Notes:
6518:    Before calling MatZeroRowsLocalIS(), the user must first set the
6519:    local-to-global mapping by calling MatSetLocalToGlobalMapping().

6521:    For the AIJ matrix formats this removes the old nonzero structure,
6522:    but does not release memory.  For the dense and block diagonal
6523:    formats this does not alter the nonzero structure.

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

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

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

6536:    Level: intermediate

6538: .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRows(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(),
6539:           MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil()
6540: @*/
6541: PetscErrorCode MatZeroRowsLocalIS(Mat mat,IS is,PetscScalar diag,Vec x,Vec b)
6542: {
6544:   PetscInt       numRows;
6545:   const PetscInt *rows;

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

6555:   ISGetLocalSize(is,&numRows);
6556:   ISGetIndices(is,&rows);
6557:   MatZeroRowsLocal(mat,numRows,rows,diag,x,b);
6558:   ISRestoreIndices(is,&rows);
6559:   return(0);
6560: }

6562: /*@
6563:    MatZeroRowsColumnsLocal - Zeros all entries (except possibly the main diagonal)
6564:    of a set of rows and columns of a matrix; using local numbering of rows.

6566:    Collective on Mat

6568:    Input Parameters:
6569: +  mat - the matrix
6570: .  numRows - the number of rows to remove
6571: .  rows - the global row indices
6572: .  diag - value put in all diagonals of eliminated rows
6573: .  x - optional vector of solutions for zeroed rows (other entries in vector are not used)
6574: -  b - optional vector of right hand side, that will be adjusted by provided solution

6576:    Notes:
6577:    Before calling MatZeroRowsColumnsLocal(), the user must first set the
6578:    local-to-global mapping by calling MatSetLocalToGlobalMapping().

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

6584:    Level: intermediate

6586: .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(),
6587:           MatZeroRows(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil()
6588: @*/
6589: PetscErrorCode MatZeroRowsColumnsLocal(Mat mat,PetscInt numRows,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
6590: {
6592:   IS             is, newis;
6593:   const PetscInt *newRows;

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

6603:   if (!mat->cmap->mapping) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Need to provide local to global mapping to matrix first");
6604:   ISCreateGeneral(PETSC_COMM_SELF,numRows,rows,PETSC_COPY_VALUES,&is);
6605:   ISLocalToGlobalMappingApplyIS(mat->cmap->mapping,is,&newis);
6606:   ISGetIndices(newis,&newRows);
6607:   (*mat->ops->zerorowscolumns)(mat,numRows,newRows,diag,x,b);
6608:   ISRestoreIndices(newis,&newRows);
6609:   ISDestroy(&newis);
6610:   ISDestroy(&is);
6611:   PetscObjectStateIncrease((PetscObject)mat);
6612:   return(0);
6613: }

6615: /*@
6616:    MatZeroRowsColumnsLocalIS - Zeros all entries (except possibly the main diagonal)
6617:    of a set of rows and columns of a matrix; using local numbering of rows.

6619:    Collective on Mat

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

6628:    Notes:
6629:    Before calling MatZeroRowsColumnsLocalIS(), the user must first set the
6630:    local-to-global mapping by calling MatSetLocalToGlobalMapping().

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

6636:    Level: intermediate

6638: .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(),
6639:           MatZeroRowsColumnsLocal(), MatZeroRows(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil()
6640: @*/
6641: PetscErrorCode MatZeroRowsColumnsLocalIS(Mat mat,IS is,PetscScalar diag,Vec x,Vec b)
6642: {
6644:   PetscInt       numRows;
6645:   const PetscInt *rows;

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

6655:   ISGetLocalSize(is,&numRows);
6656:   ISGetIndices(is,&rows);
6657:   MatZeroRowsColumnsLocal(mat,numRows,rows,diag,x,b);
6658:   ISRestoreIndices(is,&rows);
6659:   return(0);
6660: }

6662: /*@C
6663:    MatGetSize - Returns the numbers of rows and columns in a matrix.

6665:    Not Collective

6667:    Input Parameter:
6668: .  mat - the matrix

6670:    Output Parameters:
6671: +  m - the number of global rows
6672: -  n - the number of global columns

6674:    Note: both output parameters can be NULL on input.

6676:    Level: beginner

6678: .seealso: MatGetLocalSize()
6679: @*/
6680: PetscErrorCode MatGetSize(Mat mat,PetscInt *m,PetscInt *n)
6681: {
6684:   if (m) *m = mat->rmap->N;
6685:   if (n) *n = mat->cmap->N;
6686:   return(0);
6687: }

6689: /*@C
6690:    MatGetLocalSize - Returns the number of local rows and local columns
6691:    of a matrix, that is the local size of the left and right vectors as returned by MatCreateVecs().

6693:    Not Collective

6695:    Input Parameter:
6696: .  mat - the matrix

6698:    Output Parameters:
6699: +  m - the number of local rows
6700: -  n - the number of local columns

6702:    Note: both output parameters can be NULL on input.

6704:    Level: beginner

6706: .seealso: MatGetSize()
6707: @*/
6708: PetscErrorCode MatGetLocalSize(Mat mat,PetscInt *m,PetscInt *n)
6709: {
6714:   if (m) *m = mat->rmap->n;
6715:   if (n) *n = mat->cmap->n;
6716:   return(0);
6717: }

6719: /*@C
6720:    MatGetOwnershipRangeColumn - Returns the range of matrix columns associated with rows of a vector one multiplies by that owned by
6721:    this processor. (The columns of the "diagonal block")

6723:    Not Collective, unless matrix has not been allocated, then collective on Mat

6725:    Input Parameter:
6726: .  mat - the matrix

6728:    Output Parameters:
6729: +  m - the global index of the first local column
6730: -  n - one more than the global index of the last local column

6732:    Notes:
6733:     both output parameters can be NULL on input.

6735:    Level: developer

6737: .seealso:  MatGetOwnershipRange(), MatGetOwnershipRanges(), MatGetOwnershipRangesColumn()

6739: @*/
6740: PetscErrorCode MatGetOwnershipRangeColumn(Mat mat,PetscInt *m,PetscInt *n)
6741: {
6747:   MatCheckPreallocated(mat,1);
6748:   if (m) *m = mat->cmap->rstart;
6749:   if (n) *n = mat->cmap->rend;
6750:   return(0);
6751: }

6753: /*@C
6754:    MatGetOwnershipRange - Returns the range of matrix rows owned by
6755:    this processor, assuming that the matrix is laid out with the first
6756:    n1 rows on the first processor, the next n2 rows on the second, etc.
6757:    For certain parallel layouts this range may not be well defined.

6759:    Not Collective

6761:    Input Parameter:
6762: .  mat - the matrix

6764:    Output Parameters:
6765: +  m - the global index of the first local row
6766: -  n - one more than the global index of the last local row

6768:    Note: Both output parameters can be NULL on input.
6769: $  This function requires that the matrix be preallocated. If you have not preallocated, consider using
6770: $    PetscSplitOwnership(MPI_Comm comm, PetscInt *n, PetscInt *N)
6771: $  and then MPI_Scan() to calculate prefix sums of the local sizes.

6773:    Level: beginner

6775: .seealso:   MatGetOwnershipRanges(), MatGetOwnershipRangeColumn(), MatGetOwnershipRangesColumn(), PetscSplitOwnership(), PetscSplitOwnershipBlock()

6777: @*/
6778: PetscErrorCode MatGetOwnershipRange(Mat mat,PetscInt *m,PetscInt *n)
6779: {
6785:   MatCheckPreallocated(mat,1);
6786:   if (m) *m = mat->rmap->rstart;
6787:   if (n) *n = mat->rmap->rend;
6788:   return(0);
6789: }

6791: /*@C
6792:    MatGetOwnershipRanges - Returns the range of matrix rows owned by
6793:    each process

6795:    Not Collective, unless matrix has not been allocated, then collective on Mat

6797:    Input Parameters:
6798: .  mat - the matrix

6800:    Output Parameters:
6801: .  ranges - start of each processors portion plus one more than the total length at the end

6803:    Level: beginner

6805: .seealso:   MatGetOwnershipRange(), MatGetOwnershipRangeColumn(), MatGetOwnershipRangesColumn()

6807: @*/
6808: PetscErrorCode MatGetOwnershipRanges(Mat mat,const PetscInt **ranges)
6809: {

6815:   MatCheckPreallocated(mat,1);
6816:   PetscLayoutGetRanges(mat->rmap,ranges);
6817:   return(0);
6818: }

6820: /*@C
6821:    MatGetOwnershipRangesColumn - Returns the range of matrix columns associated with rows of a vector one multiplies by that owned by
6822:    this processor. (The columns of the "diagonal blocks" for each process)

6824:    Not Collective, unless matrix has not been allocated, then collective on Mat

6826:    Input Parameters:
6827: .  mat - the matrix

6829:    Output Parameters:
6830: .  ranges - start of each processors portion plus one more then the total length at the end

6832:    Level: beginner

6834: .seealso:   MatGetOwnershipRange(), MatGetOwnershipRangeColumn(), MatGetOwnershipRanges()

6836: @*/
6837: PetscErrorCode MatGetOwnershipRangesColumn(Mat mat,const PetscInt **ranges)
6838: {

6844:   MatCheckPreallocated(mat,1);
6845:   PetscLayoutGetRanges(mat->cmap,ranges);
6846:   return(0);
6847: }

6849: /*@C
6850:    MatGetOwnershipIS - Get row and column ownership as index sets

6852:    Not Collective

6854:    Input Parameter:
6855: .  A - matrix

6857:    Output Parameters:
6858: +  rows - rows in which this process owns elements
6859: -  cols - columns in which this process owns elements

6861:    Level: intermediate

6863: .seealso: MatGetOwnershipRange(), MatGetOwnershipRangeColumn(), MatSetValues(), MATELEMENTAL, MATSCALAPACK
6864: @*/
6865: PetscErrorCode MatGetOwnershipIS(Mat A,IS *rows,IS *cols)
6866: {
6867:   PetscErrorCode ierr,(*f)(Mat,IS*,IS*);

6870:   MatCheckPreallocated(A,1);
6871:   PetscObjectQueryFunction((PetscObject)A,"MatGetOwnershipIS_C",&f);
6872:   if (f) {
6873:     (*f)(A,rows,cols);
6874:   } else {   /* Create a standard row-based partition, each process is responsible for ALL columns in their row block */
6875:     if (rows) {ISCreateStride(PETSC_COMM_SELF,A->rmap->n,A->rmap->rstart,1,rows);}
6876:     if (cols) {ISCreateStride(PETSC_COMM_SELF,A->cmap->N,0,1,cols);}
6877:   }
6878:   return(0);
6879: }

6881: /*@C
6882:    MatILUFactorSymbolic - Performs symbolic ILU factorization of a matrix.
6883:    Uses levels of fill only, not drop tolerance. Use MatLUFactorNumeric()
6884:    to complete the factorization.

6886:    Collective on Mat

6888:    Input Parameters:
6889: +  mat - the matrix
6890: .  row - row permutation
6891: .  column - column permutation
6892: -  info - structure containing
6893: $      levels - number of levels of fill.
6894: $      expected fill - as ratio of original fill.
6895: $      1 or 0 - indicating force fill on diagonal (improves robustness for matrices
6896:                 missing diagonal entries)

6898:    Output Parameters:
6899: .  fact - new matrix that has been symbolically factored

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

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

6908:    Level: developer

6910: .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor()
6911:           MatGetOrdering(), MatFactorInfo

6913:     Note: this uses the definition of level of fill as in Y. Saad, 2003

6915:     Developer Note: fortran interface is not autogenerated as the f90
6916:     interface definition cannot be generated correctly [due to MatFactorInfo]

6918:    References:
6919:      Y. Saad, Iterative methods for sparse linear systems Philadelphia: Society for Industrial and Applied Mathematics, 2003
6920: @*/
6921: PetscErrorCode MatILUFactorSymbolic(Mat fact,Mat mat,IS row,IS col,const MatFactorInfo *info)
6922: {

6932:   if (info->levels < 0) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"Levels of fill negative %D",(PetscInt)info->levels);
6933:   if (info->fill < 1.0) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"Expected fill less than 1.0 %g",(double)info->fill);
6934:   if (!fact->ops->ilufactorsymbolic) {
6935:     MatSolverType stype;
6936:     MatFactorGetSolverType(fact,&stype);
6937:     SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Matrix type %s symbolic ILU using solver type %s",((PetscObject)mat)->type_name,stype);
6938:   }
6939:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
6940:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
6941:   MatCheckPreallocated(mat,2);

6943:   if (!fact->trivialsymbolic) {PetscLogEventBegin(MAT_ILUFactorSymbolic,mat,row,col,0);}
6944:   (fact->ops->ilufactorsymbolic)(fact,mat,row,col,info);
6945:   if (!fact->trivialsymbolic) {PetscLogEventEnd(MAT_ILUFactorSymbolic,mat,row,col,0);}
6946:   return(0);
6947: }

6949: /*@C
6950:    MatICCFactorSymbolic - Performs symbolic incomplete
6951:    Cholesky factorization for a symmetric matrix.  Use
6952:    MatCholeskyFactorNumeric() to complete the factorization.

6954:    Collective on Mat

6956:    Input Parameters:
6957: +  mat - the matrix
6958: .  perm - row and column permutation
6959: -  info - structure containing
6960: $      levels - number of levels of fill.
6961: $      expected fill - as ratio of original fill.

6963:    Output Parameter:
6964: .  fact - the factored matrix

6966:    Notes:
6967:    Most users should employ the KSP interface for linear solvers
6968:    instead of working directly with matrix algebra routines such as this.
6969:    See, e.g., KSPCreate().

6971:    Level: developer

6973: .seealso: MatCholeskyFactorNumeric(), MatCholeskyFactor(), MatFactorInfo

6975:     Note: this uses the definition of level of fill as in Y. Saad, 2003

6977:     Developer Note: fortran interface is not autogenerated as the f90
6978:     interface definition cannot be generated correctly [due to MatFactorInfo]

6980:    References:
6981:      Y. Saad, Iterative methods for sparse linear systems Philadelphia: Society for Industrial and Applied Mathematics, 2003
6982: @*/
6983: PetscErrorCode MatICCFactorSymbolic(Mat fact,Mat mat,IS perm,const MatFactorInfo *info)
6984: {

6993:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
6994:   if (info->levels < 0) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"Levels negative %D",(PetscInt) info->levels);
6995:   if (info->fill < 1.0) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"Expected fill less than 1.0 %g",(double)info->fill);
6996:   if (!(fact)->ops->iccfactorsymbolic) {
6997:     MatSolverType stype;
6998:     MatFactorGetSolverType(fact,&stype);
6999:     SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Matrix type %s symbolic ICC using solver type %s",((PetscObject)mat)->type_name,stype);
7000:   }
7001:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
7002:   MatCheckPreallocated(mat,2);

7004:   if (!fact->trivialsymbolic) {PetscLogEventBegin(MAT_ICCFactorSymbolic,mat,perm,0,0);}
7005:   (fact->ops->iccfactorsymbolic)(fact,mat,perm,info);
7006:   if (!fact->trivialsymbolic) {PetscLogEventEnd(MAT_ICCFactorSymbolic,mat,perm,0,0);}
7007:   return(0);
7008: }

7010: /*@C
7011:    MatCreateSubMatrices - Extracts several submatrices from a matrix. If submat
7012:    points to an array of valid matrices, they may be reused to store the new
7013:    submatrices.

7015:    Collective on Mat

7017:    Input Parameters:
7018: +  mat - the matrix
7019: .  n   - the number of submatrixes to be extracted (on this processor, may be zero)
7020: .  irow, icol - index sets of rows and columns to extract
7021: -  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX

7023:    Output Parameter:
7024: .  submat - the array of submatrices

7026:    Notes:
7027:    MatCreateSubMatrices() can extract ONLY sequential submatrices
7028:    (from both sequential and parallel matrices). Use MatCreateSubMatrix()
7029:    to extract a parallel submatrix.

7031:    Some matrix types place restrictions on the row and column
7032:    indices, such as that they be sorted or that they be equal to each other.

7034:    The index sets may not have duplicate entries.

7036:    When extracting submatrices from a parallel matrix, each processor can
7037:    form a different submatrix by setting the rows and columns of its
7038:    individual index sets according to the local submatrix desired.

7040:    When finished using the submatrices, the user should destroy
7041:    them with MatDestroySubMatrices().

7043:    MAT_REUSE_MATRIX can only be used when the nonzero structure of the
7044:    original matrix has not changed from that last call to MatCreateSubMatrices().

7046:    This routine creates the matrices in submat; you should NOT create them before
7047:    calling it. It also allocates the array of matrix pointers submat.

7049:    For BAIJ matrices the index sets must respect the block structure, that is if they
7050:    request one row/column in a block, they must request all rows/columns that are in
7051:    that block. For example, if the block size is 2 you cannot request just row 0 and
7052:    column 0.

7054:    Fortran Note:
7055:    The Fortran interface is slightly different from that given below; it
7056:    requires one to pass in  as submat a Mat (integer) array of size at least n+1.

7058:    Level: advanced

7060: .seealso: MatDestroySubMatrices(), MatCreateSubMatrix(), MatGetRow(), MatGetDiagonal(), MatReuse
7061: @*/
7062: PetscErrorCode MatCreateSubMatrices(Mat mat,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *submat[])
7063: {
7065:   PetscInt       i;
7066:   PetscBool      eq;

7071:   if (n) {
7076:   }
7078:   if (n && scall == MAT_REUSE_MATRIX) {
7081:   }
7082:   if (!mat->ops->createsubmatrices) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
7083:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
7084:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
7085:   MatCheckPreallocated(mat,1);

7087:   PetscLogEventBegin(MAT_CreateSubMats,mat,0,0,0);
7088:   (*mat->ops->createsubmatrices)(mat,n,irow,icol,scall,submat);
7089:   PetscLogEventEnd(MAT_CreateSubMats,mat,0,0,0);
7090:   for (i=0; i<n; i++) {
7091:     (*submat)[i]->factortype = MAT_FACTOR_NONE;  /* in case in place factorization was previously done on submatrix */
7092:     ISEqualUnsorted(irow[i],icol[i],&eq);
7093:     if (eq) {
7094:       MatPropagateSymmetryOptions(mat,(*submat)[i]);
7095:     }
7096:   }
7097:   return(0);
7098: }

7100: /*@C
7101:    MatCreateSubMatricesMPI - Extracts MPI submatrices across a sub communicator of mat (by pairs of IS that may live on subcomms).

7103:    Collective on Mat

7105:    Input Parameters:
7106: +  mat - the matrix
7107: .  n   - the number of submatrixes to be extracted
7108: .  irow, icol - index sets of rows and columns to extract
7109: -  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX

7111:    Output Parameter:
7112: .  submat - the array of submatrices

7114:    Level: advanced

7116: .seealso: MatCreateSubMatrices(), MatCreateSubMatrix(), MatGetRow(), MatGetDiagonal(), MatReuse
7117: @*/
7118: PetscErrorCode MatCreateSubMatricesMPI(Mat mat,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *submat[])
7119: {
7121:   PetscInt       i;
7122:   PetscBool      eq;

7127:   if (n) {
7132:   }
7134:   if (n && scall == MAT_REUSE_MATRIX) {
7137:   }
7138:   if (!mat->ops->createsubmatricesmpi) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
7139:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
7140:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
7141:   MatCheckPreallocated(mat,1);

7143:   PetscLogEventBegin(MAT_CreateSubMats,mat,0,0,0);
7144:   (*mat->ops->createsubmatricesmpi)(mat,n,irow,icol,scall,submat);
7145:   PetscLogEventEnd(MAT_CreateSubMats,mat,0,0,0);
7146:   for (i=0; i<n; i++) {
7147:     ISEqualUnsorted(irow[i],icol[i],&eq);
7148:     if (eq) {
7149:       MatPropagateSymmetryOptions(mat,(*submat)[i]);
7150:     }
7151:   }
7152:   return(0);
7153: }

7155: /*@C
7156:    MatDestroyMatrices - Destroys an array of matrices.

7158:    Collective on Mat

7160:    Input Parameters:
7161: +  n - the number of local matrices
7162: -  mat - the matrices (note that this is a pointer to the array of matrices)

7164:    Level: advanced

7166:     Notes:
7167:     Frees not only the matrices, but also the array that contains the matrices
7168:            In Fortran will not free the array.

7170: .seealso: MatCreateSubMatrices() MatDestroySubMatrices()
7171: @*/
7172: PetscErrorCode MatDestroyMatrices(PetscInt n,Mat *mat[])
7173: {
7175:   PetscInt       i;

7178:   if (!*mat) return(0);
7179:   if (n < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Trying to destroy negative number of matrices %D",n);

7182:   for (i=0; i<n; i++) {
7183:     MatDestroy(&(*mat)[i]);
7184:   }

7186:   /* memory is allocated even if n = 0 */
7187:   PetscFree(*mat);
7188:   return(0);
7189: }

7191: /*@C
7192:    MatDestroySubMatrices - Destroys a set of matrices obtained with MatCreateSubMatrices().

7194:    Collective on Mat

7196:    Input Parameters:
7197: +  n - the number of local matrices
7198: -  mat - the matrices (note that this is a pointer to the array of matrices, just to match the calling
7199:                        sequence of MatCreateSubMatrices())

7201:    Level: advanced

7203:     Notes:
7204:     Frees not only the matrices, but also the array that contains the matrices
7205:            In Fortran will not free the array.

7207: .seealso: MatCreateSubMatrices()
7208: @*/
7209: PetscErrorCode MatDestroySubMatrices(PetscInt n,Mat *mat[])
7210: {
7212:   Mat            mat0;

7215:   if (!*mat) return(0);
7216:   /* mat[] is an array of length n+1, see MatCreateSubMatrices_xxx() */
7217:   if (n < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Trying to destroy negative number of matrices %D",n);

7220:   mat0 = (*mat)[0];
7221:   if (mat0 && mat0->ops->destroysubmatrices) {
7222:     (mat0->ops->destroysubmatrices)(n,mat);
7223:   } else {
7224:     MatDestroyMatrices(n,mat);
7225:   }
7226:   return(0);
7227: }

7229: /*@C
7230:    MatGetSeqNonzeroStructure - Extracts the sequential nonzero structure from a matrix.

7232:    Collective on Mat

7234:    Input Parameters:
7235: .  mat - the matrix

7237:    Output Parameter:
7238: .  matstruct - the sequential matrix with the nonzero structure of mat

7240:   Level: intermediate

7242: .seealso: MatDestroySeqNonzeroStructure(), MatCreateSubMatrices(), MatDestroyMatrices()
7243: @*/
7244: PetscErrorCode MatGetSeqNonzeroStructure(Mat mat,Mat *matstruct)
7245: {


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

7256:   if (!mat->ops->getseqnonzerostructure) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Not for matrix type %s\n",((PetscObject)mat)->type_name);
7257:   PetscLogEventBegin(MAT_GetSeqNonzeroStructure,mat,0,0,0);
7258:   (*mat->ops->getseqnonzerostructure)(mat,matstruct);
7259:   PetscLogEventEnd(MAT_GetSeqNonzeroStructure,mat,0,0,0);
7260:   return(0);
7261: }

7263: /*@C
7264:    MatDestroySeqNonzeroStructure - Destroys matrix obtained with MatGetSeqNonzeroStructure().

7266:    Collective on Mat

7268:    Input Parameters:
7269: .  mat - the matrix (note that this is a pointer to the array of matrices, just to match the calling
7270:                        sequence of MatGetSequentialNonzeroStructure())

7272:    Level: advanced

7274:     Notes:
7275:     Frees not only the matrices, but also the array that contains the matrices

7277: .seealso: MatGetSeqNonzeroStructure()
7278: @*/
7279: PetscErrorCode MatDestroySeqNonzeroStructure(Mat *mat)
7280: {

7285:   MatDestroy(mat);
7286:   return(0);
7287: }

7289: /*@
7290:    MatIncreaseOverlap - Given a set of submatrices indicated by index sets,
7291:    replaces the index sets by larger ones that represent submatrices with
7292:    additional overlap.

7294:    Collective on Mat

7296:    Input Parameters:
7297: +  mat - the matrix
7298: .  n   - the number of index sets
7299: .  is  - the array of index sets (these index sets will changed during the call)
7300: -  ov  - the additional overlap requested

7302:    Options Database:
7303: .  -mat_increase_overlap_scalable - use a scalable algorithm to compute the overlap (supported by MPIAIJ matrix)

7305:    Level: developer

7307: .seealso: MatCreateSubMatrices()
7308: @*/
7309: PetscErrorCode MatIncreaseOverlap(Mat mat,PetscInt n,IS is[],PetscInt ov)
7310: {

7316:   if (n < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Must have one or more domains, you have %D",n);
7317:   if (n) {
7320:   }
7321:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
7322:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
7323:   MatCheckPreallocated(mat,1);

7325:   if (!ov) return(0);
7326:   if (!mat->ops->increaseoverlap) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
7327:   PetscLogEventBegin(MAT_IncreaseOverlap,mat,0,0,0);
7328:   (*mat->ops->increaseoverlap)(mat,n,is,ov);
7329:   PetscLogEventEnd(MAT_IncreaseOverlap,mat,0,0,0);
7330:   return(0);
7331: }

7333: PetscErrorCode MatIncreaseOverlapSplit_Single(Mat,IS*,PetscInt);

7335: /*@
7336:    MatIncreaseOverlapSplit - Given a set of submatrices indicated by index sets across
7337:    a sub communicator, replaces the index sets by larger ones that represent submatrices with
7338:    additional overlap.

7340:    Collective on Mat

7342:    Input Parameters:
7343: +  mat - the matrix
7344: .  n   - the number of index sets
7345: .  is  - the array of index sets (these index sets will changed during the call)
7346: -  ov  - the additional overlap requested

7348:    Options Database:
7349: .  -mat_increase_overlap_scalable - use a scalable algorithm to compute the overlap (supported by MPIAIJ matrix)

7351:    Level: developer

7353: .seealso: MatCreateSubMatrices()
7354: @*/
7355: PetscErrorCode MatIncreaseOverlapSplit(Mat mat,PetscInt n,IS is[],PetscInt ov)
7356: {
7357:   PetscInt       i;

7363:   if (n < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Must have one or more domains, you have %D",n);
7364:   if (n) {
7367:   }
7368:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
7369:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
7370:   MatCheckPreallocated(mat,1);
7371:   if (!ov) return(0);
7372:   PetscLogEventBegin(MAT_IncreaseOverlap,mat,0,0,0);
7373:   for (i=0; i<n; i++) {
7374:          MatIncreaseOverlapSplit_Single(mat,&is[i],ov);
7375:   }
7376:   PetscLogEventEnd(MAT_IncreaseOverlap,mat,0,0,0);
7377:   return(0);
7378: }

7380: /*@
7381:    MatGetBlockSize - Returns the matrix block size.

7383:    Not Collective

7385:    Input Parameter:
7386: .  mat - the matrix

7388:    Output Parameter:
7389: .  bs - block size

7391:    Notes:
7392:     Block row formats are MATSEQBAIJ, MATMPIBAIJ, MATSEQSBAIJ, MATMPISBAIJ. These formats ALWAYS have square block storage in the matrix.

7394:    If the block size has not been set yet this routine returns 1.

7396:    Level: intermediate

7398: .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSizes()
7399: @*/
7400: PetscErrorCode MatGetBlockSize(Mat mat,PetscInt *bs)
7401: {
7405:   *bs = PetscAbs(mat->rmap->bs);
7406:   return(0);
7407: }

7409: /*@
7410:    MatGetBlockSizes - Returns the matrix block row and column sizes.

7412:    Not Collective

7414:    Input Parameter:
7415: .  mat - the matrix

7417:    Output Parameters:
7418: +  rbs - row block size
7419: -  cbs - column block size

7421:    Notes:
7422:     Block row formats are MATSEQBAIJ, MATMPIBAIJ, MATSEQSBAIJ, MATMPISBAIJ. These formats ALWAYS have square block storage in the matrix.
7423:     If you pass a different block size for the columns than the rows, the row block size determines the square block storage.

7425:    If a block size has not been set yet this routine returns 1.

7427:    Level: intermediate

7429: .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize(), MatSetBlockSize(), MatSetBlockSizes()
7430: @*/
7431: PetscErrorCode MatGetBlockSizes(Mat mat,PetscInt *rbs, PetscInt *cbs)
7432: {
7437:   if (rbs) *rbs = PetscAbs(mat->rmap->bs);
7438:   if (cbs) *cbs = PetscAbs(mat->cmap->bs);
7439:   return(0);
7440: }

7442: /*@
7443:    MatSetBlockSize - Sets the matrix block size.

7445:    Logically Collective on Mat

7447:    Input Parameters:
7448: +  mat - the matrix
7449: -  bs - block size

7451:    Notes:
7452:     Block row formats are MATSEQBAIJ, MATMPIBAIJ, MATSEQSBAIJ, MATMPISBAIJ. These formats ALWAYS have square block storage in the matrix.
7453:     This must be called before MatSetUp() or MatXXXSetPreallocation() (or will default to 1) and the block size cannot be changed later.

7455:     For MATMPIAIJ and MATSEQAIJ matrix formats, this function can be called at a later stage, provided that the specified block size
7456:     is compatible with the matrix local sizes.

7458:    Level: intermediate

7460: .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize(), MatSetBlockSizes(), MatGetBlockSizes()
7461: @*/
7462: PetscErrorCode MatSetBlockSize(Mat mat,PetscInt bs)
7463: {

7469:   MatSetBlockSizes(mat,bs,bs);
7470:   return(0);
7471: }

7473: /*@
7474:    MatSetVariableBlockSizes - Sets diagonal point-blocks of the matrix that need not be of the same size

7476:    Logically Collective on Mat

7478:    Input Parameters:
7479: +  mat - the matrix
7480: .  nblocks - the number of blocks on this process
7481: -  bsizes - the block sizes

7483:    Notes:
7484:     Currently used by PCVPBJACOBI for AIJ matrices

7486:     Each variable point-block set of degrees of freedom must live on a single MPI rank. That is a point block cannot straddle two MPI ranks.

7488:    Level: intermediate

7490: .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize(), MatSetBlockSizes(), MatGetBlockSizes(), MatGetVariableBlockSizes(), PCVPBJACOBI
7491: @*/
7492: PetscErrorCode MatSetVariableBlockSizes(Mat mat,PetscInt nblocks,PetscInt *bsizes)
7493: {
7495:   PetscInt       i,ncnt = 0, nlocal;

7499:   if (nblocks < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Number of local blocks must be great than or equal to zero");
7500:   MatGetLocalSize(mat,&nlocal,NULL);
7501:   for (i=0; i<nblocks; i++) ncnt += bsizes[i];
7502:   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);
7503:   PetscFree(mat->bsizes);
7504:   mat->nblocks = nblocks;
7505:   PetscMalloc1(nblocks,&mat->bsizes);
7506:   PetscArraycpy(mat->bsizes,bsizes,nblocks);
7507:   return(0);
7508: }

7510: /*@C
7511:    MatGetVariableBlockSizes - Gets a diagonal blocks of the matrix that need not be of the same size

7513:    Logically Collective on Mat

7515:    Input Parameter:
7516: .  mat - the matrix

7518:    Output Parameters:
7519: +  nblocks - the number of blocks on this process
7520: -  bsizes - the block sizes

7522:    Notes: Currently not supported from Fortran

7524:    Level: intermediate

7526: .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize(), MatSetBlockSizes(), MatGetBlockSizes(), MatSetVariableBlockSizes()
7527: @*/
7528: PetscErrorCode MatGetVariableBlockSizes(Mat mat,PetscInt *nblocks,const PetscInt **bsizes)
7529: {
7532:   *nblocks = mat->nblocks;
7533:   *bsizes  = mat->bsizes;
7534:   return(0);
7535: }

7537: /*@
7538:    MatSetBlockSizes - Sets the matrix block row and column sizes.

7540:    Logically Collective on Mat

7542:    Input Parameters:
7543: +  mat - the matrix
7544: .  rbs - row block size
7545: -  cbs - column block size

7547:    Notes:
7548:     Block row formats are MATSEQBAIJ, MATMPIBAIJ, MATSEQSBAIJ, MATMPISBAIJ. These formats ALWAYS have square block storage in the matrix.
7549:     If you pass a different block size for the columns than the rows, the row block size determines the square block storage.
7550:     This must be called before MatSetUp() or MatXXXSetPreallocation() (or will default to 1) and the block size cannot be changed later.

7552:     For MATMPIAIJ and MATSEQAIJ matrix formats, this function can be called at a later stage, provided that the specified block sizes
7553:     are compatible with the matrix local sizes.

7555:     The row and column block size determine the blocksize of the "row" and "column" vectors returned by MatCreateVecs().

7557:    Level: intermediate

7559: .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize(), MatSetBlockSize(), MatGetBlockSizes()
7560: @*/
7561: PetscErrorCode MatSetBlockSizes(Mat mat,PetscInt rbs,PetscInt cbs)
7562: {

7569:   if (mat->ops->setblocksizes) {
7570:     (*mat->ops->setblocksizes)(mat,rbs,cbs);
7571:   }
7572:   if (mat->rmap->refcnt) {
7573:     ISLocalToGlobalMapping l2g = NULL;
7574:     PetscLayout            nmap = NULL;

7576:     PetscLayoutDuplicate(mat->rmap,&nmap);
7577:     if (mat->rmap->mapping) {
7578:       ISLocalToGlobalMappingDuplicate(mat->rmap->mapping,&l2g);
7579:     }
7580:     PetscLayoutDestroy(&mat->rmap);
7581:     mat->rmap = nmap;
7582:     mat->rmap->mapping = l2g;
7583:   }
7584:   if (mat->cmap->refcnt) {
7585:     ISLocalToGlobalMapping l2g = NULL;
7586:     PetscLayout            nmap = NULL;

7588:     PetscLayoutDuplicate(mat->cmap,&nmap);
7589:     if (mat->cmap->mapping) {
7590:       ISLocalToGlobalMappingDuplicate(mat->cmap->mapping,&l2g);
7591:     }
7592:     PetscLayoutDestroy(&mat->cmap);
7593:     mat->cmap = nmap;
7594:     mat->cmap->mapping = l2g;
7595:   }
7596:   PetscLayoutSetBlockSize(mat->rmap,rbs);
7597:   PetscLayoutSetBlockSize(mat->cmap,cbs);
7598:   return(0);
7599: }

7601: /*@
7602:    MatSetBlockSizesFromMats - Sets the matrix block row and column sizes to match a pair of matrices

7604:    Logically Collective on Mat

7606:    Input Parameters:
7607: +  mat - the matrix
7608: .  fromRow - matrix from which to copy row block size
7609: -  fromCol - matrix from which to copy column block size (can be same as fromRow)

7611:    Level: developer

7613: .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize(), MatSetBlockSizes()
7614: @*/
7615: PetscErrorCode MatSetBlockSizesFromMats(Mat mat,Mat fromRow,Mat fromCol)
7616: {

7623:   if (fromRow->rmap->bs > 0) {PetscLayoutSetBlockSize(mat->rmap,fromRow->rmap->bs);}
7624:   if (fromCol->cmap->bs > 0) {PetscLayoutSetBlockSize(mat->cmap,fromCol->cmap->bs);}
7625:   return(0);
7626: }

7628: /*@
7629:    MatResidual - Default routine to calculate the residual.

7631:    Collective on Mat

7633:    Input Parameters:
7634: +  mat - the matrix
7635: .  b   - the right-hand-side
7636: -  x   - the approximate solution

7638:    Output Parameter:
7639: .  r - location to store the residual

7641:    Level: developer

7643: .seealso: PCMGSetResidual()
7644: @*/
7645: PetscErrorCode MatResidual(Mat mat,Vec b,Vec x,Vec r)
7646: {

7655:   MatCheckPreallocated(mat,1);
7656:   PetscLogEventBegin(MAT_Residual,mat,0,0,0);
7657:   if (!mat->ops->residual) {
7658:     MatMult(mat,x,r);
7659:     VecAYPX(r,-1.0,b);
7660:   } else {
7661:     (*mat->ops->residual)(mat,b,x,r);
7662:   }
7663:   PetscLogEventEnd(MAT_Residual,mat,0,0,0);
7664:   return(0);
7665: }

7667: /*@C
7668:     MatGetRowIJ - Returns the compressed row storage i and j indices for sequential matrices.

7670:    Collective on Mat

7672:     Input Parameters:
7673: +   mat - the matrix
7674: .   shift -  0 or 1 indicating we want the indices starting at 0 or 1
7675: .   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be   symmetrized
7676: -   inodecompressed - PETSC_TRUE or PETSC_FALSE  indicating if the nonzero structure of the
7677:                  inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is
7678:                  always used.

7680:     Output Parameters:
7681: +   n - number of rows in the (possibly compressed) matrix
7682: .   ia - the row pointers; that is ia[0] = 0, ia[row] = ia[row-1] + number of elements in that row of the matrix
7683: .   ja - the column indices
7684: -   done - indicates if the routine actually worked and returned appropriate ia[] and ja[] arrays; callers
7685:            are responsible for handling the case when done == PETSC_FALSE and ia and ja are not set

7687:     Level: developer

7689:     Notes:
7690:     You CANNOT change any of the ia[] or ja[] values.

7692:     Use MatRestoreRowIJ() when you are finished accessing the ia[] and ja[] values.

7694:     Fortran Notes:
7695:     In Fortran use
7696: $
7697: $      PetscInt ia(1), ja(1)
7698: $      PetscOffset iia, jja
7699: $      call MatGetRowIJ(mat,shift,symmetric,inodecompressed,n,ia,iia,ja,jja,done,ierr)
7700: $      ! Access the ith and jth entries via ia(iia + i) and ja(jja + j)

7702:      or
7703: $
7704: $    PetscInt, pointer :: ia(:),ja(:)
7705: $    call MatGetRowIJF90(mat,shift,symmetric,inodecompressed,n,ia,ja,done,ierr)
7706: $    ! Access the ith and jth entries via ia(i) and ja(j)

7708: .seealso: MatGetColumnIJ(), MatRestoreRowIJ(), MatSeqAIJGetArray()
7709: @*/
7710: PetscErrorCode MatGetRowIJ(Mat mat,PetscInt shift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
7711: {

7721:   MatCheckPreallocated(mat,1);
7722:   if (!mat->ops->getrowij) *done = PETSC_FALSE;
7723:   else {
7724:     *done = PETSC_TRUE;
7725:     PetscLogEventBegin(MAT_GetRowIJ,mat,0,0,0);
7726:     (*mat->ops->getrowij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);
7727:     PetscLogEventEnd(MAT_GetRowIJ,mat,0,0,0);
7728:   }
7729:   return(0);
7730: }

7732: /*@C
7733:     MatGetColumnIJ - Returns the compressed column storage i and j indices for sequential matrices.

7735:     Collective on Mat

7737:     Input Parameters:
7738: +   mat - the matrix
7739: .   shift - 1 or zero indicating we want the indices starting at 0 or 1
7740: .   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be
7741:                 symmetrized
7742: .   inodecompressed - PETSC_TRUE or PETSC_FALSE indicating if the nonzero structure of the
7743:                  inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is
7744:                  always used.
7745: .   n - number of columns in the (possibly compressed) matrix
7746: .   ia - the column pointers; that is ia[0] = 0, ia[col] = i[col-1] + number of elements in that col of the matrix
7747: -   ja - the row indices

7749:     Output Parameters:
7750: .   done - PETSC_TRUE or PETSC_FALSE, indicating whether the values have been returned

7752:     Level: developer

7754: .seealso: MatGetRowIJ(), MatRestoreColumnIJ()
7755: @*/
7756: PetscErrorCode MatGetColumnIJ(Mat mat,PetscInt shift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
7757: {

7767:   MatCheckPreallocated(mat,1);
7768:   if (!mat->ops->getcolumnij) *done = PETSC_FALSE;
7769:   else {
7770:     *done = PETSC_TRUE;
7771:     (*mat->ops->getcolumnij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);
7772:   }
7773:   return(0);
7774: }

7776: /*@C
7777:     MatRestoreRowIJ - Call after you are completed with the ia,ja indices obtained with
7778:     MatGetRowIJ().

7780:     Collective on Mat

7782:     Input Parameters:
7783: +   mat - the matrix
7784: .   shift - 1 or zero indicating we want the indices starting at 0 or 1
7785: .   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be
7786:                 symmetrized
7787: .   inodecompressed -  PETSC_TRUE or PETSC_FALSE indicating if the nonzero structure of the
7788:                  inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is
7789:                  always used.
7790: .   n - size of (possibly compressed) matrix
7791: .   ia - the row pointers
7792: -   ja - the column indices

7794:     Output Parameters:
7795: .   done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned

7797:     Note:
7798:     This routine zeros out n, ia, and ja. This is to prevent accidental
7799:     us of the array after it has been restored. If you pass NULL, it will
7800:     not zero the pointers.  Use of ia or ja after MatRestoreRowIJ() is invalid.

7802:     Level: developer

7804: .seealso: MatGetRowIJ(), MatRestoreColumnIJ()
7805: @*/
7806: PetscErrorCode MatRestoreRowIJ(Mat mat,PetscInt shift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
7807: {

7816:   MatCheckPreallocated(mat,1);

7818:   if (!mat->ops->restorerowij) *done = PETSC_FALSE;
7819:   else {
7820:     *done = PETSC_TRUE;
7821:     (*mat->ops->restorerowij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);
7822:     if (n)  *n = 0;
7823:     if (ia) *ia = NULL;
7824:     if (ja) *ja = NULL;
7825:   }
7826:   return(0);
7827: }

7829: /*@C
7830:     MatRestoreColumnIJ - Call after you are completed with the ia,ja indices obtained with
7831:     MatGetColumnIJ().

7833:     Collective on Mat

7835:     Input Parameters:
7836: +   mat - the matrix
7837: .   shift - 1 or zero indicating we want the indices starting at 0 or 1
7838: .   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be
7839:                 symmetrized
7840: -   inodecompressed - PETSC_TRUE or PETSC_FALSE indicating if the nonzero structure of the
7841:                  inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is
7842:                  always used.

7844:     Output Parameters:
7845: +   n - size of (possibly compressed) matrix
7846: .   ia - the column pointers
7847: .   ja - the row indices
7848: -   done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned

7850:     Level: developer

7852: .seealso: MatGetColumnIJ(), MatRestoreRowIJ()
7853: @*/
7854: PetscErrorCode MatRestoreColumnIJ(Mat mat,PetscInt shift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
7855: {

7864:   MatCheckPreallocated(mat,1);

7866:   if (!mat->ops->restorecolumnij) *done = PETSC_FALSE;
7867:   else {
7868:     *done = PETSC_TRUE;
7869:     (*mat->ops->restorecolumnij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);
7870:     if (n)  *n = 0;
7871:     if (ia) *ia = NULL;
7872:     if (ja) *ja = NULL;
7873:   }
7874:   return(0);
7875: }

7877: /*@C
7878:     MatColoringPatch -Used inside matrix coloring routines that
7879:     use MatGetRowIJ() and/or MatGetColumnIJ().

7881:     Collective on Mat

7883:     Input Parameters:
7884: +   mat - the matrix
7885: .   ncolors - max color value
7886: .   n   - number of entries in colorarray
7887: -   colorarray - array indicating color for each column

7889:     Output Parameters:
7890: .   iscoloring - coloring generated using colorarray information

7892:     Level: developer

7894: .seealso: MatGetRowIJ(), MatGetColumnIJ()

7896: @*/
7897: PetscErrorCode MatColoringPatch(Mat mat,PetscInt ncolors,PetscInt n,ISColoringValue colorarray[],ISColoring *iscoloring)
7898: {

7906:   MatCheckPreallocated(mat,1);

7908:   if (!mat->ops->coloringpatch) {
7909:     ISColoringCreate(PetscObjectComm((PetscObject)mat),ncolors,n,colorarray,PETSC_OWN_POINTER,iscoloring);
7910:   } else {
7911:     (*mat->ops->coloringpatch)(mat,ncolors,n,colorarray,iscoloring);
7912:   }
7913:   return(0);
7914: }

7916: /*@
7917:    MatSetUnfactored - Resets a factored matrix to be treated as unfactored.

7919:    Logically Collective on Mat

7921:    Input Parameter:
7922: .  mat - the factored matrix to be reset

7924:    Notes:
7925:    This routine should be used only with factored matrices formed by in-place
7926:    factorization via ILU(0) (or by in-place LU factorization for the MATSEQDENSE
7927:    format).  This option can save memory, for example, when solving nonlinear
7928:    systems with a matrix-free Newton-Krylov method and a matrix-based, in-place
7929:    ILU(0) preconditioner.

7931:    Note that one can specify in-place ILU(0) factorization by calling
7932: .vb
7933:      PCType(pc,PCILU);
7934:      PCFactorSeUseInPlace(pc);
7935: .ve
7936:    or by using the options -pc_type ilu -pc_factor_in_place

7938:    In-place factorization ILU(0) can also be used as a local
7939:    solver for the blocks within the block Jacobi or additive Schwarz
7940:    methods (runtime option: -sub_pc_factor_in_place).  See Users-Manual: ch_pc
7941:    for details on setting local solver options.

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

7947:    Level: developer

7949: .seealso: PCFactorSetUseInPlace(), PCFactorGetUseInPlace()

7951: @*/
7952: PetscErrorCode MatSetUnfactored(Mat mat)
7953: {

7959:   MatCheckPreallocated(mat,1);
7960:   mat->factortype = MAT_FACTOR_NONE;
7961:   if (!mat->ops->setunfactored) return(0);
7962:   (*mat->ops->setunfactored)(mat);
7963:   return(0);
7964: }

7966: /*MC
7967:     MatDenseGetArrayF90 - Accesses a matrix array from Fortran90.

7969:     Synopsis:
7970:     MatDenseGetArrayF90(Mat x,{Scalar, pointer :: xx_v(:,:)},integer ierr)

7972:     Not collective

7974:     Input Parameter:
7975: .   x - matrix

7977:     Output Parameters:
7978: +   xx_v - the Fortran90 pointer to the array
7979: -   ierr - error code

7981:     Example of Usage:
7982: .vb
7983:       PetscScalar, pointer xx_v(:,:)
7984:       ....
7985:       call MatDenseGetArrayF90(x,xx_v,ierr)
7986:       a = xx_v(3)
7987:       call MatDenseRestoreArrayF90(x,xx_v,ierr)
7988: .ve

7990:     Level: advanced

7992: .seealso:  MatDenseRestoreArrayF90(), MatDenseGetArray(), MatDenseRestoreArray(), MatSeqAIJGetArrayF90()

7994: M*/

7996: /*MC
7997:     MatDenseRestoreArrayF90 - Restores a matrix array that has been
7998:     accessed with MatDenseGetArrayF90().

8000:     Synopsis:
8001:     MatDenseRestoreArrayF90(Mat x,{Scalar, pointer :: xx_v(:,:)},integer ierr)

8003:     Not collective

8005:     Input Parameters:
8006: +   x - matrix
8007: -   xx_v - the Fortran90 pointer to the array

8009:     Output Parameter:
8010: .   ierr - error code

8012:     Example of Usage:
8013: .vb
8014:        PetscScalar, pointer xx_v(:,:)
8015:        ....
8016:        call MatDenseGetArrayF90(x,xx_v,ierr)
8017:        a = xx_v(3)
8018:        call MatDenseRestoreArrayF90(x,xx_v,ierr)
8019: .ve

8021:     Level: advanced

8023: .seealso:  MatDenseGetArrayF90(), MatDenseGetArray(), MatDenseRestoreArray(), MatSeqAIJRestoreArrayF90()

8025: M*/

8027: /*MC
8028:     MatSeqAIJGetArrayF90 - Accesses a matrix array from Fortran90.

8030:     Synopsis:
8031:     MatSeqAIJGetArrayF90(Mat x,{Scalar, pointer :: xx_v(:)},integer ierr)

8033:     Not collective

8035:     Input Parameter:
8036: .   x - matrix

8038:     Output Parameters:
8039: +   xx_v - the Fortran90 pointer to the array
8040: -   ierr - error code

8042:     Example of Usage:
8043: .vb
8044:       PetscScalar, pointer xx_v(:)
8045:       ....
8046:       call MatSeqAIJGetArrayF90(x,xx_v,ierr)
8047:       a = xx_v(3)
8048:       call MatSeqAIJRestoreArrayF90(x,xx_v,ierr)
8049: .ve

8051:     Level: advanced

8053: .seealso:  MatSeqAIJRestoreArrayF90(), MatSeqAIJGetArray(), MatSeqAIJRestoreArray(), MatDenseGetArrayF90()

8055: M*/

8057: /*MC
8058:     MatSeqAIJRestoreArrayF90 - Restores a matrix array that has been
8059:     accessed with MatSeqAIJGetArrayF90().

8061:     Synopsis:
8062:     MatSeqAIJRestoreArrayF90(Mat x,{Scalar, pointer :: xx_v(:)},integer ierr)

8064:     Not collective

8066:     Input Parameters:
8067: +   x - matrix
8068: -   xx_v - the Fortran90 pointer to the array

8070:     Output Parameter:
8071: .   ierr - error code

8073:     Example of Usage:
8074: .vb
8075:        PetscScalar, pointer xx_v(:)
8076:        ....
8077:        call MatSeqAIJGetArrayF90(x,xx_v,ierr)
8078:        a = xx_v(3)
8079:        call MatSeqAIJRestoreArrayF90(x,xx_v,ierr)
8080: .ve

8082:     Level: advanced

8084: .seealso:  MatSeqAIJGetArrayF90(), MatSeqAIJGetArray(), MatSeqAIJRestoreArray(), MatDenseRestoreArrayF90()

8086: M*/

8088: /*@
8089:     MatCreateSubMatrix - Gets a single submatrix on the same number of processors
8090:                       as the original matrix.

8092:     Collective on Mat

8094:     Input Parameters:
8095: +   mat - the original matrix
8096: .   isrow - parallel IS containing the rows this processor should obtain
8097: .   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.
8098: -   cll - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX

8100:     Output Parameter:
8101: .   newmat - the new submatrix, of the same type as the old

8103:     Level: advanced

8105:     Notes:
8106:     The submatrix will be able to be multiplied with vectors using the same layout as iscol.

8108:     Some matrix types place restrictions on the row and column indices, such
8109:     as that they be sorted or that they be equal to each other.

8111:     The index sets may not have duplicate entries.

8113:       The first time this is called you should use a cll of MAT_INITIAL_MATRIX,
8114:    the MatCreateSubMatrix() routine will create the newmat for you. Any additional calls
8115:    to this routine with a mat of the same nonzero structure and with a call of MAT_REUSE_MATRIX
8116:    will reuse the matrix generated the first time.  You should call MatDestroy() on newmat when
8117:    you are finished using it.

8119:     The communicator of the newly obtained matrix is ALWAYS the same as the communicator of
8120:     the input matrix.

8122:     If iscol is NULL then all columns are obtained (not supported in Fortran).

8124:    Example usage:
8125:    Consider the following 8x8 matrix with 34 non-zero values, that is
8126:    assembled across 3 processors. Let's assume that proc0 owns 3 rows,
8127:    proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown
8128:    as follows:

8130: .vb
8131:             1  2  0  |  0  3  0  |  0  4
8132:     Proc0   0  5  6  |  7  0  0  |  8  0
8133:             9  0 10  | 11  0  0  | 12  0
8134:     -------------------------------------
8135:            13  0 14  | 15 16 17  |  0  0
8136:     Proc1   0 18  0  | 19 20 21  |  0  0
8137:             0  0  0  | 22 23  0  | 24  0
8138:     -------------------------------------
8139:     Proc2  25 26 27  |  0  0 28  | 29  0
8140:            30  0  0  | 31 32 33  |  0 34
8141: .ve

8143:     Suppose isrow = [0 1 | 4 | 6 7] and iscol = [1 2 | 3 4 5 | 6].  The resulting submatrix is

8145: .vb
8146:             2  0  |  0  3  0  |  0
8147:     Proc0   5  6  |  7  0  0  |  8
8148:     -------------------------------
8149:     Proc1  18  0  | 19 20 21  |  0
8150:     -------------------------------
8151:     Proc2  26 27  |  0  0 28  | 29
8152:             0  0  | 31 32 33  |  0
8153: .ve

8155: .seealso: MatCreateSubMatrices(), MatCreateSubMatricesMPI(), MatCreateSubMatrixVirtual(), MatSubMatrixVirtualUpdate()
8156: @*/
8157: PetscErrorCode MatCreateSubMatrix(Mat mat,IS isrow,IS iscol,MatReuse cll,Mat *newmat)
8158: {
8160:   PetscMPIInt    size;
8161:   Mat            *local;
8162:   IS             iscoltmp;
8163:   PetscBool      flg;

8172:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8173:   if (cll == MAT_IGNORE_MATRIX) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Cannot use MAT_IGNORE_MATRIX");

8175:   MatCheckPreallocated(mat,1);
8176:   MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);

8178:   if (!iscol || isrow == iscol) {
8179:     PetscBool   stride;
8180:     PetscMPIInt grabentirematrix = 0,grab;
8181:     PetscObjectTypeCompare((PetscObject)isrow,ISSTRIDE,&stride);
8182:     if (stride) {
8183:       PetscInt first,step,n,rstart,rend;
8184:       ISStrideGetInfo(isrow,&first,&step);
8185:       if (step == 1) {
8186:         MatGetOwnershipRange(mat,&rstart,&rend);
8187:         if (rstart == first) {
8188:           ISGetLocalSize(isrow,&n);
8189:           if (n == rend-rstart) {
8190:             grabentirematrix = 1;
8191:           }
8192:         }
8193:       }
8194:     }
8195:     MPIU_Allreduce(&grabentirematrix,&grab,1,MPI_INT,MPI_MIN,PetscObjectComm((PetscObject)mat));
8196:     if (grab) {
8197:       PetscInfo(mat,"Getting entire matrix as submatrix\n");
8198:       if (cll == MAT_INITIAL_MATRIX) {
8199:         *newmat = mat;
8200:         PetscObjectReference((PetscObject)mat);
8201:       }
8202:       return(0);
8203:     }
8204:   }

8206:   if (!iscol) {
8207:     ISCreateStride(PetscObjectComm((PetscObject)mat),mat->cmap->n,mat->cmap->rstart,1,&iscoltmp);
8208:   } else {
8209:     iscoltmp = iscol;
8210:   }

8212:   /* if original matrix is on just one processor then use submatrix generated */
8213:   if (mat->ops->createsubmatrices && !mat->ops->createsubmatrix && size == 1 && cll == MAT_REUSE_MATRIX) {
8214:     MatCreateSubMatrices(mat,1,&isrow,&iscoltmp,MAT_REUSE_MATRIX,&newmat);
8215:     goto setproperties;
8216:   } else if (mat->ops->createsubmatrices && !mat->ops->createsubmatrix && size == 1) {
8217:     MatCreateSubMatrices(mat,1,&isrow,&iscoltmp,MAT_INITIAL_MATRIX,&local);
8218:     *newmat = *local;
8219:     PetscFree(local);
8220:     goto setproperties;
8221:   } else if (!mat->ops->createsubmatrix) {
8222:     /* Create a new matrix type that implements the operation using the full matrix */
8223:     PetscLogEventBegin(MAT_CreateSubMat,mat,0,0,0);
8224:     switch (cll) {
8225:     case MAT_INITIAL_MATRIX:
8226:       MatCreateSubMatrixVirtual(mat,isrow,iscoltmp,newmat);
8227:       break;
8228:     case MAT_REUSE_MATRIX:
8229:       MatSubMatrixVirtualUpdate(*newmat,mat,isrow,iscoltmp);
8230:       break;
8231:     default: SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"Invalid MatReuse, must be either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX");
8232:     }
8233:     PetscLogEventEnd(MAT_CreateSubMat,mat,0,0,0);
8234:     goto setproperties;
8235:   }

8237:   if (!mat->ops->createsubmatrix) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
8238:   PetscLogEventBegin(MAT_CreateSubMat,mat,0,0,0);
8239:   (*mat->ops->createsubmatrix)(mat,isrow,iscoltmp,cll,newmat);
8240:   PetscLogEventEnd(MAT_CreateSubMat,mat,0,0,0);

8242: setproperties:
8243:   ISEqualUnsorted(isrow,iscoltmp,&flg);
8244:   if (flg) {
8245:     MatPropagateSymmetryOptions(mat,*newmat);
8246:   }
8247:   if (!iscol) {ISDestroy(&iscoltmp);}
8248:   if (*newmat && cll == MAT_INITIAL_MATRIX) {PetscObjectStateIncrease((PetscObject)*newmat);}
8249:   return(0);
8250: }

8252: /*@
8253:    MatPropagateSymmetryOptions - Propagates symmetry options set on a matrix to another matrix

8255:    Not Collective

8257:    Input Parameters:
8258: +  A - the matrix we wish to propagate options from
8259: -  B - the matrix we wish to propagate options to

8261:    Level: beginner

8263:    Notes: Propagates the options associated to MAT_SYMMETRY_ETERNAL, MAT_STRUCTURALLY_SYMMETRIC, MAT_HERMITIAN, MAT_SPD and MAT_SYMMETRIC

8265: .seealso: MatSetOption()
8266: @*/
8267: PetscErrorCode MatPropagateSymmetryOptions(Mat A, Mat B)
8268: {

8274:   if (A->symmetric_eternal) { /* symmetric_eternal does not have a corresponding *set flag */
8275:     MatSetOption(B,MAT_SYMMETRY_ETERNAL,A->symmetric_eternal);
8276:   }
8277:   if (A->structurally_symmetric_set) {
8278:     MatSetOption(B,MAT_STRUCTURALLY_SYMMETRIC,A->structurally_symmetric);
8279:   }
8280:   if (A->hermitian_set) {
8281:     MatSetOption(B,MAT_HERMITIAN,A->hermitian);
8282:   }
8283:   if (A->spd_set) {
8284:     MatSetOption(B,MAT_SPD,A->spd);
8285:   }
8286:   if (A->symmetric_set) {
8287:     MatSetOption(B,MAT_SYMMETRIC,A->symmetric);
8288:   }
8289:   return(0);
8290: }

8292: /*@
8293:    MatStashSetInitialSize - sets the sizes of the matrix stash, that is
8294:    used during the assembly process to store values that belong to
8295:    other processors.

8297:    Not Collective

8299:    Input Parameters:
8300: +  mat   - the matrix
8301: .  size  - the initial size of the stash.
8302: -  bsize - the initial size of the block-stash(if used).

8304:    Options Database Keys:
8305: +   -matstash_initial_size <size> or <size0,size1,...sizep-1>
8306: -   -matstash_block_initial_size <bsize>  or <bsize0,bsize1,...bsizep-1>

8308:    Level: intermediate

8310:    Notes:
8311:      The block-stash is used for values set with MatSetValuesBlocked() while
8312:      the stash is used for values set with MatSetValues()

8314:      Run with the option -info and look for output of the form
8315:      MatAssemblyBegin_MPIXXX:Stash has MM entries, uses nn mallocs.
8316:      to determine the appropriate value, MM, to use for size and
8317:      MatAssemblyBegin_MPIXXX:Block-Stash has BMM entries, uses nn mallocs.
8318:      to determine the value, BMM to use for bsize

8320: .seealso: MatAssemblyBegin(), MatAssemblyEnd(), Mat, MatStashGetInfo()

8322: @*/
8323: PetscErrorCode MatStashSetInitialSize(Mat mat,PetscInt size, PetscInt bsize)
8324: {

8330:   MatStashSetInitialSize_Private(&mat->stash,size);
8331:   MatStashSetInitialSize_Private(&mat->bstash,bsize);
8332:   return(0);
8333: }

8335: /*@
8336:    MatInterpolateAdd - w = y + A*x or A'*x depending on the shape of
8337:      the matrix

8339:    Neighbor-wise Collective on Mat

8341:    Input Parameters:
8342: +  mat   - the matrix
8343: .  x,y - the vectors
8344: -  w - where the result is stored

8346:    Level: intermediate

8348:    Notes:
8349:     w may be the same vector as y.

8351:     This allows one to use either the restriction or interpolation (its transpose)
8352:     matrix to do the interpolation

8354: .seealso: MatMultAdd(), MatMultTransposeAdd(), MatRestrict()

8356: @*/
8357: PetscErrorCode MatInterpolateAdd(Mat A,Vec x,Vec y,Vec w)
8358: {
8360:   PetscInt       M,N,Ny;

8367:   MatGetSize(A,&M,&N);
8368:   VecGetSize(y,&Ny);
8369:   if (M == Ny) {
8370:     MatMultAdd(A,x,y,w);
8371:   } else {
8372:     MatMultTransposeAdd(A,x,y,w);
8373:   }
8374:   return(0);
8375: }

8377: /*@
8378:    MatInterpolate - y = A*x or A'*x depending on the shape of
8379:      the matrix

8381:    Neighbor-wise Collective on Mat

8383:    Input Parameters:
8384: +  mat   - the matrix
8385: -  x,y - the vectors

8387:    Level: intermediate

8389:    Notes:
8390:     This allows one to use either the restriction or interpolation (its transpose)
8391:     matrix to do the interpolation

8393: .seealso: MatMultAdd(), MatMultTransposeAdd(), MatRestrict()

8395: @*/
8396: PetscErrorCode MatInterpolate(Mat A,Vec x,Vec y)
8397: {
8399:   PetscInt       M,N,Ny;

8405:   MatGetSize(A,&M,&N);
8406:   VecGetSize(y,&Ny);
8407:   if (M == Ny) {
8408:     MatMult(A,x,y);
8409:   } else {
8410:     MatMultTranspose(A,x,y);
8411:   }
8412:   return(0);
8413: }

8415: /*@
8416:    MatRestrict - y = A*x or A'*x

8418:    Neighbor-wise Collective on Mat

8420:    Input Parameters:
8421: +  mat   - the matrix
8422: -  x,y - the vectors

8424:    Level: intermediate

8426:    Notes:
8427:     This allows one to use either the restriction or interpolation (its transpose)
8428:     matrix to do the restriction

8430: .seealso: MatMultAdd(), MatMultTransposeAdd(), MatInterpolate()

8432: @*/
8433: PetscErrorCode MatRestrict(Mat A,Vec x,Vec y)
8434: {
8436:   PetscInt       M,N,Ny;

8442:   MatGetSize(A,&M,&N);
8443:   VecGetSize(y,&Ny);
8444:   if (M == Ny) {
8445:     MatMult(A,x,y);
8446:   } else {
8447:     MatMultTranspose(A,x,y);
8448:   }
8449:   return(0);
8450: }

8452: /*@
8453:    MatMatInterpolateAdd - Y = W + A*X or W + A'*X

8455:    Neighbor-wise Collective on Mat

8457:    Input Parameters:
8458: +  mat   - the matrix
8459: -  w, x - the input dense matrices

8461:    Output Parameters:
8462: .  y - the output dense matrix

8464:    Level: intermediate

8466:    Notes:
8467:     This allows one to use either the restriction or interpolation (its transpose)
8468:     matrix to do the interpolation. y matrix can be reused if already created with the proper sizes,
8469:     otherwise it will be recreated. y must be initialized to NULL if not supplied.

8471: .seealso: MatInterpolateAdd(), MatMatInterpolate(), MatMatRestrict()

8473: @*/
8474: PetscErrorCode MatMatInterpolateAdd(Mat A,Mat x,Mat w,Mat *y)
8475: {
8477:   PetscInt       M,N,Mx,Nx,Mo,My = 0,Ny = 0;
8478:   PetscBool      trans = PETSC_TRUE;
8479:   MatReuse       reuse = MAT_INITIAL_MATRIX;

8487:   MatGetSize(A,&M,&N);
8488:   MatGetSize(x,&Mx,&Nx);
8489:   if (N == Mx) trans = PETSC_FALSE;
8490:   else if (M != Mx) SETERRQ4(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Size mismatch: A %Dx%D, X %Dx%D",M,N,Mx,Nx);
8491:   Mo = trans ? N : M;
8492:   if (*y) {
8493:     MatGetSize(*y,&My,&Ny);
8494:     if (Mo == My && Nx == Ny) { reuse = MAT_REUSE_MATRIX; }
8495:     else {
8496:       if (w && *y == w) SETERRQ6(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Cannot reuse y and w, size mismatch: A %Dx%D, X %Dx%D, Y %Dx%D",M,N,Mx,Nx,My,Ny);
8497:       MatDestroy(y);
8498:     }
8499:   }

8501:   if (w && *y == w) { /* this is to minimize changes in PCMG */
8502:     PetscBool flg;

8504:     PetscObjectQuery((PetscObject)*y,"__MatMatIntAdd_w",(PetscObject*)&w);
8505:     if (w) {
8506:       PetscInt My,Ny,Mw,Nw;

8508:       PetscObjectTypeCompare((PetscObject)*y,((PetscObject)w)->type_name,&flg);
8509:       MatGetSize(*y,&My,&Ny);
8510:       MatGetSize(w,&Mw,&Nw);
8511:       if (!flg || My != Mw || Ny != Nw) w = NULL;
8512:     }
8513:     if (!w) {
8514:       MatDuplicate(*y,MAT_COPY_VALUES,&w);
8515:       PetscObjectCompose((PetscObject)*y,"__MatMatIntAdd_w",(PetscObject)w);
8516:       PetscLogObjectParent((PetscObject)*y,(PetscObject)w);
8517:       PetscObjectDereference((PetscObject)w);
8518:     } else {
8519:       MatCopy(*y,w,UNKNOWN_NONZERO_PATTERN);
8520:     }
8521:   }
8522:   if (!trans) {
8523:     MatMatMult(A,x,reuse,PETSC_DEFAULT,y);
8524:   } else {
8525:     MatTransposeMatMult(A,x,reuse,PETSC_DEFAULT,y);
8526:   }
8527:   if (w) {
8528:     MatAXPY(*y,1.0,w,UNKNOWN_NONZERO_PATTERN);
8529:   }
8530:   return(0);
8531: }

8533: /*@
8534:    MatMatInterpolate - Y = A*X or A'*X

8536:    Neighbor-wise Collective on Mat

8538:    Input Parameters:
8539: +  mat   - the matrix
8540: -  x - the input dense matrix

8542:    Output Parameters:
8543: .  y - the output dense matrix

8545:    Level: intermediate

8547:    Notes:
8548:     This allows one to use either the restriction or interpolation (its transpose)
8549:     matrix to do the interpolation. y matrix can be reused if already created with the proper sizes,
8550:     otherwise it will be recreated. y must be initialized to NULL if not supplied.

8552: .seealso: MatInterpolate(), MatRestrict(), MatMatRestrict()

8554: @*/
8555: PetscErrorCode MatMatInterpolate(Mat A,Mat x,Mat *y)
8556: {

8560:   MatMatInterpolateAdd(A,x,NULL,y);
8561:   return(0);
8562: }

8564: /*@
8565:    MatMatRestrict - Y = A*X or A'*X

8567:    Neighbor-wise Collective on Mat

8569:    Input Parameters:
8570: +  mat   - the matrix
8571: -  x - the input dense matrix

8573:    Output Parameters:
8574: .  y - the output dense matrix

8576:    Level: intermediate

8578:    Notes:
8579:     This allows one to use either the restriction or interpolation (its transpose)
8580:     matrix to do the restriction. y matrix can be reused if already created with the proper sizes,
8581:     otherwise it will be recreated. y must be initialized to NULL if not supplied.

8583: .seealso: MatRestrict(), MatInterpolate(), MatMatInterpolate()
8584: @*/
8585: PetscErrorCode MatMatRestrict(Mat A,Mat x,Mat *y)
8586: {

8590:   MatMatInterpolateAdd(A,x,NULL,y);
8591:   return(0);
8592: }

8594: /*@
8595:    MatGetNullSpace - retrieves the null space of a matrix.

8597:    Logically Collective on Mat

8599:    Input Parameters:
8600: +  mat - the matrix
8601: -  nullsp - the null space object

8603:    Level: developer

8605: .seealso: MatCreate(), MatNullSpaceCreate(), MatSetNearNullSpace(), MatSetNullSpace()
8606: @*/
8607: PetscErrorCode MatGetNullSpace(Mat mat, MatNullSpace *nullsp)
8608: {
8612:   *nullsp = (mat->symmetric_set && mat->symmetric && !mat->nullsp) ? mat->transnullsp : mat->nullsp;
8613:   return(0);
8614: }

8616: /*@
8617:    MatSetNullSpace - attaches a null space to a matrix.

8619:    Logically Collective on Mat

8621:    Input Parameters:
8622: +  mat - the matrix
8623: -  nullsp - the null space object

8625:    Level: advanced

8627:    Notes:
8628:       This null space is used by the linear solvers. Overwrites any previous null space that may have been attached

8630:       For inconsistent singular systems (linear systems where the right hand side is not in the range of the operator) you also likely should
8631:       call MatSetTransposeNullSpace(). This allows the linear system to be solved in a least squares sense.

8633:       You can remove the null space by calling this routine with an nullsp of NULL

8635:       The fundamental theorem of linear algebra (Gilbert Strang, Introduction to Applied Mathematics, page 72) states that
8636:    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).
8637:    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
8638:    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
8639:    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).

8641:       Krylov solvers can produce the minimal norm solution to the least squares problem by utilizing MatNullSpaceRemove().

8643:     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
8644:     routine also automatically calls MatSetTransposeNullSpace().

8646: .seealso: MatCreate(), MatNullSpaceCreate(), MatSetNearNullSpace(), MatGetNullSpace(), MatSetTransposeNullSpace(), MatGetTransposeNullSpace(), MatNullSpaceRemove()
8647: @*/
8648: PetscErrorCode MatSetNullSpace(Mat mat,MatNullSpace nullsp)
8649: {

8655:   if (nullsp) {PetscObjectReference((PetscObject)nullsp);}
8656:   MatNullSpaceDestroy(&mat->nullsp);
8657:   mat->nullsp = nullsp;
8658:   if (mat->symmetric_set && mat->symmetric) {
8659:     MatSetTransposeNullSpace(mat,nullsp);
8660:   }
8661:   return(0);
8662: }

8664: /*@
8665:    MatGetTransposeNullSpace - retrieves the null space of the transpose of a matrix.

8667:    Logically Collective on Mat

8669:    Input Parameters:
8670: +  mat - the matrix
8671: -  nullsp - the null space object

8673:    Level: developer

8675: .seealso: MatCreate(), MatNullSpaceCreate(), MatSetNearNullSpace(), MatSetTransposeNullSpace(), MatSetNullSpace(), MatGetNullSpace()
8676: @*/
8677: PetscErrorCode MatGetTransposeNullSpace(Mat mat, MatNullSpace *nullsp)
8678: {
8683:   *nullsp = (mat->symmetric_set && mat->symmetric && !mat->transnullsp) ? mat->nullsp : mat->transnullsp;
8684:   return(0);
8685: }

8687: /*@
8688:    MatSetTransposeNullSpace - attaches a null space to a matrix.

8690:    Logically Collective on Mat

8692:    Input Parameters:
8693: +  mat - the matrix
8694: -  nullsp - the null space object

8696:    Level: advanced

8698:    Notes:
8699:       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.
8700:       You must also call MatSetNullSpace()

8702:       The fundamental theorem of linear algebra (Gilbert Strang, Introduction to Applied Mathematics, page 72) states that
8703:    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).
8704:    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
8705:    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
8706:    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).

8708:       Krylov solvers can produce the minimal norm solution to the least squares problem by utilizing MatNullSpaceRemove().

8710: .seealso: MatCreate(), MatNullSpaceCreate(), MatSetNearNullSpace(), MatGetNullSpace(), MatSetNullSpace(), MatGetTransposeNullSpace(), MatNullSpaceRemove()
8711: @*/
8712: PetscErrorCode MatSetTransposeNullSpace(Mat mat,MatNullSpace nullsp)
8713: {

8719:   if (nullsp) {PetscObjectReference((PetscObject)nullsp);}
8720:   MatNullSpaceDestroy(&mat->transnullsp);
8721:   mat->transnullsp = nullsp;
8722:   return(0);
8723: }

8725: /*@
8726:    MatSetNearNullSpace - attaches a null space to a matrix, which is often the null space (rigid body modes) of the operator without boundary conditions
8727:         This null space will be used to provide near null space vectors to a multigrid preconditioner built from this matrix.

8729:    Logically Collective on Mat

8731:    Input Parameters:
8732: +  mat - the matrix
8733: -  nullsp - the null space object

8735:    Level: advanced

8737:    Notes:
8738:       Overwrites any previous near null space that may have been attached

8740:       You can remove the null space by calling this routine with an nullsp of NULL

8742: .seealso: MatCreate(), MatNullSpaceCreate(), MatSetNullSpace(), MatNullSpaceCreateRigidBody(), MatGetNearNullSpace()
8743: @*/
8744: PetscErrorCode MatSetNearNullSpace(Mat mat,MatNullSpace nullsp)
8745: {

8752:   MatCheckPreallocated(mat,1);
8753:   if (nullsp) {PetscObjectReference((PetscObject)nullsp);}
8754:   MatNullSpaceDestroy(&mat->nearnullsp);
8755:   mat->nearnullsp = nullsp;
8756:   return(0);
8757: }

8759: /*@
8760:    MatGetNearNullSpace - Get null space attached with MatSetNearNullSpace()

8762:    Not Collective

8764:    Input Parameter:
8765: .  mat - the matrix

8767:    Output Parameter:
8768: .  nullsp - the null space object, NULL if not set

8770:    Level: developer

8772: .seealso: MatSetNearNullSpace(), MatGetNullSpace(), MatNullSpaceCreate()
8773: @*/
8774: PetscErrorCode MatGetNearNullSpace(Mat mat,MatNullSpace *nullsp)
8775: {
8780:   MatCheckPreallocated(mat,1);
8781:   *nullsp = mat->nearnullsp;
8782:   return(0);
8783: }

8785: /*@C
8786:    MatICCFactor - Performs in-place incomplete Cholesky factorization of matrix.

8788:    Collective on Mat

8790:    Input Parameters:
8791: +  mat - the matrix
8792: .  row - row/column permutation
8793: .  fill - expected fill factor >= 1.0
8794: -  level - level of fill, for ICC(k)

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

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

8804:    Level: developer

8806: .seealso: MatICCFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor()

8808:     Developer Note: fortran interface is not autogenerated as the f90
8809:     interface definition cannot be generated correctly [due to MatFactorInfo]

8811: @*/
8812: PetscErrorCode MatICCFactor(Mat mat,IS row,const MatFactorInfo *info)
8813: {

8821:   if (mat->rmap->N != mat->cmap->N) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONG,"matrix must be square");
8822:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8823:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8824:   if (!mat->ops->iccfactor) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
8825:   MatCheckPreallocated(mat,1);
8826:   (*mat->ops->iccfactor)(mat,row,info);
8827:   PetscObjectStateIncrease((PetscObject)mat);
8828:   return(0);
8829: }

8831: /*@
8832:    MatDiagonalScaleLocal - Scales columns of a matrix given the scaling values including the
8833:          ghosted ones.

8835:    Not Collective

8837:    Input Parameters:
8838: +  mat - the matrix
8839: -  diag = the diagonal values, including ghost ones

8841:    Level: developer

8843:    Notes:
8844:     Works only for MPIAIJ and MPIBAIJ matrices

8846: .seealso: MatDiagonalScale()
8847: @*/
8848: PetscErrorCode MatDiagonalScaleLocal(Mat mat,Vec diag)
8849: {
8851:   PetscMPIInt    size;


8858:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Matrix must be already assembled");
8859:   PetscLogEventBegin(MAT_Scale,mat,0,0,0);
8860:   MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);
8861:   if (size == 1) {
8862:     PetscInt n,m;
8863:     VecGetSize(diag,&n);
8864:     MatGetSize(mat,NULL,&m);
8865:     if (m == n) {
8866:       MatDiagonalScale(mat,NULL,diag);
8867:     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only supported for sequential matrices when no ghost points/periodic conditions");
8868:   } else {
8869:     PetscUseMethod(mat,"MatDiagonalScaleLocal_C",(Mat,Vec),(mat,diag));
8870:   }
8871:   PetscLogEventEnd(MAT_Scale,mat,0,0,0);
8872:   PetscObjectStateIncrease((PetscObject)mat);
8873:   return(0);
8874: }

8876: /*@
8877:    MatGetInertia - Gets the inertia from a factored matrix

8879:    Collective on Mat

8881:    Input Parameter:
8882: .  mat - the matrix

8884:    Output Parameters:
8885: +   nneg - number of negative eigenvalues
8886: .   nzero - number of zero eigenvalues
8887: -   npos - number of positive eigenvalues

8889:    Level: advanced

8891:    Notes:
8892:     Matrix must have been factored by MatCholeskyFactor()

8894: @*/
8895: PetscErrorCode MatGetInertia(Mat mat,PetscInt *nneg,PetscInt *nzero,PetscInt *npos)
8896: {

8902:   if (!mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
8903:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Numeric factor mat is not assembled");
8904:   if (!mat->ops->getinertia) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
8905:   (*mat->ops->getinertia)(mat,nneg,nzero,npos);
8906:   return(0);
8907: }

8909: /* ----------------------------------------------------------------*/
8910: /*@C
8911:    MatSolves - Solves A x = b, given a factored matrix, for a collection of vectors

8913:    Neighbor-wise Collective on Mats

8915:    Input Parameters:
8916: +  mat - the factored matrix
8917: -  b - the right-hand-side vectors

8919:    Output Parameter:
8920: .  x - the result vectors

8922:    Notes:
8923:    The vectors b and x cannot be the same.  I.e., one cannot
8924:    call MatSolves(A,x,x).

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

8931:    Level: developer

8933: .seealso: MatSolveAdd(), MatSolveTranspose(), MatSolveTransposeAdd(), MatSolve()
8934: @*/
8935: PetscErrorCode MatSolves(Mat mat,Vecs b,Vecs x)
8936: {

8942:   if (x == b) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"x and b must be different vectors");
8943:   if (!mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
8944:   if (!mat->rmap->N && !mat->cmap->N) return(0);

8946:   if (!mat->ops->solves) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
8947:   MatCheckPreallocated(mat,1);
8948:   PetscLogEventBegin(MAT_Solves,mat,0,0,0);
8949:   (*mat->ops->solves)(mat,b,x);
8950:   PetscLogEventEnd(MAT_Solves,mat,0,0,0);
8951:   return(0);
8952: }

8954: /*@
8955:    MatIsSymmetric - Test whether a matrix is symmetric

8957:    Collective on Mat

8959:    Input Parameters:
8960: +  A - the matrix to test
8961: -  tol - difference between value and its transpose less than this amount counts as equal (use 0.0 for exact transpose)

8963:    Output Parameters:
8964: .  flg - the result

8966:    Notes:
8967:     For real numbers MatIsSymmetric() and MatIsHermitian() return identical results

8969:    Level: intermediate

8971: .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), MatIsSymmetricKnown()
8972: @*/
8973: PetscErrorCode MatIsSymmetric(Mat A,PetscReal tol,PetscBool  *flg)
8974: {


8981:   if (!A->symmetric_set) {
8982:     if (!A->ops->issymmetric) {
8983:       MatType mattype;
8984:       MatGetType(A,&mattype);
8985:       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type %s does not support checking for symmetric",mattype);
8986:     }
8987:     (*A->ops->issymmetric)(A,tol,flg);
8988:     if (!tol) {
8989:       MatSetOption(A,MAT_SYMMETRIC,*flg);
8990:     }
8991:   } else if (A->symmetric) {
8992:     *flg = PETSC_TRUE;
8993:   } else if (!tol) {
8994:     *flg = PETSC_FALSE;
8995:   } else {
8996:     if (!A->ops->issymmetric) {
8997:       MatType mattype;
8998:       MatGetType(A,&mattype);
8999:       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type %s does not support checking for symmetric",mattype);
9000:     }
9001:     (*A->ops->issymmetric)(A,tol,flg);
9002:   }
9003:   return(0);
9004: }

9006: /*@
9007:    MatIsHermitian - Test whether a matrix is Hermitian

9009:    Collective on Mat

9011:    Input Parameters:
9012: +  A - the matrix to test
9013: -  tol - difference between value and its transpose less than this amount counts as equal (use 0.0 for exact Hermitian)

9015:    Output Parameters:
9016: .  flg - the result

9018:    Level: intermediate

9020: .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(),
9021:           MatIsSymmetricKnown(), MatIsSymmetric()
9022: @*/
9023: PetscErrorCode MatIsHermitian(Mat A,PetscReal tol,PetscBool  *flg)
9024: {


9031:   if (!A->hermitian_set) {
9032:     if (!A->ops->ishermitian) {
9033:       MatType mattype;
9034:       MatGetType(A,&mattype);
9035:       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type %s does not support checking for hermitian",mattype);
9036:     }
9037:     (*A->ops->ishermitian)(A,tol,flg);
9038:     if (!tol) {
9039:       MatSetOption(A,MAT_HERMITIAN,*flg);
9040:     }
9041:   } else if (A->hermitian) {
9042:     *flg = PETSC_TRUE;
9043:   } else if (!tol) {
9044:     *flg = PETSC_FALSE;
9045:   } else {
9046:     if (!A->ops->ishermitian) {
9047:       MatType mattype;
9048:       MatGetType(A,&mattype);
9049:       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type %s does not support checking for hermitian",mattype);
9050:     }
9051:     (*A->ops->ishermitian)(A,tol,flg);
9052:   }
9053:   return(0);
9054: }

9056: /*@
9057:    MatIsSymmetricKnown - Checks the flag on the matrix to see if it is symmetric.

9059:    Not Collective

9061:    Input Parameter:
9062: .  A - the matrix to check

9064:    Output Parameters:
9065: +  set - if the symmetric flag is set (this tells you if the next flag is valid)
9066: -  flg - the result

9068:    Level: advanced

9070:    Note: Does not check the matrix values directly, so this may return unknown (set = PETSC_FALSE). Use MatIsSymmetric()
9071:          if you want it explicitly checked

9073: .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), MatIsSymmetric()
9074: @*/
9075: PetscErrorCode MatIsSymmetricKnown(Mat A,PetscBool *set,PetscBool *flg)
9076: {
9081:   if (A->symmetric_set) {
9082:     *set = PETSC_TRUE;
9083:     *flg = A->symmetric;
9084:   } else {
9085:     *set = PETSC_FALSE;
9086:   }
9087:   return(0);
9088: }

9090: /*@
9091:    MatIsHermitianKnown - Checks the flag on the matrix to see if it is hermitian.

9093:    Not Collective

9095:    Input Parameter:
9096: .  A - the matrix to check

9098:    Output Parameters:
9099: +  set - if the hermitian flag is set (this tells you if the next flag is valid)
9100: -  flg - the result

9102:    Level: advanced

9104:    Note: Does not check the matrix values directly, so this may return unknown (set = PETSC_FALSE). Use MatIsHermitian()
9105:          if you want it explicitly checked

9107: .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), MatIsSymmetric()
9108: @*/
9109: PetscErrorCode MatIsHermitianKnown(Mat A,PetscBool *set,PetscBool *flg)
9110: {
9115:   if (A->hermitian_set) {
9116:     *set = PETSC_TRUE;
9117:     *flg = A->hermitian;
9118:   } else {
9119:     *set = PETSC_FALSE;
9120:   }
9121:   return(0);
9122: }

9124: /*@
9125:    MatIsStructurallySymmetric - Test whether a matrix is structurally symmetric

9127:    Collective on Mat

9129:    Input Parameter:
9130: .  A - the matrix to test

9132:    Output Parameters:
9133: .  flg - the result

9135:    Level: intermediate

9137: .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsSymmetric(), MatSetOption()
9138: @*/
9139: PetscErrorCode MatIsStructurallySymmetric(Mat A,PetscBool *flg)
9140: {

9146:   if (!A->structurally_symmetric_set) {
9147:     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);
9148:     (*A->ops->isstructurallysymmetric)(A,flg);
9149:     MatSetOption(A,MAT_STRUCTURALLY_SYMMETRIC,*flg);
9150:   } else *flg = A->structurally_symmetric;
9151:   return(0);
9152: }

9154: /*@
9155:    MatStashGetInfo - Gets how many values are currently in the matrix stash, i.e. need
9156:        to be communicated to other processors during the MatAssemblyBegin/End() process

9158:     Not collective

9160:    Input Parameter:
9161: .   vec - the vector

9163:    Output Parameters:
9164: +   nstash   - the size of the stash
9165: .   reallocs - the number of additional mallocs incurred.
9166: .   bnstash   - the size of the block stash
9167: -   breallocs - the number of additional mallocs incurred.in the block stash

9169:    Level: advanced

9171: .seealso: MatAssemblyBegin(), MatAssemblyEnd(), Mat, MatStashSetInitialSize()

9173: @*/
9174: PetscErrorCode MatStashGetInfo(Mat mat,PetscInt *nstash,PetscInt *reallocs,PetscInt *bnstash,PetscInt *breallocs)
9175: {

9179:   MatStashGetInfo_Private(&mat->stash,nstash,reallocs);
9180:   MatStashGetInfo_Private(&mat->bstash,bnstash,breallocs);
9181:   return(0);
9182: }

9184: /*@C
9185:    MatCreateVecs - Get vector(s) compatible with the matrix, i.e. with the same
9186:      parallel layout

9188:    Collective on Mat

9190:    Input Parameter:
9191: .  mat - the matrix

9193:    Output Parameters:
9194: +   right - (optional) vector that the matrix can be multiplied against
9195: -   left - (optional) vector that the matrix vector product can be stored in

9197:    Notes:
9198:     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().

9200:   Notes:
9201:     These are new vectors which are not owned by the Mat, they should be destroyed in VecDestroy() when no longer needed

9203:   Level: advanced

9205: .seealso: MatCreate(), VecDestroy()
9206: @*/
9207: PetscErrorCode MatCreateVecs(Mat mat,Vec *right,Vec *left)
9208: {

9214:   if (mat->ops->getvecs) {
9215:     (*mat->ops->getvecs)(mat,right,left);
9216:   } else {
9217:     PetscInt rbs,cbs;
9218:     MatGetBlockSizes(mat,&rbs,&cbs);
9219:     if (right) {
9220:       if (mat->cmap->n < 0) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"PetscLayout for columns not yet setup");
9221:       VecCreate(PetscObjectComm((PetscObject)mat),right);
9222:       VecSetSizes(*right,mat->cmap->n,PETSC_DETERMINE);
9223:       VecSetBlockSize(*right,cbs);
9224:       VecSetType(*right,mat->defaultvectype);
9225:       PetscLayoutReference(mat->cmap,&(*right)->map);
9226:     }
9227:     if (left) {
9228:       if (mat->rmap->n < 0) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"PetscLayout for rows not yet setup");
9229:       VecCreate(PetscObjectComm((PetscObject)mat),left);
9230:       VecSetSizes(*left,mat->rmap->n,PETSC_DETERMINE);
9231:       VecSetBlockSize(*left,rbs);
9232:       VecSetType(*left,mat->defaultvectype);
9233:       PetscLayoutReference(mat->rmap,&(*left)->map);
9234:     }
9235:   }
9236:   return(0);
9237: }

9239: /*@C
9240:    MatFactorInfoInitialize - Initializes a MatFactorInfo data structure
9241:      with default values.

9243:    Not Collective

9245:    Input Parameters:
9246: .    info - the MatFactorInfo data structure

9248:    Notes:
9249:     The solvers are generally used through the KSP and PC objects, for example
9250:           PCLU, PCILU, PCCHOLESKY, PCICC

9252:    Level: developer

9254: .seealso: MatFactorInfo

9256:     Developer Note: fortran interface is not autogenerated as the f90
9257:     interface definition cannot be generated correctly [due to MatFactorInfo]

9259: @*/

9261: PetscErrorCode MatFactorInfoInitialize(MatFactorInfo *info)
9262: {

9266:   PetscMemzero(info,sizeof(MatFactorInfo));
9267:   return(0);
9268: }

9270: /*@
9271:    MatFactorSetSchurIS - Set indices corresponding to the Schur complement you wish to have computed

9273:    Collective on Mat

9275:    Input Parameters:
9276: +  mat - the factored matrix
9277: -  is - the index set defining the Schur indices (0-based)

9279:    Notes:
9280:     Call MatFactorSolveSchurComplement() or MatFactorSolveSchurComplementTranspose() after this call to solve a Schur complement system.

9282:    You can call MatFactorGetSchurComplement() or MatFactorCreateSchurComplement() after this call.

9284:    Level: developer

9286: .seealso: MatGetFactor(), MatFactorGetSchurComplement(), MatFactorRestoreSchurComplement(), MatFactorCreateSchurComplement(), MatFactorSolveSchurComplement(),
9287:           MatFactorSolveSchurComplementTranspose(), MatFactorSolveSchurComplement()

9289: @*/
9290: PetscErrorCode MatFactorSetSchurIS(Mat mat,IS is)
9291: {
9292:   PetscErrorCode ierr,(*f)(Mat,IS);

9300:   if (!mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Only for factored matrix");
9301:   PetscObjectQueryFunction((PetscObject)mat,"MatFactorSetSchurIS_C",&f);
9302:   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");
9303:   MatDestroy(&mat->schur);
9304:   (*f)(mat,is);
9305:   if (!mat->schur) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_PLIB,"Schur complement has not been created");
9306:   return(0);
9307: }

9309: /*@
9310:   MatFactorCreateSchurComplement - Create a Schur complement matrix object using Schur data computed during the factorization step

9312:    Logically Collective on Mat

9314:    Input Parameters:
9315: +  F - the factored matrix obtained by calling MatGetFactor() from PETSc-MUMPS interface
9316: .  S - location where to return the Schur complement, can be NULL
9317: -  status - the status of the Schur complement matrix, can be NULL

9319:    Notes:
9320:    You must call MatFactorSetSchurIS() before calling this routine.

9322:    The routine provides a copy of the Schur matrix stored within the solver data structures.
9323:    The caller must destroy the object when it is no longer needed.
9324:    If MatFactorInvertSchurComplement() has been called, the routine gets back the inverse.

9326:    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)

9328:    Developer Notes:
9329:     The reason this routine exists is because the representation of the Schur complement within the factor matrix may be different than a standard PETSc
9330:    matrix representation and we normally do not want to use the time or memory to make a copy as a regular PETSc matrix.

9332:    See MatCreateSchurComplement() or MatGetSchurComplement() for ways to create virtual or approximate Schur complements.

9334:    Level: advanced

9336:    References:

9338: .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorGetSchurComplement(), MatFactorSchurStatus
9339: @*/
9340: PetscErrorCode MatFactorCreateSchurComplement(Mat F,Mat* S,MatFactorSchurStatus* status)
9341: {

9348:   if (S) {
9349:     PetscErrorCode (*f)(Mat,Mat*);

9351:     PetscObjectQueryFunction((PetscObject)F,"MatFactorCreateSchurComplement_C",&f);
9352:     if (f) {
9353:       (*f)(F,S);
9354:     } else {
9355:       MatDuplicate(F->schur,MAT_COPY_VALUES,S);
9356:     }
9357:   }
9358:   if (status) *status = F->schur_status;
9359:   return(0);
9360: }

9362: /*@
9363:   MatFactorGetSchurComplement - Gets access to a Schur complement matrix using the current Schur data within a factored matrix

9365:    Logically Collective on Mat

9367:    Input Parameters:
9368: +  F - the factored matrix obtained by calling MatGetFactor()
9369: .  *S - location where to return the Schur complement, can be NULL
9370: -  status - the status of the Schur complement matrix, can be NULL

9372:    Notes:
9373:    You must call MatFactorSetSchurIS() before calling this routine.

9375:    Schur complement mode is currently implemented for sequential matrices.
9376:    The routine returns a the Schur Complement stored within the data strutures of the solver.
9377:    If MatFactorInvertSchurComplement() has previously been called, the returned matrix is actually the inverse of the Schur complement.
9378:    The returned matrix should not be destroyed; the caller should call MatFactorRestoreSchurComplement() when the object is no longer needed.

9380:    Use MatFactorCreateSchurComplement() to create a copy of the Schur complement matrix that is within a factored matrix

9382:    See MatCreateSchurComplement() or MatGetSchurComplement() for ways to create virtual or approximate Schur complements.

9384:    Level: advanced

9386:    References:

9388: .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorRestoreSchurComplement(), MatFactorCreateSchurComplement(), MatFactorSchurStatus
9389: @*/
9390: PetscErrorCode MatFactorGetSchurComplement(Mat F,Mat* S,MatFactorSchurStatus* status)
9391: {
9396:   if (S) *S = F->schur;
9397:   if (status) *status = F->schur_status;
9398:   return(0);
9399: }

9401: /*@
9402:   MatFactorRestoreSchurComplement - Restore the Schur complement matrix object obtained from a call to MatFactorGetSchurComplement

9404:    Logically Collective on Mat

9406:    Input Parameters:
9407: +  F - the factored matrix obtained by calling MatGetFactor()
9408: .  *S - location where the Schur complement is stored
9409: -  status - the status of the Schur complement matrix (see MatFactorSchurStatus)

9411:    Notes:

9413:    Level: advanced

9415:    References:

9417: .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorRestoreSchurComplement(), MatFactorCreateSchurComplement(), MatFactorSchurStatus
9418: @*/
9419: PetscErrorCode MatFactorRestoreSchurComplement(Mat F,Mat* S,MatFactorSchurStatus status)
9420: {

9425:   if (S) {
9427:     *S = NULL;
9428:   }
9429:   F->schur_status = status;
9430:   MatFactorUpdateSchurStatus_Private(F);
9431:   return(0);
9432: }

9434: /*@
9435:   MatFactorSolveSchurComplementTranspose - Solve the transpose of the Schur complement system computed during the factorization step

9437:    Logically Collective on Mat

9439:    Input Parameters:
9440: +  F - the factored matrix obtained by calling MatGetFactor()
9441: .  rhs - location where the right hand side of the Schur complement system is stored
9442: -  sol - location where the solution of the Schur complement system has to be returned

9444:    Notes:
9445:    The sizes of the vectors should match the size of the Schur complement

9447:    Must be called after MatFactorSetSchurIS()

9449:    Level: advanced

9451:    References:

9453: .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorSolveSchurComplement()
9454: @*/
9455: PetscErrorCode MatFactorSolveSchurComplementTranspose(Mat F, Vec rhs, Vec sol)
9456: {

9468:   MatFactorFactorizeSchurComplement(F);
9469:   switch (F->schur_status) {
9470:   case MAT_FACTOR_SCHUR_FACTORED:
9471:     MatSolveTranspose(F->schur,rhs,sol);
9472:     break;
9473:   case MAT_FACTOR_SCHUR_INVERTED:
9474:     MatMultTranspose(F->schur,rhs,sol);
9475:     break;
9476:   default:
9477:     SETERRQ1(PetscObjectComm((PetscObject)F),PETSC_ERR_SUP,"Unhandled MatFactorSchurStatus %D",F->schur_status);
9478:   }
9479:   return(0);
9480: }

9482: /*@
9483:   MatFactorSolveSchurComplement - Solve the Schur complement system computed during the factorization step

9485:    Logically Collective on Mat

9487:    Input Parameters:
9488: +  F - the factored matrix obtained by calling MatGetFactor()
9489: .  rhs - location where the right hand side of the Schur complement system is stored
9490: -  sol - location where the solution of the Schur complement system has to be returned

9492:    Notes:
9493:    The sizes of the vectors should match the size of the Schur complement

9495:    Must be called after MatFactorSetSchurIS()

9497:    Level: advanced

9499:    References:

9501: .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorSolveSchurComplementTranspose()
9502: @*/
9503: PetscErrorCode MatFactorSolveSchurComplement(Mat F, Vec rhs, Vec sol)
9504: {

9516:   MatFactorFactorizeSchurComplement(F);
9517:   switch (F->schur_status) {
9518:   case MAT_FACTOR_SCHUR_FACTORED:
9519:     MatSolve(F->schur,rhs,sol);
9520:     break;
9521:   case MAT_FACTOR_SCHUR_INVERTED:
9522:     MatMult(F->schur,rhs,sol);
9523:     break;
9524:   default:
9525:     SETERRQ1(PetscObjectComm((PetscObject)F),PETSC_ERR_SUP,"Unhandled MatFactorSchurStatus %D",F->schur_status);
9526:   }
9527:   return(0);
9528: }

9530: /*@
9531:   MatFactorInvertSchurComplement - Invert the Schur complement matrix computed during the factorization step

9533:    Logically Collective on Mat

9535:    Input Parameters:
9536: .  F - the factored matrix obtained by calling MatGetFactor()

9538:    Notes:
9539:     Must be called after MatFactorSetSchurIS().

9541:    Call MatFactorGetSchurComplement() or  MatFactorCreateSchurComplement() AFTER this call to actually compute the inverse and get access to it.

9543:    Level: advanced

9545:    References:

9547: .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorGetSchurComplement(), MatFactorCreateSchurComplement()
9548: @*/
9549: PetscErrorCode MatFactorInvertSchurComplement(Mat F)
9550: {

9556:   if (F->schur_status == MAT_FACTOR_SCHUR_INVERTED) return(0);
9557:   MatFactorFactorizeSchurComplement(F);
9558:   MatFactorInvertSchurComplement_Private(F);
9559:   F->schur_status = MAT_FACTOR_SCHUR_INVERTED;
9560:   return(0);
9561: }

9563: /*@
9564:   MatFactorFactorizeSchurComplement - Factorize the Schur complement matrix computed during the factorization step

9566:    Logically Collective on Mat

9568:    Input Parameters:
9569: .  F - the factored matrix obtained by calling MatGetFactor()

9571:    Notes:
9572:     Must be called after MatFactorSetSchurIS().

9574:    Level: advanced

9576:    References:

9578: .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorInvertSchurComplement()
9579: @*/
9580: PetscErrorCode MatFactorFactorizeSchurComplement(Mat F)
9581: {

9587:   if (F->schur_status == MAT_FACTOR_SCHUR_INVERTED || F->schur_status == MAT_FACTOR_SCHUR_FACTORED) return(0);
9588:   MatFactorFactorizeSchurComplement_Private(F);
9589:   F->schur_status = MAT_FACTOR_SCHUR_FACTORED;
9590:   return(0);
9591: }

9593: /*@
9594:    MatPtAP - Creates the matrix product C = P^T * A * P

9596:    Neighbor-wise Collective on Mat

9598:    Input Parameters:
9599: +  A - the matrix
9600: .  P - the projection matrix
9601: .  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
9602: -  fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(P)), use PETSC_DEFAULT if you do not have a good estimate
9603:           if the result is a dense matrix this is irrelevant

9605:    Output Parameters:
9606: .  C - the product matrix

9608:    Notes:
9609:    C will be created and must be destroyed by the user with MatDestroy().

9611:    For matrix types without special implementation the function fallbacks to MatMatMult() followed by MatTransposeMatMult().

9613:    Level: intermediate

9615: .seealso: MatMatMult(), MatRARt()
9616: @*/
9617: PetscErrorCode MatPtAP(Mat A,Mat P,MatReuse scall,PetscReal fill,Mat *C)
9618: {

9622:   if (scall == MAT_REUSE_MATRIX) MatCheckProduct(*C,5);
9623:   if (scall == MAT_INPLACE_MATRIX) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Inplace product not supported");

9625:   if (scall == MAT_INITIAL_MATRIX) {
9626:     MatProductCreate(A,P,NULL,C);
9627:     MatProductSetType(*C,MATPRODUCT_PtAP);
9628:     MatProductSetAlgorithm(*C,"default");
9629:     MatProductSetFill(*C,fill);

9631:     (*C)->product->api_user = PETSC_TRUE;
9632:     MatProductSetFromOptions(*C);
9633:     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);
9634:     MatProductSymbolic(*C);
9635:   } else { /* scall == MAT_REUSE_MATRIX */
9636:     MatProductReplaceMats(A,P,NULL,*C);
9637:   }

9639:   MatProductNumeric(*C);
9640:   if (A->symmetric_set && A->symmetric) {
9641:     MatSetOption(*C,MAT_SYMMETRIC,PETSC_TRUE);
9642:   }
9643:   return(0);
9644: }

9646: /*@
9647:    MatRARt - Creates the matrix product C = R * A * R^T

9649:    Neighbor-wise Collective on Mat

9651:    Input Parameters:
9652: +  A - the matrix
9653: .  R - the projection matrix
9654: .  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
9655: -  fill - expected fill as ratio of nnz(C)/nnz(A), use PETSC_DEFAULT if you do not have a good estimate
9656:           if the result is a dense matrix this is irrelevant

9658:    Output Parameters:
9659: .  C - the product matrix

9661:    Notes:
9662:    C will be created and must be destroyed by the user with MatDestroy().

9664:    This routine is currently only implemented for pairs of AIJ matrices and classes
9665:    which inherit from AIJ. Due to PETSc sparse matrix block row distribution among processes,
9666:    parallel MatRARt is implemented via explicit transpose of R, which could be very expensive.
9667:    We recommend using MatPtAP().

9669:    Level: intermediate

9671: .seealso: MatMatMult(), MatPtAP()
9672: @*/
9673: PetscErrorCode MatRARt(Mat A,Mat R,MatReuse scall,PetscReal fill,Mat *C)
9674: {

9678:   if (scall == MAT_REUSE_MATRIX) MatCheckProduct(*C,5);
9679:   if (scall == MAT_INPLACE_MATRIX) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Inplace product not supported");

9681:   if (scall == MAT_INITIAL_MATRIX) {
9682:     MatProductCreate(A,R,NULL,C);
9683:     MatProductSetType(*C,MATPRODUCT_RARt);
9684:     MatProductSetAlgorithm(*C,"default");
9685:     MatProductSetFill(*C,fill);

9687:     (*C)->product->api_user = PETSC_TRUE;
9688:     MatProductSetFromOptions(*C);
9689:     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);
9690:     MatProductSymbolic(*C);
9691:   } else { /* scall == MAT_REUSE_MATRIX */
9692:     MatProductReplaceMats(A,R,NULL,*C);
9693:   }

9695:   MatProductNumeric(*C);
9696:   if (A->symmetric_set && A->symmetric) {
9697:     MatSetOption(*C,MAT_SYMMETRIC,PETSC_TRUE);
9698:   }
9699:   return(0);
9700: }

9702: static PetscErrorCode MatProduct_Private(Mat A,Mat B,MatReuse scall,PetscReal fill,MatProductType ptype, Mat *C)
9703: {

9707:   if (scall == MAT_INPLACE_MATRIX) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Inplace product not supported");

9709:   if (scall == MAT_INITIAL_MATRIX) {
9710:     PetscInfo1(A,"Calling MatProduct API with MAT_INITIAL_MATRIX and product type %s\n",MatProductTypes[ptype]);
9711:     MatProductCreate(A,B,NULL,C);
9712:     MatProductSetType(*C,ptype);
9713:     MatProductSetAlgorithm(*C,MATPRODUCTALGORITHM_DEFAULT);
9714:     MatProductSetFill(*C,fill);

9716:     (*C)->product->api_user = PETSC_TRUE;
9717:     MatProductSetFromOptions(*C);
9718:     MatProductSymbolic(*C);
9719:   } else { /* scall == MAT_REUSE_MATRIX */
9720:     Mat_Product *product = (*C)->product;

9722:     PetscInfo2(A,"Calling MatProduct API with MAT_REUSE_MATRIX %s product present and product type %s\n",product ? "with" : "without",MatProductTypes[ptype]);
9723:     if (!product) {
9724:       /* user provide the dense matrix *C without calling MatProductCreate() */
9725:       PetscBool isdense;

9727:       PetscObjectBaseTypeCompareAny((PetscObject)(*C),&isdense,MATSEQDENSE,MATMPIDENSE,"");
9728:       if (isdense) {
9729:         /* user wants to reuse an assembled dense matrix */
9730:         /* Create product -- see MatCreateProduct() */
9731:         MatProductCreate_Private(A,B,NULL,*C);
9732:         product = (*C)->product;
9733:         product->fill     = fill;
9734:         product->api_user = PETSC_TRUE;
9735:         product->clear    = PETSC_TRUE;

9737:         MatProductSetType(*C,ptype);
9738:         MatProductSetFromOptions(*C);
9739:         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);
9740:         MatProductSymbolic(*C);
9741:       } else SETERRQ(PetscObjectComm((PetscObject)(*C)),PETSC_ERR_SUP,"Call MatProductCreate() first");
9742:     } else { /* user may change input matrices A or B when REUSE */
9743:       MatProductReplaceMats(A,B,NULL,*C);
9744:     }
9745:   }
9746:   MatProductNumeric(*C);
9747:   return(0);
9748: }

9750: /*@
9751:    MatMatMult - Performs Matrix-Matrix Multiplication C=A*B.

9753:    Neighbor-wise Collective on Mat

9755:    Input Parameters:
9756: +  A - the left matrix
9757: .  B - the right matrix
9758: .  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
9759: -  fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if you do not have a good estimate
9760:           if the result is a dense matrix this is irrelevant

9762:    Output Parameters:
9763: .  C - the product matrix

9765:    Notes:
9766:    Unless scall is MAT_REUSE_MATRIX C will be created.

9768:    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
9769:    call to this function with MAT_INITIAL_MATRIX.

9771:    To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value actually needed.

9773:    If you have many matrices with the same non-zero structure to multiply, you should use MatProductCreate()/MatProductSymbolic()/MatProductReplaceMats(), and call MatProductNumeric() repeatedly.

9775:    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.

9777:    Example of Usage:
9778: .vb
9779:      MatProductCreate(A,B,NULL,&C);
9780:      MatProductSetType(C,MATPRODUCT_AB);
9781:      MatProductSymbolic(C);
9782:      MatProductNumeric(C); // compute C=A * B
9783:      MatProductReplaceMats(A1,B1,NULL,C); // compute C=A1 * B1
9784:      MatProductNumeric(C);
9785:      MatProductReplaceMats(A2,NULL,NULL,C); // compute C=A2 * B1
9786:      MatProductNumeric(C);
9787: .ve

9789:    Level: intermediate

9791: .seealso: MatTransposeMatMult(), MatMatTransposeMult(), MatPtAP(), MatProductCreate(), MatProductSymbolic(), MatProductReplaceMats(), MatProductNumeric()
9792: @*/
9793: PetscErrorCode MatMatMult(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
9794: {

9798:   MatProduct_Private(A,B,scall,fill,MATPRODUCT_AB,C);
9799:   return(0);
9800: }

9802: /*@
9803:    MatMatTransposeMult - Performs Matrix-Matrix Multiplication C=A*B^T.

9805:    Neighbor-wise Collective on Mat

9807:    Input Parameters:
9808: +  A - the left matrix
9809: .  B - the right matrix
9810: .  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
9811: -  fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if not known

9813:    Output Parameters:
9814: .  C - the product matrix

9816:    Notes:
9817:    C will be created if MAT_INITIAL_MATRIX and must be destroyed by the user with MatDestroy().

9819:    MAT_REUSE_MATRIX can only be used if the matrices A and B have the same nonzero pattern as in the previous call

9821:   To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value
9822:    actually needed.

9824:    This routine is currently only implemented for pairs of SeqAIJ matrices, for the SeqDense class,
9825:    and for pairs of MPIDense matrices.

9827:    Options Database Keys:
9828: .  -matmattransmult_mpidense_mpidense_via {allgatherv,cyclic} - Choose between algorthims for MPIDense matrices: the
9829:                                                                 first redundantly copies the transposed B matrix on each process and requiers O(log P) communication complexity;
9830:                                                                 the second never stores more than one portion of the B matrix at a time by requires O(P) communication complexity.

9832:    Level: intermediate

9834: .seealso: MatMatMult(), MatTransposeMatMult() MatPtAP()
9835: @*/
9836: PetscErrorCode MatMatTransposeMult(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
9837: {

9841:   MatProduct_Private(A,B,scall,fill,MATPRODUCT_ABt,C);
9842:   return(0);
9843: }

9845: /*@
9846:    MatTransposeMatMult - Performs Matrix-Matrix Multiplication C=A^T*B.

9848:    Neighbor-wise Collective on Mat

9850:    Input Parameters:
9851: +  A - the left matrix
9852: .  B - the right matrix
9853: .  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
9854: -  fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if not known

9856:    Output Parameters:
9857: .  C - the product matrix

9859:    Notes:
9860:    C will be created if MAT_INITIAL_MATRIX and must be destroyed by the user with MatDestroy().

9862:    MAT_REUSE_MATRIX can only be used if the matrices A and B have the same nonzero pattern as in the previous call.

9864:   To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value
9865:    actually needed.

9867:    This routine is currently implemented for pairs of AIJ matrices and pairs of SeqDense matrices and classes
9868:    which inherit from SeqAIJ.  C will be of same type as the input matrices.

9870:    Level: intermediate

9872: .seealso: MatMatMult(), MatMatTransposeMult(), MatPtAP()
9873: @*/
9874: PetscErrorCode MatTransposeMatMult(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
9875: {

9879:   MatProduct_Private(A,B,scall,fill,MATPRODUCT_AtB,C);
9880:   return(0);
9881: }

9883: /*@
9884:    MatMatMatMult - Performs Matrix-Matrix-Matrix Multiplication D=A*B*C.

9886:    Neighbor-wise Collective on Mat

9888:    Input Parameters:
9889: +  A - the left matrix
9890: .  B - the middle matrix
9891: .  C - the right matrix
9892: .  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
9893: -  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
9894:           if the result is a dense matrix this is irrelevant

9896:    Output Parameters:
9897: .  D - the product matrix

9899:    Notes:
9900:    Unless scall is MAT_REUSE_MATRIX D will be created.

9902:    MAT_REUSE_MATRIX can only be used if the matrices A, B and C have the same nonzero pattern as in the previous call

9904:    To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value
9905:    actually needed.

9907:    If you have many matrices with the same non-zero structure to multiply, you
9908:    should use MAT_REUSE_MATRIX in all calls but the first or

9910:    Level: intermediate

9912: .seealso: MatMatMult, MatPtAP()
9913: @*/
9914: PetscErrorCode MatMatMatMult(Mat A,Mat B,Mat C,MatReuse scall,PetscReal fill,Mat *D)
9915: {

9919:   if (scall == MAT_REUSE_MATRIX) MatCheckProduct(*D,6);
9920:   if (scall == MAT_INPLACE_MATRIX) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Inplace product not supported");

9922:   if (scall == MAT_INITIAL_MATRIX) {
9923:     MatProductCreate(A,B,C,D);
9924:     MatProductSetType(*D,MATPRODUCT_ABC);
9925:     MatProductSetAlgorithm(*D,"default");
9926:     MatProductSetFill(*D,fill);

9928:     (*D)->product->api_user = PETSC_TRUE;
9929:     MatProductSetFromOptions(*D);
9930:     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);
9931:     MatProductSymbolic(*D);
9932:   } else { /* user may change input matrices when REUSE */
9933:     MatProductReplaceMats(A,B,C,*D);
9934:   }
9935:   MatProductNumeric(*D);
9936:   return(0);
9937: }

9939: /*@
9940:    MatCreateRedundantMatrix - Create redundant matrices and put them into processors of subcommunicators.

9942:    Collective on Mat

9944:    Input Parameters:
9945: +  mat - the matrix
9946: .  nsubcomm - the number of subcommunicators (= number of redundant parallel or sequential matrices)
9947: .  subcomm - MPI communicator split from the communicator where mat resides in (or MPI_COMM_NULL if nsubcomm is used)
9948: -  reuse - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX

9950:    Output Parameter:
9951: .  matredundant - redundant matrix

9953:    Notes:
9954:    MAT_REUSE_MATRIX can only be used when the nonzero structure of the
9955:    original matrix has not changed from that last call to MatCreateRedundantMatrix().

9957:    This routine creates the duplicated matrices in subcommunicators; you should NOT create them before
9958:    calling it.

9960:    Level: advanced

9962: .seealso: MatDestroy()
9963: @*/
9964: PetscErrorCode MatCreateRedundantMatrix(Mat mat,PetscInt nsubcomm,MPI_Comm subcomm,MatReuse reuse,Mat *matredundant)
9965: {
9967:   MPI_Comm       comm;
9968:   PetscMPIInt    size;
9969:   PetscInt       mloc_sub,nloc_sub,rstart,rend,M=mat->rmap->N,N=mat->cmap->N,bs=mat->rmap->bs;
9970:   Mat_Redundant  *redund=NULL;
9971:   PetscSubcomm   psubcomm=NULL;
9972:   MPI_Comm       subcomm_in=subcomm;
9973:   Mat            *matseq;
9974:   IS             isrow,iscol;
9975:   PetscBool      newsubcomm=PETSC_FALSE;

9979:   if (nsubcomm && reuse == MAT_REUSE_MATRIX) {
9982:   }

9984:   MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);
9985:   if (size == 1 || nsubcomm == 1) {
9986:     if (reuse == MAT_INITIAL_MATRIX) {
9987:       MatDuplicate(mat,MAT_COPY_VALUES,matredundant);
9988:     } else {
9989:       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");
9990:       MatCopy(mat,*matredundant,SAME_NONZERO_PATTERN);
9991:     }
9992:     return(0);
9993:   }

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

9999:   PetscLogEventBegin(MAT_RedundantMat,mat,0,0,0);
10000:   if (subcomm_in == MPI_COMM_NULL && reuse == MAT_INITIAL_MATRIX) { /* get subcomm if user does not provide subcomm */
10001:     /* create psubcomm, then get subcomm */
10002:     PetscObjectGetComm((PetscObject)mat,&comm);
10003:     MPI_Comm_size(comm,&size);
10004:     if (nsubcomm < 1 || nsubcomm > size) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"nsubcomm must between 1 and %D",size);

10006:     PetscSubcommCreate(comm,&psubcomm);
10007:     PetscSubcommSetNumber(psubcomm,nsubcomm);
10008:     PetscSubcommSetType(psubcomm,PETSC_SUBCOMM_CONTIGUOUS);
10009:     PetscSubcommSetFromOptions(psubcomm);
10010:     PetscCommDuplicate(PetscSubcommChild(psubcomm),&subcomm,NULL);
10011:     newsubcomm = PETSC_TRUE;
10012:     PetscSubcommDestroy(&psubcomm);
10013:   }

10015:   /* get isrow, iscol and a local sequential matrix matseq[0] */
10016:   if (reuse == MAT_INITIAL_MATRIX) {
10017:     mloc_sub = PETSC_DECIDE;
10018:     nloc_sub = PETSC_DECIDE;
10019:     if (bs < 1) {
10020:       PetscSplitOwnership(subcomm,&mloc_sub,&M);
10021:       PetscSplitOwnership(subcomm,&nloc_sub,&N);
10022:     } else {
10023:       PetscSplitOwnershipBlock(subcomm,bs,&mloc_sub,&M);
10024:       PetscSplitOwnershipBlock(subcomm,bs,&nloc_sub,&N);
10025:     }
10026:     MPI_Scan(&mloc_sub,&rend,1,MPIU_INT,MPI_SUM,subcomm);
10027:     rstart = rend - mloc_sub;
10028:     ISCreateStride(PETSC_COMM_SELF,mloc_sub,rstart,1,&isrow);
10029:     ISCreateStride(PETSC_COMM_SELF,N,0,1,&iscol);
10030:   } else { /* reuse == MAT_REUSE_MATRIX */
10031:     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");
10032:     /* retrieve subcomm */
10033:     PetscObjectGetComm((PetscObject)(*matredundant),&subcomm);
10034:     redund = (*matredundant)->redundant;
10035:     isrow  = redund->isrow;
10036:     iscol  = redund->iscol;
10037:     matseq = redund->matseq;
10038:   }
10039:   MatCreateSubMatrices(mat,1,&isrow,&iscol,reuse,&matseq);

10041:   /* get matredundant over subcomm */
10042:   if (reuse == MAT_INITIAL_MATRIX) {
10043:     MatCreateMPIMatConcatenateSeqMat(subcomm,matseq[0],nloc_sub,reuse,matredundant);

10045:     /* create a supporting struct and attach it to C for reuse */
10046:     PetscNewLog(*matredundant,&redund);
10047:     (*matredundant)->redundant = redund;
10048:     redund->isrow              = isrow;
10049:     redund->iscol              = iscol;
10050:     redund->matseq             = matseq;
10051:     if (newsubcomm) {
10052:       redund->subcomm          = subcomm;
10053:     } else {
10054:       redund->subcomm          = MPI_COMM_NULL;
10055:     }
10056:   } else {
10057:     MatCreateMPIMatConcatenateSeqMat(subcomm,matseq[0],PETSC_DECIDE,reuse,matredundant);
10058:   }
10059:   PetscLogEventEnd(MAT_RedundantMat,mat,0,0,0);
10060:   return(0);
10061: }

10063: /*@C
10064:    MatGetMultiProcBlock - Create multiple [bjacobi] 'parallel submatrices' from
10065:    a given 'mat' object. Each submatrix can span multiple procs.

10067:    Collective on Mat

10069:    Input Parameters:
10070: +  mat - the matrix
10071: .  subcomm - the subcommunicator obtained by com_split(comm)
10072: -  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX

10074:    Output Parameter:
10075: .  subMat - 'parallel submatrices each spans a given subcomm

10077:   Notes:
10078:   The submatrix partition across processors is dictated by 'subComm' a
10079:   communicator obtained by com_split(comm). The comm_split
10080:   is not restriced to be grouped with consecutive original ranks.

10082:   Due the comm_split() usage, the parallel layout of the submatrices
10083:   map directly to the layout of the original matrix [wrt the local
10084:   row,col partitioning]. So the original 'DiagonalMat' naturally maps
10085:   into the 'DiagonalMat' of the subMat, hence it is used directly from
10086:   the subMat. However the offDiagMat looses some columns - and this is
10087:   reconstructed with MatSetValues()

10089:   Level: advanced

10091: .seealso: MatCreateSubMatrices()
10092: @*/
10093: PetscErrorCode   MatGetMultiProcBlock(Mat mat, MPI_Comm subComm, MatReuse scall,Mat *subMat)
10094: {
10096:   PetscMPIInt    commsize,subCommSize;

10099:   MPI_Comm_size(PetscObjectComm((PetscObject)mat),&commsize);
10100:   MPI_Comm_size(subComm,&subCommSize);
10101:   if (subCommSize > commsize) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"CommSize %D < SubCommZize %D",commsize,subCommSize);

10103:   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");
10104:   PetscLogEventBegin(MAT_GetMultiProcBlock,mat,0,0,0);
10105:   (*mat->ops->getmultiprocblock)(mat,subComm,scall,subMat);
10106:   PetscLogEventEnd(MAT_GetMultiProcBlock,mat,0,0,0);
10107:   return(0);
10108: }

10110: /*@
10111:    MatGetLocalSubMatrix - Gets a reference to a submatrix specified in local numbering

10113:    Not Collective

10115:    Input Parameters:
10116: +  mat - matrix to extract local submatrix from
10117: .  isrow - local row indices for submatrix
10118: -  iscol - local column indices for submatrix

10120:    Output Parameter:
10121: .  submat - the submatrix

10123:    Level: intermediate

10125:    Notes:
10126:    The submat should be returned with MatRestoreLocalSubMatrix().

10128:    Depending on the format of mat, the returned submat may not implement MatMult().  Its communicator may be
10129:    the same as mat, it may be PETSC_COMM_SELF, or some other subcomm of mat's.

10131:    The submat always implements MatSetValuesLocal().  If isrow and iscol have the same block size, then
10132:    MatSetValuesBlockedLocal() will also be implemented.

10134:    The mat must have had a ISLocalToGlobalMapping provided to it with MatSetLocalToGlobalMapping(). Note that
10135:    matrices obtained with DMCreateMatrix() generally already have the local to global mapping provided.

10137: .seealso: MatRestoreLocalSubMatrix(), MatCreateLocalRef(), MatSetLocalToGlobalMapping()
10138: @*/
10139: PetscErrorCode MatGetLocalSubMatrix(Mat mat,IS isrow,IS iscol,Mat *submat)
10140: {

10149:   if (!mat->rmap->mapping) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Matrix must have local to global mapping provided before this call");

10151:   if (mat->ops->getlocalsubmatrix) {
10152:     (*mat->ops->getlocalsubmatrix)(mat,isrow,iscol,submat);
10153:   } else {
10154:     MatCreateLocalRef(mat,isrow,iscol,submat);
10155:   }
10156:   return(0);
10157: }

10159: /*@
10160:    MatRestoreLocalSubMatrix - Restores a reference to a submatrix specified in local numbering

10162:    Not Collective

10164:    Input Parameters:
10165: +  mat - matrix to extract local submatrix from
10166: .  isrow - local row indices for submatrix
10167: .  iscol - local column indices for submatrix
10168: -  submat - the submatrix

10170:    Level: intermediate

10172: .seealso: MatGetLocalSubMatrix()
10173: @*/
10174: PetscErrorCode MatRestoreLocalSubMatrix(Mat mat,IS isrow,IS iscol,Mat *submat)
10175: {

10184:   if (*submat) {
10186:   }

10188:   if (mat->ops->restorelocalsubmatrix) {
10189:     (*mat->ops->restorelocalsubmatrix)(mat,isrow,iscol,submat);
10190:   } else {
10191:     MatDestroy(submat);
10192:   }
10193:   *submat = NULL;
10194:   return(0);
10195: }

10197: /* --------------------------------------------------------*/
10198: /*@
10199:    MatFindZeroDiagonals - Finds all the rows of a matrix that have zero or no diagonal entry in the matrix

10201:    Collective on Mat

10203:    Input Parameter:
10204: .  mat - the matrix

10206:    Output Parameter:
10207: .  is - if any rows have zero diagonals this contains the list of them

10209:    Level: developer

10211: .seealso: MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd()
10212: @*/
10213: PetscErrorCode MatFindZeroDiagonals(Mat mat,IS *is)
10214: {

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

10223:   if (!mat->ops->findzerodiagonals) {
10224:     Vec                diag;
10225:     const PetscScalar *a;
10226:     PetscInt          *rows;
10227:     PetscInt           rStart, rEnd, r, nrow = 0;

10229:     MatCreateVecs(mat, &diag, NULL);
10230:     MatGetDiagonal(mat, diag);
10231:     MatGetOwnershipRange(mat, &rStart, &rEnd);
10232:     VecGetArrayRead(diag, &a);
10233:     for (r = 0; r < rEnd-rStart; ++r) if (a[r] == 0.0) ++nrow;
10234:     PetscMalloc1(nrow, &rows);
10235:     nrow = 0;
10236:     for (r = 0; r < rEnd-rStart; ++r) if (a[r] == 0.0) rows[nrow++] = r+rStart;
10237:     VecRestoreArrayRead(diag, &a);
10238:     VecDestroy(&diag);
10239:     ISCreateGeneral(PetscObjectComm((PetscObject) mat), nrow, rows, PETSC_OWN_POINTER, is);
10240:   } else {
10241:     (*mat->ops->findzerodiagonals)(mat, is);
10242:   }
10243:   return(0);
10244: }

10246: /*@
10247:    MatFindOffBlockDiagonalEntries - Finds all the rows of a matrix that have entries outside of the main diagonal block (defined by the matrix block size)

10249:    Collective on Mat

10251:    Input Parameter:
10252: .  mat - the matrix

10254:    Output Parameter:
10255: .  is - contains the list of rows with off block diagonal entries

10257:    Level: developer

10259: .seealso: MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd()
10260: @*/
10261: PetscErrorCode MatFindOffBlockDiagonalEntries(Mat mat,IS *is)
10262: {

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

10271:   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);
10272:   (*mat->ops->findoffblockdiagonalentries)(mat,is);
10273:   return(0);
10274: }

10276: /*@C
10277:   MatInvertBlockDiagonal - Inverts the block diagonal entries.

10279:   Collective on Mat

10281:   Input Parameters:
10282: . mat - the matrix

10284:   Output Parameters:
10285: . values - the block inverses in column major order (FORTRAN-like)

10287:    Note:
10288:      The size of the blocks is determined by the block size of the matrix.

10290:    Fortran Note:
10291:      This routine is not available from Fortran.

10293:   Level: advanced

10295: .seealso: MatInvertBockDiagonalMat()
10296: @*/
10297: PetscErrorCode MatInvertBlockDiagonal(Mat mat,const PetscScalar **values)
10298: {

10303:   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
10304:   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
10305:   if (!mat->ops->invertblockdiagonal) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not supported for type %s",((PetscObject)mat)->type_name);
10306:   (*mat->ops->invertblockdiagonal)(mat,values);
10307:   return(0);
10308: }

10310: /*@C
10311:   MatInvertVariableBlockDiagonal - Inverts the point block diagonal entries.

10313:   Collective on Mat

10315:   Input Parameters:
10316: + mat - the matrix
10317: . nblocks - the number of blocks
10318: - bsizes - the size of each block

10320:   Output Parameters:
10321: . values - the block inverses in column major order (FORTRAN-like)

10323:    Note:
10324:    This routine is not available from Fortran.

10326:   Level: advanced

10328: .seealso: MatInvertBockDiagonal()
10329: @*/
10330: PetscErrorCode MatInvertVariableBlockDiagonal(Mat mat,PetscInt nblocks,const PetscInt *bsizes,PetscScalar *values)
10331: {

10336:   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
10337:   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
10338:   if (!mat->ops->invertvariableblockdiagonal) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not supported for type",((PetscObject)mat)->type_name);
10339:   (*mat->ops->invertvariableblockdiagonal)(mat,nblocks,bsizes,values);
10340:   return(0);
10341: }

10343: /*@
10344:   MatInvertBlockDiagonalMat - set matrix C to be the inverted block diagonal of matrix A

10346:   Collective on Mat

10348:   Input Parameters:
10349: . A - the matrix

10351:   Output Parameters:
10352: . C - matrix with inverted block diagonal of A.  This matrix should be created and may have its type set.

10354:   Notes: the blocksize of the matrix is used to determine the blocks on the diagonal of C

10356:   Level: advanced

10358: .seealso: MatInvertBockDiagonal()
10359: @*/
10360: PetscErrorCode MatInvertBlockDiagonalMat(Mat A,Mat C)
10361: {
10362:   PetscErrorCode     ierr;
10363:   const PetscScalar *vals;
10364:   PetscInt          *dnnz;
10365:   PetscInt           M,N,m,n,rstart,rend,bs,i,j;

10368:   MatInvertBlockDiagonal(A,&vals);
10369:   MatGetBlockSize(A,&bs);
10370:   MatGetSize(A,&M,&N);
10371:   MatGetLocalSize(A,&m,&n);
10372:   MatSetSizes(C,m,n,M,N);
10373:   MatSetBlockSize(C,bs);
10374:   PetscMalloc1(m/bs,&dnnz);
10375:   for (j = 0; j < m/bs; j++) dnnz[j] = 1;
10376:   MatXAIJSetPreallocation(C,bs,dnnz,NULL,NULL,NULL);
10377:   PetscFree(dnnz);
10378:   MatGetOwnershipRange(C,&rstart,&rend);
10379:   MatSetOption(C,MAT_ROW_ORIENTED,PETSC_FALSE);
10380:   for (i = rstart/bs; i < rend/bs; i++) {
10381:     MatSetValuesBlocked(C,1,&i,1,&i,&vals[(i-rstart/bs)*bs*bs],INSERT_VALUES);
10382:   }
10383:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
10384:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
10385:   MatSetOption(C,MAT_ROW_ORIENTED,PETSC_TRUE);
10386:   return(0);
10387: }

10389: /*@C
10390:     MatTransposeColoringDestroy - Destroys a coloring context for matrix product C=A*B^T that was created
10391:     via MatTransposeColoringCreate().

10393:     Collective on MatTransposeColoring

10395:     Input Parameter:
10396: .   c - coloring context

10398:     Level: intermediate

10400: .seealso: MatTransposeColoringCreate()
10401: @*/
10402: PetscErrorCode MatTransposeColoringDestroy(MatTransposeColoring *c)
10403: {
10404:   PetscErrorCode       ierr;
10405:   MatTransposeColoring matcolor=*c;

10408:   if (!matcolor) return(0);
10409:   if (--((PetscObject)matcolor)->refct > 0) {matcolor = NULL; return(0);}

10411:   PetscFree3(matcolor->ncolumns,matcolor->nrows,matcolor->colorforrow);
10412:   PetscFree(matcolor->rows);
10413:   PetscFree(matcolor->den2sp);
10414:   PetscFree(matcolor->colorforcol);
10415:   PetscFree(matcolor->columns);
10416:   if (matcolor->brows>0) {
10417:     PetscFree(matcolor->lstart);
10418:   }
10419:   PetscHeaderDestroy(c);
10420:   return(0);
10421: }

10423: /*@C
10424:     MatTransColoringApplySpToDen - Given a symbolic matrix product C=A*B^T for which
10425:     a MatTransposeColoring context has been created, computes a dense B^T by Apply
10426:     MatTransposeColoring to sparse B.

10428:     Collective on MatTransposeColoring

10430:     Input Parameters:
10431: +   B - sparse matrix B
10432: .   Btdense - symbolic dense matrix B^T
10433: -   coloring - coloring context created with MatTransposeColoringCreate()

10435:     Output Parameter:
10436: .   Btdense - dense matrix B^T

10438:     Level: advanced

10440:      Notes:
10441:     These are used internally for some implementations of MatRARt()

10443: .seealso: MatTransposeColoringCreate(), MatTransposeColoringDestroy(), MatTransColoringApplyDenToSp()

10445: @*/
10446: PetscErrorCode MatTransColoringApplySpToDen(MatTransposeColoring coloring,Mat B,Mat Btdense)
10447: {


10455:   if (!B->ops->transcoloringapplysptoden) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not supported for this matrix type %s",((PetscObject)B)->type_name);
10456:   (B->ops->transcoloringapplysptoden)(coloring,B,Btdense);
10457:   return(0);
10458: }

10460: /*@C
10461:     MatTransColoringApplyDenToSp - Given a symbolic matrix product Csp=A*B^T for which
10462:     a MatTransposeColoring context has been created and a dense matrix Cden=A*Btdense
10463:     in which Btdens is obtained from MatTransColoringApplySpToDen(), recover sparse matrix
10464:     Csp from Cden.

10466:     Collective on MatTransposeColoring

10468:     Input Parameters:
10469: +   coloring - coloring context created with MatTransposeColoringCreate()
10470: -   Cden - matrix product of a sparse matrix and a dense matrix Btdense

10472:     Output Parameter:
10473: .   Csp - sparse matrix

10475:     Level: advanced

10477:      Notes:
10478:     These are used internally for some implementations of MatRARt()

10480: .seealso: MatTransposeColoringCreate(), MatTransposeColoringDestroy(), MatTransColoringApplySpToDen()

10482: @*/
10483: PetscErrorCode MatTransColoringApplyDenToSp(MatTransposeColoring matcoloring,Mat Cden,Mat Csp)
10484: {


10492:   if (!Csp->ops->transcoloringapplydentosp) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not supported for this matrix type %s",((PetscObject)Csp)->type_name);
10493:   (Csp->ops->transcoloringapplydentosp)(matcoloring,Cden,Csp);
10494:   MatAssemblyBegin(Csp,MAT_FINAL_ASSEMBLY);
10495:   MatAssemblyEnd(Csp,MAT_FINAL_ASSEMBLY);
10496:   return(0);
10497: }

10499: /*@C
10500:    MatTransposeColoringCreate - Creates a matrix coloring context for matrix product C=A*B^T.

10502:    Collective on Mat

10504:    Input Parameters:
10505: +  mat - the matrix product C
10506: -  iscoloring - the coloring of the matrix; usually obtained with MatColoringCreate() or DMCreateColoring()

10508:     Output Parameter:
10509: .   color - the new coloring context

10511:     Level: intermediate

10513: .seealso: MatTransposeColoringDestroy(),  MatTransColoringApplySpToDen(),
10514:            MatTransColoringApplyDenToSp()
10515: @*/
10516: PetscErrorCode MatTransposeColoringCreate(Mat mat,ISColoring iscoloring,MatTransposeColoring *color)
10517: {
10518:   MatTransposeColoring c;
10519:   MPI_Comm             comm;
10520:   PetscErrorCode       ierr;

10523:   PetscLogEventBegin(MAT_TransposeColoringCreate,mat,0,0,0);
10524:   PetscObjectGetComm((PetscObject)mat,&comm);
10525:   PetscHeaderCreate(c,MAT_TRANSPOSECOLORING_CLASSID,"MatTransposeColoring","Matrix product C=A*B^T via coloring","Mat",comm,MatTransposeColoringDestroy,NULL);

10527:   c->ctype = iscoloring->ctype;
10528:   if (mat->ops->transposecoloringcreate) {
10529:     (*mat->ops->transposecoloringcreate)(mat,iscoloring,c);
10530:   } else SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Code not yet written for matrix type %s",((PetscObject)mat)->type_name);

10532:   *color = c;
10533:   PetscLogEventEnd(MAT_TransposeColoringCreate,mat,0,0,0);
10534:   return(0);
10535: }

10537: /*@
10538:       MatGetNonzeroState - Returns a 64 bit integer representing the current state of nonzeros in the matrix. If the
10539:         matrix has had no new nonzero locations added to the matrix since the previous call then the value will be the
10540:         same, otherwise it will be larger

10542:      Not Collective

10544:   Input Parameter:
10545: .    A  - the matrix

10547:   Output Parameter:
10548: .    state - the current state

10550:   Notes:
10551:     You can only compare states from two different calls to the SAME matrix, you cannot compare calls between
10552:          different matrices

10554:   Level: intermediate

10556: @*/
10557: PetscErrorCode MatGetNonzeroState(Mat mat,PetscObjectState *state)
10558: {
10561:   *state = mat->nonzerostate;
10562:   return(0);
10563: }

10565: /*@
10566:       MatCreateMPIMatConcatenateSeqMat - Creates a single large PETSc matrix by concatenating sequential
10567:                  matrices from each processor

10569:     Collective

10571:    Input Parameters:
10572: +    comm - the communicators the parallel matrix will live on
10573: .    seqmat - the input sequential matrices
10574: .    n - number of local columns (or PETSC_DECIDE)
10575: -    reuse - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX

10577:    Output Parameter:
10578: .    mpimat - the parallel matrix generated

10580:     Level: advanced

10582:    Notes:
10583:     The number of columns of the matrix in EACH processor MUST be the same.

10585: @*/
10586: PetscErrorCode MatCreateMPIMatConcatenateSeqMat(MPI_Comm comm,Mat seqmat,PetscInt n,MatReuse reuse,Mat *mpimat)
10587: {

10591:   if (!seqmat->ops->creatempimatconcatenateseqmat) SETERRQ1(PetscObjectComm((PetscObject)seqmat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)seqmat)->type_name);
10592:   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");

10594:   PetscLogEventBegin(MAT_Merge,seqmat,0,0,0);
10595:   (*seqmat->ops->creatempimatconcatenateseqmat)(comm,seqmat,n,reuse,mpimat);
10596:   PetscLogEventEnd(MAT_Merge,seqmat,0,0,0);
10597:   return(0);
10598: }

10600: /*@
10601:      MatSubdomainsCreateCoalesce - Creates index subdomains by coalescing adjacent
10602:                  ranks' ownership ranges.

10604:     Collective on A

10606:    Input Parameters:
10607: +    A   - the matrix to create subdomains from
10608: -    N   - requested number of subdomains

10610:    Output Parameters:
10611: +    n   - number of subdomains resulting on this rank
10612: -    iss - IS list with indices of subdomains on this rank

10614:     Level: advanced

10616:     Notes:
10617:     number of subdomains must be smaller than the communicator size
10618: @*/
10619: PetscErrorCode MatSubdomainsCreateCoalesce(Mat A,PetscInt N,PetscInt *n,IS *iss[])
10620: {
10621:   MPI_Comm        comm,subcomm;
10622:   PetscMPIInt     size,rank,color;
10623:   PetscInt        rstart,rend,k;
10624:   PetscErrorCode  ierr;

10627:   PetscObjectGetComm((PetscObject)A,&comm);
10628:   MPI_Comm_size(comm,&size);
10629:   MPI_Comm_rank(comm,&rank);
10630:   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);
10631:   *n = 1;
10632:   k = ((PetscInt)size)/N + ((PetscInt)size%N>0); /* There are up to k ranks to a color */
10633:   color = rank/k;
10634:   MPI_Comm_split(comm,color,rank,&subcomm);
10635:   PetscMalloc1(1,iss);
10636:   MatGetOwnershipRange(A,&rstart,&rend);
10637:   ISCreateStride(subcomm,rend-rstart,rstart,1,iss[0]);
10638:   MPI_Comm_free(&subcomm);
10639:   return(0);
10640: }

10642: /*@
10643:    MatGalerkin - Constructs the coarse grid problem via Galerkin projection.

10645:    If the interpolation and restriction operators are the same, uses MatPtAP.
10646:    If they are not the same, use MatMatMatMult.

10648:    Once the coarse grid problem is constructed, correct for interpolation operators
10649:    that are not of full rank, which can legitimately happen in the case of non-nested
10650:    geometric multigrid.

10652:    Input Parameters:
10653: +  restrct - restriction operator
10654: .  dA - fine grid matrix
10655: .  interpolate - interpolation operator
10656: .  reuse - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
10657: -  fill - expected fill, use PETSC_DEFAULT if you do not have a good estimate

10659:    Output Parameters:
10660: .  A - the Galerkin coarse matrix

10662:    Options Database Key:
10663: .  -pc_mg_galerkin <both,pmat,mat,none>

10665:    Level: developer

10667: .seealso: MatPtAP(), MatMatMatMult()
10668: @*/
10669: PetscErrorCode  MatGalerkin(Mat restrct, Mat dA, Mat interpolate, MatReuse reuse, PetscReal fill, Mat *A)
10670: {
10672:   IS             zerorows;
10673:   Vec            diag;

10676:   if (reuse == MAT_INPLACE_MATRIX) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Inplace product not supported");
10677:   /* Construct the coarse grid matrix */
10678:   if (interpolate == restrct) {
10679:     MatPtAP(dA,interpolate,reuse,fill,A);
10680:   } else {
10681:     MatMatMatMult(restrct,dA,interpolate,reuse,fill,A);
10682:   }

10684:   /* If the interpolation matrix is not of full rank, A will have zero rows.
10685:      This can legitimately happen in the case of non-nested geometric multigrid.
10686:      In that event, we set the rows of the matrix to the rows of the identity,
10687:      ignoring the equations (as the RHS will also be zero). */

10689:   MatFindZeroRows(*A, &zerorows);

10691:   if (zerorows != NULL) { /* if there are any zero rows */
10692:     MatCreateVecs(*A, &diag, NULL);
10693:     MatGetDiagonal(*A, diag);
10694:     VecISSet(diag, zerorows, 1.0);
10695:     MatDiagonalSet(*A, diag, INSERT_VALUES);
10696:     VecDestroy(&diag);
10697:     ISDestroy(&zerorows);
10698:   }
10699:   return(0);
10700: }

10702: /*@C
10703:     MatSetOperation - Allows user to set a matrix operation for any matrix type

10705:    Logically Collective on Mat

10707:     Input Parameters:
10708: +   mat - the matrix
10709: .   op - the name of the operation
10710: -   f - the function that provides the operation

10712:    Level: developer

10714:     Usage:
10715: $      extern PetscErrorCode usermult(Mat,Vec,Vec);
10716: $      MatCreateXXX(comm,...&A);
10717: $      MatSetOperation(A,MATOP_MULT,(void(*)(void))usermult);

10719:     Notes:
10720:     See the file include/petscmat.h for a complete list of matrix
10721:     operations, which all have the form MATOP_<OPERATION>, where
10722:     <OPERATION> is the name (in all capital letters) of the
10723:     user interface routine (e.g., MatMult() -> MATOP_MULT).

10725:     All user-provided functions (except for MATOP_DESTROY) should have the same calling
10726:     sequence as the usual matrix interface routines, since they
10727:     are intended to be accessed via the usual matrix interface
10728:     routines, e.g.,
10729: $       MatMult(Mat,Vec,Vec) -> usermult(Mat,Vec,Vec)

10731:     In particular each function MUST return an error code of 0 on success and
10732:     nonzero on failure.

10734:     This routine is distinct from MatShellSetOperation() in that it can be called on any matrix type.

10736: .seealso: MatGetOperation(), MatCreateShell(), MatShellSetContext(), MatShellSetOperation()
10737: @*/
10738: PetscErrorCode MatSetOperation(Mat mat,MatOperation op,void (*f)(void))
10739: {
10742:   if (op == MATOP_VIEW && !mat->ops->viewnative && f != (void (*)(void))(mat->ops->view)) {
10743:     mat->ops->viewnative = mat->ops->view;
10744:   }
10745:   (((void(**)(void))mat->ops)[op]) = f;
10746:   return(0);
10747: }

10749: /*@C
10750:     MatGetOperation - Gets a matrix operation for any matrix type.

10752:     Not Collective

10754:     Input Parameters:
10755: +   mat - the matrix
10756: -   op - the name of the operation

10758:     Output Parameter:
10759: .   f - the function that provides the operation

10761:     Level: developer

10763:     Usage:
10764: $      PetscErrorCode (*usermult)(Mat,Vec,Vec);
10765: $      MatGetOperation(A,MATOP_MULT,(void(**)(void))&usermult);

10767:     Notes:
10768:     See the file include/petscmat.h for a complete list of matrix
10769:     operations, which all have the form MATOP_<OPERATION>, where
10770:     <OPERATION> is the name (in all capital letters) of the
10771:     user interface routine (e.g., MatMult() -> MATOP_MULT).

10773:     This routine is distinct from MatShellGetOperation() in that it can be called on any matrix type.

10775: .seealso: MatSetOperation(), MatCreateShell(), MatShellGetContext(), MatShellGetOperation()
10776: @*/
10777: PetscErrorCode MatGetOperation(Mat mat,MatOperation op,void(**f)(void))
10778: {
10781:   *f = (((void (**)(void))mat->ops)[op]);
10782:   return(0);
10783: }

10785: /*@
10786:     MatHasOperation - Determines whether the given matrix supports the particular
10787:     operation.

10789:    Not Collective

10791:    Input Parameters:
10792: +  mat - the matrix
10793: -  op - the operation, for example, MATOP_GET_DIAGONAL

10795:    Output Parameter:
10796: .  has - either PETSC_TRUE or PETSC_FALSE

10798:    Level: advanced

10800:    Notes:
10801:    See the file include/petscmat.h for a complete list of matrix
10802:    operations, which all have the form MATOP_<OPERATION>, where
10803:    <OPERATION> is the name (in all capital letters) of the
10804:    user-level routine.  E.g., MatNorm() -> MATOP_NORM.

10806: .seealso: MatCreateShell()
10807: @*/
10808: PetscErrorCode MatHasOperation(Mat mat,MatOperation op,PetscBool *has)
10809: {

10814:   /* symbolic product can be set before matrix type */
10817:   if (mat->ops->hasoperation) {
10818:     (*mat->ops->hasoperation)(mat,op,has);
10819:   } else {
10820:     if (((void**)mat->ops)[op]) *has =  PETSC_TRUE;
10821:     else {
10822:       *has = PETSC_FALSE;
10823:       if (op == MATOP_CREATE_SUBMATRIX) {
10824:         PetscMPIInt size;

10826:         MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);
10827:         if (size == 1) {
10828:           MatHasOperation(mat,MATOP_CREATE_SUBMATRICES,has);
10829:         }
10830:       }
10831:     }
10832:   }
10833:   return(0);
10834: }

10836: /*@
10837:     MatHasCongruentLayouts - Determines whether the rows and columns layouts
10838:     of the matrix are congruent

10840:    Collective on mat

10842:    Input Parameters:
10843: .  mat - the matrix

10845:    Output Parameter:
10846: .  cong - either PETSC_TRUE or PETSC_FALSE

10848:    Level: beginner

10850:    Notes:

10852: .seealso: MatCreate(), MatSetSizes()
10853: @*/
10854: PetscErrorCode MatHasCongruentLayouts(Mat mat,PetscBool *cong)
10855: {

10862:   if (!mat->rmap || !mat->cmap) {
10863:     *cong = mat->rmap == mat->cmap ? PETSC_TRUE : PETSC_FALSE;
10864:     return(0);
10865:   }
10866:   if (mat->congruentlayouts == PETSC_DECIDE) { /* first time we compare rows and cols layouts */
10867:     PetscLayoutCompare(mat->rmap,mat->cmap,cong);
10868:     if (*cong) mat->congruentlayouts = 1;
10869:     else       mat->congruentlayouts = 0;
10870:   } else *cong = mat->congruentlayouts ? PETSC_TRUE : PETSC_FALSE;
10871:   return(0);
10872: }

10874: PetscErrorCode MatSetInf(Mat A)
10875: {

10879:   if (!A->ops->setinf) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"No support for this operation for this matrix type");
10880:   (*A->ops->setinf)(A);
10881:   return(0);
10882: }