Actual source code: matrix.c

petsc-3.11.0 2019-03-29
Report Typos and Errors

  2: /*
  3:    This is where the abstract matrix operations are defined
  4: */

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

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

 16: PetscLogEvent MAT_Mult, MAT_Mults, MAT_MultConstrained, MAT_MultAdd, MAT_MultTranspose;
 17: PetscLogEvent MAT_MultTransposeConstrained, MAT_MultTransposeAdd, MAT_Solve, MAT_Solves, MAT_SolveAdd, MAT_SolveTranspose, MAT_MatSolve,MAT_MatTrSolve;
 18: PetscLogEvent MAT_SolveTransposeAdd, MAT_SOR, MAT_ForwardSolve, MAT_BackwardSolve, MAT_LUFactor, MAT_LUFactorSymbolic;
 19: PetscLogEvent MAT_LUFactorNumeric, MAT_CholeskyFactor, MAT_CholeskyFactorSymbolic, MAT_CholeskyFactorNumeric, MAT_ILUFactor;
 20: PetscLogEvent MAT_ILUFactorSymbolic, MAT_ICCFactorSymbolic, MAT_Copy, MAT_Convert, MAT_Scale, MAT_AssemblyBegin;
 21: PetscLogEvent MAT_AssemblyEnd, MAT_SetValues, MAT_GetValues, MAT_GetRow, MAT_GetRowIJ, MAT_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_Transpose_SeqAIJ, MAT_GetBrowsOfAcols;
 32: PetscLogEvent MAT_GetBrowsOfAocols, MAT_Getlocalmat, MAT_Getlocalmatcondensed, MAT_Seqstompi, MAT_Seqstompinum, MAT_Seqstompisym;
 33: PetscLogEvent MAT_Applypapt, MAT_Applypapt_numeric, MAT_Applypapt_symbolic, MAT_GetSequentialNonzeroStructure;
 34: PetscLogEvent MAT_GetMultiProcBlock;
 35: PetscLogEvent MAT_CUSPARSECopyToGPU, MAT_SetValuesBatch;
 36: PetscLogEvent MAT_ViennaCLCopyToGPU;
 37: PetscLogEvent MAT_Merge,MAT_Residual,MAT_SetRandom;
 38: PetscLogEvent MATCOLORING_Apply,MATCOLORING_Comm,MATCOLORING_Local,MATCOLORING_ISCreate,MATCOLORING_SetUp,MATCOLORING_Weights;

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

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

 45:    Logically Collective on Mat

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

 52:    Output Parameter:
 53: .  x  - the matrix

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

 62:    Level: intermediate

 64:    Concepts: matrix^setting to random
 65:    Concepts: random^matrix

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


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

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

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

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

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

102:    Logically Collective on Mat

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

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

112:    Level: advanced

114:    Notes:
115:     This routine does not work for factorizations done with external packages.
116:    This routine should only be called if MatGetFactorError() returns a value of MAT_FACTOR_NUMERIC_ZEROPIVOT

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

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

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

134:    Logically Collective on Mat

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

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

142:    Level: advanced

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

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

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

160:    Logically Collective on Mat

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

165:    Level: developer

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

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

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

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

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

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

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

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

240:   Level: intermediate

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

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

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

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

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

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

273:   Level: intermediate

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


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

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

302:    Not Collective

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

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

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

315:    Level: advanced

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

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

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

342:    Collective on Mat

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

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

350:    Level: advanced

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

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

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

369:    Logically Collective on Mat

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

374:    Level: advanced


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

386:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
387:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
388:   if (!mat->ops->realpart) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
389:   MatCheckPreallocated(mat,1);
390:   (*mat->ops->realpart)(mat);
391: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
392:   if (mat->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED) {
393:     mat->valid_GPU_matrix = PETSC_OFFLOAD_CPU;
394:   }
395: #endif
396:   return(0);
397: }

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

402:    Collective on Mat

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

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

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

414:    Level: advanced

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

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


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

439:    Logically Collective on Mat

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

444:    Level: advanced


447: .seealso: MatRealPart()
448: @*/
449: PetscErrorCode MatImaginaryPart(Mat mat)
450: {

456:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
457:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
458:   if (!mat->ops->imaginarypart) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
459:   MatCheckPreallocated(mat,1);
460:   (*mat->ops->imaginarypart)(mat);
461: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
462:   if (mat->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED) {
463:     mat->valid_GPU_matrix = PETSC_OFFLOAD_CPU;
464:   }
465: #endif
466:   return(0);
467: }

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

472:    Not Collective

474:    Input Parameter:
475: .  mat - the matrix

477:    Output Parameters:
478: +  missing - is any diagonal missing
479: -  dd - first diagonal entry that is missing (optional) on this process

481:    Level: advanced


484: .seealso: MatRealPart()
485: @*/
486: PetscErrorCode MatMissingDiagonal(Mat mat,PetscBool *missing,PetscInt *dd)
487: {

493:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
494:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
495:   if (!mat->ops->missingdiagonal) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
496:   (*mat->ops->missingdiagonal)(mat,missing,dd);
497:   return(0);
498: }

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

505:    Not Collective

507:    Input Parameters:
508: +  mat - the matrix
509: -  row - the row to get

511:    Output Parameters:
512: +  ncols -  if not NULL, the number of nonzeros in the row
513: .  cols - if not NULL, the column numbers
514: -  vals - if not NULL, the values

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

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

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

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

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

538:    Fortran Notes:
539:    The calling sequence from Fortran is
540: .vb
541:    MatGetRow(matrix,row,ncols,cols,values,ierr)
542:          Mat     matrix (input)
543:          integer row    (input)
544:          integer ncols  (output)
545:          integer cols(maxcols) (output)
546:          double precision (or double complex) values(maxcols) output
547: .ve
548:    where maxcols >= maximum nonzeros in any row of the matrix.


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

555:    Level: advanced

557:    Concepts: matrices^row access

559: .seealso: MatRestoreRow(), MatSetValues(), MatGetValues(), MatCreateSubMatrices(), MatGetDiagonal()
560: @*/
561: PetscErrorCode MatGetRow(Mat mat,PetscInt row,PetscInt *ncols,const PetscInt *cols[],const PetscScalar *vals[])
562: {
564:   PetscInt       incols;

569:   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
570:   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
571:   if (!mat->ops->getrow) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
572:   MatCheckPreallocated(mat,1);
573:   PetscLogEventBegin(MAT_GetRow,mat,0,0,0);
574:   (*mat->ops->getrow)(mat,row,&incols,(PetscInt**)cols,(PetscScalar**)vals);
575:   if (ncols) *ncols = incols;
576:   PetscLogEventEnd(MAT_GetRow,mat,0,0,0);
577:   return(0);
578: }

580: /*@
581:    MatConjugate - replaces the matrix values with their complex conjugates

583:    Logically Collective on Mat

585:    Input Parameters:
586: .  mat - the matrix

588:    Level: advanced

590: .seealso:  VecConjugate()
591: @*/
592: PetscErrorCode MatConjugate(Mat mat)
593: {
594: #if defined(PETSC_USE_COMPLEX)

599:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
600:   if (!mat->ops->conjugate) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Not provided for this matrix format, send email to petsc-maint@mcs.anl.gov");
601:   (*mat->ops->conjugate)(mat);
602: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
603:   if (mat->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED) {
604:     mat->valid_GPU_matrix = PETSC_OFFLOAD_CPU;
605:   }
606: #endif
607:   return(0);
608: #else
609:   return 0;
610: #endif
611: }

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

616:    Not Collective

618:    Input Parameters:
619: +  mat - the matrix
620: .  row - the row to get
621: .  ncols, cols - the number of nonzeros and their columns
622: -  vals - if nonzero the column values

624:    Notes:
625:    This routine should be called after you have finished examining the entries.

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

631:    Fortran Notes:
632:    The calling sequence from Fortran is
633: .vb
634:    MatRestoreRow(matrix,row,ncols,cols,values,ierr)
635:       Mat     matrix (input)
636:       integer row    (input)
637:       integer ncols  (output)
638:       integer cols(maxcols) (output)
639:       double precision (or double complex) values(maxcols) output
640: .ve
641:    Where maxcols >= maximum nonzeros in any row of the matrix.

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

646:    Level: advanced

648: .seealso:  MatGetRow()
649: @*/
650: PetscErrorCode MatRestoreRow(Mat mat,PetscInt row,PetscInt *ncols,const PetscInt *cols[],const PetscScalar *vals[])
651: {

657:   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
658:   if (!mat->ops->restorerow) return(0);
659:   (*mat->ops->restorerow)(mat,row,ncols,(PetscInt **)cols,(PetscScalar **)vals);
660:   if (ncols) *ncols = 0;
661:   if (cols)  *cols = NULL;
662:   if (vals)  *vals = NULL;
663:   return(0);
664: }

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

670:    Not Collective

672:    Input Parameters:
673: +  mat - the matrix

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

678:    Level: advanced

680:    Concepts: matrices^row access

682: .seealso: MatRestoreRowRowUpperTriangular()
683: @*/
684: PetscErrorCode MatGetRowUpperTriangular(Mat mat)
685: {

691:   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
692:   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
693:   if (!mat->ops->getrowuppertriangular) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
694:   MatCheckPreallocated(mat,1);
695:   (*mat->ops->getrowuppertriangular)(mat);
696:   return(0);
697: }

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

702:    Not Collective

704:    Input Parameters:
705: +  mat - the matrix

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


711:    Level: advanced

713: .seealso:  MatGetRowUpperTriangular()
714: @*/
715: PetscErrorCode MatRestoreRowUpperTriangular(Mat mat)
716: {

721:   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
722:   if (!mat->ops->restorerowuppertriangular) return(0);
723:   (*mat->ops->restorerowuppertriangular)(mat);
724:   return(0);
725: }

727: /*@C
728:    MatSetOptionsPrefix - Sets the prefix used for searching for all
729:    Mat options in the database.

731:    Logically Collective on Mat

733:    Input Parameter:
734: +  A - the Mat context
735: -  prefix - the prefix to prepend to all option names

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

741:    Level: advanced

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

745: .seealso: MatSetFromOptions()
746: @*/
747: PetscErrorCode MatSetOptionsPrefix(Mat A,const char prefix[])
748: {

753:   PetscObjectSetOptionsPrefix((PetscObject)A,prefix);
754:   return(0);
755: }

757: /*@C
758:    MatAppendOptionsPrefix - Appends to the prefix used for searching for all
759:    Mat options in the database.

761:    Logically Collective on Mat

763:    Input Parameters:
764: +  A - the Mat context
765: -  prefix - the prefix to prepend to all option names

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

771:    Level: advanced

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

775: .seealso: MatGetOptionsPrefix()
776: @*/
777: PetscErrorCode MatAppendOptionsPrefix(Mat A,const char prefix[])
778: {

783:   PetscObjectAppendOptionsPrefix((PetscObject)A,prefix);
784:   return(0);
785: }

787: /*@C
788:    MatGetOptionsPrefix - Sets the prefix used for searching for all
789:    Mat options in the database.

791:    Not Collective

793:    Input Parameter:
794: .  A - the Mat context

796:    Output Parameter:
797: .  prefix - pointer to the prefix string used

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

803:    Level: advanced

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

807: .seealso: MatAppendOptionsPrefix()
808: @*/
809: PetscErrorCode MatGetOptionsPrefix(Mat A,const char *prefix[])
810: {

815:   PetscObjectGetOptionsPrefix((PetscObject)A,prefix);
816:   return(0);
817: }

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

822:    Collective on Mat

824:    Input Parameters:
825: .  A - the Mat context

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

831:    Level: beginner

833: .keywords: Mat, ResetPreallocation

835: .seealso: MatSeqAIJSetPreallocation(), MatMPIAIJSetPreallocation(), MatXAIJSetPreallocation()
836: @*/
837: PetscErrorCode MatResetPreallocation(Mat A)
838: {

844:   PetscUseMethod(A,"MatResetPreallocation_C",(Mat),(A));
845:   return(0);
846: }


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

852:    Collective on Mat

854:    Input Parameters:
855: .  A - the Mat context

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

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

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

864:    Level: beginner

866: .keywords: Mat, setup

868: .seealso: MatCreate(), MatDestroy()
869: @*/
870: PetscErrorCode MatSetUp(Mat A)
871: {
872:   PetscMPIInt    size;

877:   if (!((PetscObject)A)->type_name) {
878:     MPI_Comm_size(PetscObjectComm((PetscObject)A), &size);
879:     if (size == 1) {
880:       MatSetType(A, MATSEQAIJ);
881:     } else {
882:       MatSetType(A, MATMPIAIJ);
883:     }
884:   }
885:   if (!A->preallocated && A->ops->setup) {
886:     PetscInfo(A,"Warning not preallocating matrix storage\n");
887:     (*A->ops->setup)(A);
888:   }
889:   PetscLayoutSetUp(A->rmap);
890:   PetscLayoutSetUp(A->cmap);
891:   A->preallocated = PETSC_TRUE;
892:   return(0);
893: }

895: #if defined(PETSC_HAVE_SAWS)
896:  #include <petscviewersaws.h>
897: #endif
898: /*@C
899:    MatView - Visualizes a matrix object.

901:    Collective on Mat

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

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

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

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

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

954:    Notes:
955:     See the manual page for MatLoad() for the exact format of the binary file when the binary
956:       viewer is used.

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

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

967:    Concepts: matrices^viewing
968:    Concepts: matrices^plotting
969:    Concepts: matrices^printing

971: .seealso: PetscViewerPushFormat(), PetscViewerASCIIOpen(), PetscViewerDrawOpen(),
972:           PetscViewerSocketOpen(), PetscViewerBinaryOpen(), MatLoad()
973: @*/
974: PetscErrorCode MatView(Mat mat,PetscViewer viewer)
975: {
976:   PetscErrorCode    ierr;
977:   PetscInt          rows,cols,rbs,cbs;
978:   PetscBool         iascii,ibinary;
979:   PetscViewerFormat format;
980:   PetscMPIInt       size;
981: #if defined(PETSC_HAVE_SAWS)
982:   PetscBool         issaws;
983: #endif

988:   if (!viewer) {
989:     PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)mat),&viewer);
990:   }
993:   MatCheckPreallocated(mat,1);
994:   PetscViewerGetFormat(viewer,&format);
995:   MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);
996:   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) return(0);
997:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&ibinary);
998:   if (ibinary) {
999:     PetscBool mpiio;
1000:     PetscViewerBinaryGetUseMPIIO(viewer,&mpiio);
1001:     if (mpiio) SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_SUP,"PETSc matrix viewers do not support using MPI-IO, turn off that flag");
1002:   }

1004:   PetscLogEventBegin(MAT_View,mat,viewer,0,0);
1005:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
1006:   if ((!iascii || (format != PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL)) && mat->factortype) {
1007:     SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"No viewers for factored matrix except ASCII info or info_detailed");
1008:   }

1010: #if defined(PETSC_HAVE_SAWS)
1011:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);
1012: #endif
1013:   if (iascii) {
1014:     if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ORDER,"Must call MatAssemblyBegin/End() before viewing matrix");
1015:     PetscObjectPrintClassNamePrefixType((PetscObject)mat,viewer);
1016:     if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
1017:       MatNullSpace nullsp,transnullsp;

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

1050:     PetscObjectName((PetscObject)mat);
1051:     MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
1052:     if (!((PetscObject)mat)->amsmem && !rank) {
1053:       PetscObjectViewSAWs((PetscObject)mat,viewer);
1054:     }
1055: #endif
1056:   }
1057:   if ((format == PETSC_VIEWER_NATIVE || format == PETSC_VIEWER_LOAD_BALANCE) && mat->ops->viewnative) {
1058:     PetscViewerASCIIPushTab(viewer);
1059:     (*mat->ops->viewnative)(mat,viewer);
1060:     PetscViewerASCIIPopTab(viewer);
1061:   } else if (mat->ops->view) {
1062:     PetscViewerASCIIPushTab(viewer);
1063:     (*mat->ops->view)(mat,viewer);
1064:     PetscViewerASCIIPopTab(viewer);
1065:   }
1066:   if (iascii) {
1067:     PetscViewerGetFormat(viewer,&format);
1068:     if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
1069:       PetscViewerASCIIPopTab(viewer);
1070:     }
1071:   }
1072:   PetscLogEventEnd(MAT_View,mat,viewer,0,0);
1073:   return(0);
1074: }

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

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

1095:    Collective on PetscViewer

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

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

1107:    Level: beginner

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1193:    Corresponding MatView() is not yet implemented.

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

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

1204: .keywords: matrix, load, binary, input, HDF5

1206: .seealso: PetscViewerBinaryOpen(), PetscViewerSetType(), PetscViewerHDF5SetAIJNames(), MatView(), VecLoad()

1208:  @*/
1209: PetscErrorCode MatLoad(Mat newmat,PetscViewer viewer)
1210: {
1212:   PetscBool      flg;


1218:   if (!((PetscObject)newmat)->type_name) {
1219:     MatSetType(newmat,MATAIJ);
1220:   }

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

1234:   if (!newmat->ops->load) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"MatLoad is not supported for type");
1235:   PetscLogEventBegin(MAT_Load,viewer,0,0,0);
1236:   (*newmat->ops->load)(newmat,viewer);
1237:   PetscLogEventEnd(MAT_Load,viewer,0,0,0);
1238:   return(0);
1239: }

1241: PetscErrorCode MatDestroy_Redundant(Mat_Redundant **redundant)
1242: {
1244:   Mat_Redundant  *redund = *redundant;
1245:   PetscInt       i;

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

1264:     if (redund->subcomm) {
1265:       PetscCommDestroy(&redund->subcomm);
1266:     }
1267:     PetscFree(redund);
1268:   }
1269:   return(0);
1270: }

1272: /*@
1273:    MatDestroy - Frees space taken by a matrix.

1275:    Collective on Mat

1277:    Input Parameter:
1278: .  A - the matrix

1280:    Level: beginner

1282: @*/
1283: PetscErrorCode MatDestroy(Mat *A)
1284: {

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

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

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

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

1317:    Not Collective

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

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

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

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

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

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

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

1350:    Level: beginner

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

1356:    Concepts: matrices^putting entries in

1358: .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(),
1359:           InsertMode, INSERT_VALUES, ADD_VALUES
1360: @*/
1361: PetscErrorCode MatSetValues(Mat mat,PetscInt m,const PetscInt idxm[],PetscInt n,const PetscInt idxn[],const PetscScalar v[],InsertMode addv)
1362: {
1364: #if defined(PETSC_USE_DEBUG)
1365:   PetscInt       i,j;
1366: #endif

1371:   if (!m || !n) return(0); /* no values to insert */
1375:   MatCheckPreallocated(mat,1);
1376:   if (mat->insertmode == NOT_SET_VALUES) {
1377:     mat->insertmode = addv;
1378:   }
1379: #if defined(PETSC_USE_DEBUG)
1380:   else if (mat->insertmode != addv) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values");
1381:   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1382:   if (!mat->ops->setvalues) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);

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

1396:   if (mat->assembled) {
1397:     mat->was_assembled = PETSC_TRUE;
1398:     mat->assembled     = PETSC_FALSE;
1399:   }
1400:   PetscLogEventBegin(MAT_SetValues,mat,0,0,0);
1401:   (*mat->ops->setvalues)(mat,m,idxm,n,idxn,v,addv);
1402:   PetscLogEventEnd(MAT_SetValues,mat,0,0,0);
1403: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
1404:   if (mat->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED) {
1405:     mat->valid_GPU_matrix = PETSC_OFFLOAD_CPU;
1406:   }
1407: #endif
1408:   return(0);
1409: }


1412: /*@
1413:    MatSetValuesRowLocal - Inserts a row (block row for BAIJ matrices) of nonzero
1414:         values into a matrix

1416:    Not Collective

1418:    Input Parameters:
1419: +  mat - the matrix
1420: .  row - the (block) row to set
1421: -  v - a logically two-dimensional array of values

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

1426:    All the nonzeros in the row must be provided

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

1430:    The row must belong to this process

1432:    Level: intermediate

1434:    Concepts: matrices^putting entries in

1436: .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(),
1437:           InsertMode, INSERT_VALUES, ADD_VALUES, MatSetValues(), MatSetValuesRow(), MatSetLocalToGlobalMapping()
1438: @*/
1439: PetscErrorCode MatSetValuesRowLocal(Mat mat,PetscInt row,const PetscScalar v[])
1440: {
1442:   PetscInt       globalrow;

1448:   ISLocalToGlobalMappingApply(mat->rmap->mapping,1,&row,&globalrow);
1449:   MatSetValuesRow(mat,globalrow,v);
1450: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
1451:   if (mat->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED) {
1452:     mat->valid_GPU_matrix = PETSC_OFFLOAD_CPU;
1453:   }
1454: #endif
1455:   return(0);
1456: }

1458: /*@
1459:    MatSetValuesRow - Inserts a row (block row for BAIJ matrices) of nonzero
1460:         values into a matrix

1462:    Not Collective

1464:    Input Parameters:
1465: +  mat - the matrix
1466: .  row - the (block) row to set
1467: -  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

1469:    Notes:
1470:    The values, v, are column-oriented for the block version.

1472:    All the nonzeros in the row must be provided

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

1476:    The row must belong to this process

1478:    Level: advanced

1480:    Concepts: matrices^putting entries in

1482: .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(),
1483:           InsertMode, INSERT_VALUES, ADD_VALUES, MatSetValues()
1484: @*/
1485: PetscErrorCode MatSetValuesRow(Mat mat,PetscInt row,const PetscScalar v[])
1486: {

1492:   MatCheckPreallocated(mat,1);
1494: #if defined(PETSC_USE_DEBUG)
1495:   if (mat->insertmode == ADD_VALUES) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add and insert values");
1496:   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1497: #endif
1498:   mat->insertmode = INSERT_VALUES;

1500:   if (mat->assembled) {
1501:     mat->was_assembled = PETSC_TRUE;
1502:     mat->assembled     = PETSC_FALSE;
1503:   }
1504:   PetscLogEventBegin(MAT_SetValues,mat,0,0,0);
1505:   if (!mat->ops->setvaluesrow) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
1506:   (*mat->ops->setvaluesrow)(mat,row,v);
1507:   PetscLogEventEnd(MAT_SetValues,mat,0,0,0);
1508: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
1509:   if (mat->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED) {
1510:     mat->valid_GPU_matrix = PETSC_OFFLOAD_CPU;
1511:   }
1512: #endif
1513:   return(0);
1514: }

1516: /*@
1517:    MatSetValuesStencil - Inserts or adds a block of values into a matrix.
1518:      Using structured grid indexing

1520:    Not Collective

1522:    Input Parameters:
1523: +  mat - the matrix
1524: .  m - number of rows being entered
1525: .  idxm - grid coordinates (and component number when dof > 1) for matrix rows being entered
1526: .  n - number of columns being entered
1527: .  idxn - grid coordinates (and component number when dof > 1) for matrix columns being entered
1528: .  v - a logically two-dimensional array of values
1529: -  addv - either ADD_VALUES or INSERT_VALUES, where
1530:    ADD_VALUES adds values to any existing entries, and
1531:    INSERT_VALUES replaces existing entries with new values

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

1536:    Calls to MatSetValuesStencil() with the INSERT_VALUES and ADD_VALUES
1537:    options cannot be mixed without intervening calls to the assembly
1538:    routines.

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

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

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

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

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

1556:    In Fortran idxm and idxn should be declared as
1557: $     MatStencil idxm(4,m),idxn(4,n)
1558:    and the values inserted using
1559: $    idxm(MatStencil_i,1) = i
1560: $    idxm(MatStencil_j,1) = j
1561: $    idxm(MatStencil_k,1) = k
1562: $    idxm(MatStencil_c,1) = c
1563:    etc

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

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

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

1576:    Efficiency Alert:
1577:    The routine MatSetValuesBlockedStencil() may offer much better efficiency
1578:    for users of block sparse formats (MATSEQBAIJ and MATMPIBAIJ).

1580:    Level: beginner

1582:    Concepts: matrices^putting entries in

1584: .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal()
1585:           MatSetValues(), MatSetValuesBlockedStencil(), MatSetStencil(), DMCreateMatrix(), DMDAVecGetArray(), MatStencil
1586: @*/
1587: PetscErrorCode MatSetValuesStencil(Mat mat,PetscInt m,const MatStencil idxm[],PetscInt n,const MatStencil idxn[],const PetscScalar v[],InsertMode addv)
1588: {
1590:   PetscInt       buf[8192],*bufm=0,*bufn=0,*jdxm,*jdxn;
1591:   PetscInt       j,i,dim = mat->stencil.dim,*dims = mat->stencil.dims+1,tmp;
1592:   PetscInt       *starts = mat->stencil.starts,*dxm = (PetscInt*)idxm,*dxn = (PetscInt*)idxn,sdim = dim - (1 - (PetscInt)mat->stencil.noc);

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

1602:   if ((m+n) <= (PetscInt)(sizeof(buf)/sizeof(PetscInt))) {
1603:     jdxm = buf; jdxn = buf+m;
1604:   } else {
1605:     PetscMalloc2(m,&bufm,n,&bufn);
1606:     jdxm = bufm; jdxn = bufn;
1607:   }
1608:   for (i=0; i<m; i++) {
1609:     for (j=0; j<3-sdim; j++) dxm++;
1610:     tmp = *dxm++ - starts[0];
1611:     for (j=0; j<dim-1; j++) {
1612:       if ((*dxm++ - starts[j+1]) < 0 || tmp < 0) tmp = -1;
1613:       else                                       tmp = tmp*dims[j] + *(dxm-1) - starts[j+1];
1614:     }
1615:     if (mat->stencil.noc) dxm++;
1616:     jdxm[i] = tmp;
1617:   }
1618:   for (i=0; i<n; i++) {
1619:     for (j=0; j<3-sdim; j++) dxn++;
1620:     tmp = *dxn++ - starts[0];
1621:     for (j=0; j<dim-1; j++) {
1622:       if ((*dxn++ - starts[j+1]) < 0 || tmp < 0) tmp = -1;
1623:       else                                       tmp = tmp*dims[j] + *(dxn-1) - starts[j+1];
1624:     }
1625:     if (mat->stencil.noc) dxn++;
1626:     jdxn[i] = tmp;
1627:   }
1628:   MatSetValuesLocal(mat,m,jdxm,n,jdxn,v,addv);
1629:   PetscFree2(bufm,bufn);
1630:   return(0);
1631: }

1633: /*@
1634:    MatSetValuesBlockedStencil - Inserts or adds a block of values into a matrix.
1635:      Using structured grid indexing

1637:    Not Collective

1639:    Input Parameters:
1640: +  mat - the matrix
1641: .  m - number of rows being entered
1642: .  idxm - grid coordinates for matrix rows being entered
1643: .  n - number of columns being entered
1644: .  idxn - grid coordinates for matrix columns being entered
1645: .  v - a logically two-dimensional array of values
1646: -  addv - either ADD_VALUES or INSERT_VALUES, where
1647:    ADD_VALUES adds values to any existing entries, and
1648:    INSERT_VALUES replaces existing entries with new values

1650:    Notes:
1651:    By default the values, v, are row-oriented and unsorted.
1652:    See MatSetOption() for other options.

1654:    Calls to MatSetValuesBlockedStencil() with the INSERT_VALUES and ADD_VALUES
1655:    options cannot be mixed without intervening calls to the assembly
1656:    routines.

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

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

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

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

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

1674:    In Fortran idxm and idxn should be declared as
1675: $     MatStencil idxm(4,m),idxn(4,n)
1676:    and the values inserted using
1677: $    idxm(MatStencil_i,1) = i
1678: $    idxm(MatStencil_j,1) = j
1679: $    idxm(MatStencil_k,1) = k
1680:    etc

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

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

1690:    Level: beginner

1692:    Concepts: matrices^putting entries in

1694: .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal()
1695:           MatSetValues(), MatSetValuesStencil(), MatSetStencil(), DMCreateMatrix(), DMDAVecGetArray(), MatStencil,
1696:           MatSetBlockSize(), MatSetLocalToGlobalMapping()
1697: @*/
1698: PetscErrorCode MatSetValuesBlockedStencil(Mat mat,PetscInt m,const MatStencil idxm[],PetscInt n,const MatStencil idxn[],const PetscScalar v[],InsertMode addv)
1699: {
1701:   PetscInt       buf[8192],*bufm=0,*bufn=0,*jdxm,*jdxn;
1702:   PetscInt       j,i,dim = mat->stencil.dim,*dims = mat->stencil.dims+1,tmp;
1703:   PetscInt       *starts = mat->stencil.starts,*dxm = (PetscInt*)idxm,*dxn = (PetscInt*)idxn,sdim = dim - (1 - (PetscInt)mat->stencil.noc);

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

1713:   if ((m+n) <= (PetscInt)(sizeof(buf)/sizeof(PetscInt))) {
1714:     jdxm = buf; jdxn = buf+m;
1715:   } else {
1716:     PetscMalloc2(m,&bufm,n,&bufn);
1717:     jdxm = bufm; jdxn = bufn;
1718:   }
1719:   for (i=0; i<m; i++) {
1720:     for (j=0; j<3-sdim; j++) dxm++;
1721:     tmp = *dxm++ - starts[0];
1722:     for (j=0; j<sdim-1; j++) {
1723:       if ((*dxm++ - starts[j+1]) < 0 || tmp < 0) tmp = -1;
1724:       else                                       tmp = tmp*dims[j] + *(dxm-1) - starts[j+1];
1725:     }
1726:     dxm++;
1727:     jdxm[i] = tmp;
1728:   }
1729:   for (i=0; i<n; i++) {
1730:     for (j=0; j<3-sdim; j++) dxn++;
1731:     tmp = *dxn++ - starts[0];
1732:     for (j=0; j<sdim-1; j++) {
1733:       if ((*dxn++ - starts[j+1]) < 0 || tmp < 0) tmp = -1;
1734:       else                                       tmp = tmp*dims[j] + *(dxn-1) - starts[j+1];
1735:     }
1736:     dxn++;
1737:     jdxn[i] = tmp;
1738:   }
1739:   MatSetValuesBlockedLocal(mat,m,jdxm,n,jdxn,v,addv);
1740:   PetscFree2(bufm,bufn);
1741: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
1742:   if (mat->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED) {
1743:     mat->valid_GPU_matrix = PETSC_OFFLOAD_CPU;
1744:   }
1745: #endif
1746:   return(0);
1747: }

1749: /*@
1750:    MatSetStencil - Sets the grid information for setting values into a matrix via
1751:         MatSetValuesStencil()

1753:    Not Collective

1755:    Input Parameters:
1756: +  mat - the matrix
1757: .  dim - dimension of the grid 1, 2, or 3
1758: .  dims - number of grid points in x, y, and z direction, including ghost points on your processor
1759: .  starts - starting point of ghost nodes on your processor in x, y, and z direction
1760: -  dof - number of degrees of freedom per node


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

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

1769:    Level: beginner

1771:    Concepts: matrices^putting entries in

1773: .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal()
1774:           MatSetValues(), MatSetValuesBlockedStencil(), MatSetValuesStencil()
1775: @*/
1776: PetscErrorCode MatSetStencil(Mat mat,PetscInt dim,const PetscInt dims[],const PetscInt starts[],PetscInt dof)
1777: {
1778:   PetscInt i;


1785:   mat->stencil.dim = dim + (dof > 1);
1786:   for (i=0; i<dim; i++) {
1787:     mat->stencil.dims[i]   = dims[dim-i-1];      /* copy the values in backwards */
1788:     mat->stencil.starts[i] = starts[dim-i-1];
1789:   }
1790:   mat->stencil.dims[dim]   = dof;
1791:   mat->stencil.starts[dim] = 0;
1792:   mat->stencil.noc         = (PetscBool)(dof == 1);
1793:   return(0);
1794: }

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

1799:    Not Collective

1801:    Input Parameters:
1802: +  mat - the matrix
1803: .  v - a logically two-dimensional array of values
1804: .  m, idxm - the number of block rows and their global block indices
1805: .  n, idxn - the number of block columns and their global block indices
1806: -  addv - either ADD_VALUES or INSERT_VALUES, where
1807:    ADD_VALUES adds values to any existing entries, and
1808:    INSERT_VALUES replaces existing entries with new values

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

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

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

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

1826:    Calls to MatSetValuesBlocked() with the INSERT_VALUES and ADD_VALUES
1827:    options cannot be mixed without intervening calls to the assembly
1828:    routines.

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

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

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

1844:    Example:
1845: $   Suppose m=n=2 and block size(bs) = 2 The array is
1846: $
1847: $   1  2  | 3  4
1848: $   5  6  | 7  8
1849: $   - - - | - - -
1850: $   9  10 | 11 12
1851: $   13 14 | 15 16
1852: $
1853: $   v[] should be passed in like
1854: $   v[] = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
1855: $
1856: $  If you are not using row oriented storage of v (that is you called MatSetOption(mat,MAT_ROW_ORIENTED,PETSC_FALSE)) then
1857: $   v[] = [1,5,9,13,2,6,10,14,3,7,11,15,4,8,12,16]

1859:    Level: intermediate

1861:    Concepts: matrices^putting entries in blocked

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

1872:   if (!m || !n) return(0); /* no values to insert */
1876:   MatCheckPreallocated(mat,1);
1877:   if (mat->insertmode == NOT_SET_VALUES) {
1878:     mat->insertmode = addv;
1879:   }
1880: #if defined(PETSC_USE_DEBUG)
1881:   else if (mat->insertmode != addv) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values");
1882:   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1883:   if (!mat->ops->setvaluesblocked && !mat->ops->setvalues) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
1884: #endif

1886:   if (mat->assembled) {
1887:     mat->was_assembled = PETSC_TRUE;
1888:     mat->assembled     = PETSC_FALSE;
1889:   }
1890:   PetscLogEventBegin(MAT_SetValues,mat,0,0,0);
1891:   if (mat->ops->setvaluesblocked) {
1892:     (*mat->ops->setvaluesblocked)(mat,m,idxm,n,idxn,v,addv);
1893:   } else {
1894:     PetscInt buf[8192],*bufr=0,*bufc=0,*iidxm,*iidxn;
1895:     PetscInt i,j,bs,cbs;
1896:     MatGetBlockSizes(mat,&bs,&cbs);
1897:     if (m*bs+n*cbs <= (PetscInt)(sizeof(buf)/sizeof(PetscInt))) {
1898:       iidxm = buf; iidxn = buf + m*bs;
1899:     } else {
1900:       PetscMalloc2(m*bs,&bufr,n*cbs,&bufc);
1901:       iidxm = bufr; iidxn = bufc;
1902:     }
1903:     for (i=0; i<m; i++) {
1904:       for (j=0; j<bs; j++) {
1905:         iidxm[i*bs+j] = bs*idxm[i] + j;
1906:       }
1907:     }
1908:     for (i=0; i<n; i++) {
1909:       for (j=0; j<cbs; j++) {
1910:         iidxn[i*cbs+j] = cbs*idxn[i] + j;
1911:       }
1912:     }
1913:     MatSetValues(mat,m*bs,iidxm,n*cbs,iidxn,v,addv);
1914:     PetscFree2(bufr,bufc);
1915:   }
1916:   PetscLogEventEnd(MAT_SetValues,mat,0,0,0);
1917: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
1918:   if (mat->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED) {
1919:     mat->valid_GPU_matrix = PETSC_OFFLOAD_CPU;
1920:   }
1921: #endif
1922:   return(0);
1923: }

1925: /*@
1926:    MatGetValues - Gets a block of values from a matrix.

1928:    Not Collective; currently only returns a local block

1930:    Input Parameters:
1931: +  mat - the matrix
1932: .  v - a logically two-dimensional array for storing the values
1933: .  m, idxm - the number of rows and their global indices
1934: -  n, idxn - the number of columns and their global indices

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

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

1944:    MatGetValues() requires that the matrix has been assembled
1945:    with MatAssemblyBegin()/MatAssemblyEnd().  Thus, calls to
1946:    MatSetValues() and MatGetValues() CANNOT be made in succession
1947:    without intermediate matrix assembly.

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

1952:    Level: advanced

1954:    Concepts: matrices^accessing values

1956: .seealso: MatGetRow(), MatCreateSubMatrices(), MatSetValues()
1957: @*/
1958: PetscErrorCode MatGetValues(Mat mat,PetscInt m,const PetscInt idxm[],PetscInt n,const PetscInt idxn[],PetscScalar v[])
1959: {

1965:   if (!m || !n) return(0);
1969:   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
1970:   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1971:   if (!mat->ops->getvalues) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
1972:   MatCheckPreallocated(mat,1);

1974:   PetscLogEventBegin(MAT_GetValues,mat,0,0,0);
1975:   (*mat->ops->getvalues)(mat,m,idxm,n,idxn,v);
1976:   PetscLogEventEnd(MAT_GetValues,mat,0,0,0);
1977:   return(0);
1978: }

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

1984:   Not Collective

1986:   Input Parameters:
1987: + mat - the matrix
1988: . nb - the number of blocks
1989: . bs - the number of rows (and columns) in each block
1990: . rows - a concatenation of the rows for each block
1991: - v - a concatenation of logically two-dimensional arrays of values

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

1996:   Level: advanced

1998:   Concepts: matrices^putting entries in

2000: .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(),
2001:           InsertMode, INSERT_VALUES, ADD_VALUES, MatSetValues()
2002: @*/
2003: PetscErrorCode MatSetValuesBatch(Mat mat, PetscInt nb, PetscInt bs, PetscInt rows[], const PetscScalar v[])
2004: {

2012: #if defined(PETSC_USE_DEBUG)
2013:   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2014: #endif

2016:   PetscLogEventBegin(MAT_SetValuesBatch,mat,0,0,0);
2017:   if (mat->ops->setvaluesbatch) {
2018:     (*mat->ops->setvaluesbatch)(mat,nb,bs,rows,v);
2019:   } else {
2020:     PetscInt b;
2021:     for (b = 0; b < nb; ++b) {
2022:       MatSetValues(mat, bs, &rows[b*bs], bs, &rows[b*bs], &v[b*bs*bs], ADD_VALUES);
2023:     }
2024:   }
2025:   PetscLogEventEnd(MAT_SetValuesBatch,mat,0,0,0);
2026:   return(0);
2027: }

2029: /*@
2030:    MatSetLocalToGlobalMapping - Sets a local-to-global numbering for use by
2031:    the routine MatSetValuesLocal() to allow users to insert matrix entries
2032:    using a local (per-processor) numbering.

2034:    Not Collective

2036:    Input Parameters:
2037: +  x - the matrix
2038: .  rmapping - row mapping created with ISLocalToGlobalMappingCreate()   or ISLocalToGlobalMappingCreateIS()
2039: - cmapping - column mapping

2041:    Level: intermediate

2043:    Concepts: matrices^local to global mapping
2044:    Concepts: local to global mapping^for matrices

2046: .seealso:  MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetValuesLocal()
2047: @*/
2048: PetscErrorCode MatSetLocalToGlobalMapping(Mat x,ISLocalToGlobalMapping rmapping,ISLocalToGlobalMapping cmapping)
2049: {


2058:   if (x->ops->setlocaltoglobalmapping) {
2059:     (*x->ops->setlocaltoglobalmapping)(x,rmapping,cmapping);
2060:   } else {
2061:     PetscLayoutSetISLocalToGlobalMapping(x->rmap,rmapping);
2062:     PetscLayoutSetISLocalToGlobalMapping(x->cmap,cmapping);
2063:   }
2064:   return(0);
2065: }


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

2071:    Not Collective

2073:    Input Parameters:
2074: .  A - the matrix

2076:    Output Parameters:
2077: + rmapping - row mapping
2078: - cmapping - column mapping

2080:    Level: advanced

2082:    Concepts: matrices^local to global mapping
2083:    Concepts: local to global mapping^for matrices

2085: .seealso:  MatSetValuesLocal()
2086: @*/
2087: PetscErrorCode MatGetLocalToGlobalMapping(Mat A,ISLocalToGlobalMapping *rmapping,ISLocalToGlobalMapping *cmapping)
2088: {
2094:   if (rmapping) *rmapping = A->rmap->mapping;
2095:   if (cmapping) *cmapping = A->cmap->mapping;
2096:   return(0);
2097: }

2099: /*@
2100:    MatGetLayouts - Gets the PetscLayout objects for rows and columns

2102:    Not Collective

2104:    Input Parameters:
2105: .  A - the matrix

2107:    Output Parameters:
2108: + rmap - row layout
2109: - cmap - column layout

2111:    Level: advanced

2113: .seealso:  MatCreateVecs(), MatGetLocalToGlobalMapping()
2114: @*/
2115: PetscErrorCode MatGetLayouts(Mat A,PetscLayout *rmap,PetscLayout *cmap)
2116: {
2122:   if (rmap) *rmap = A->rmap;
2123:   if (cmap) *cmap = A->cmap;
2124:   return(0);
2125: }

2127: /*@C
2128:    MatSetValuesLocal - Inserts or adds values into certain locations of a matrix,
2129:    using a local ordering of the nodes.

2131:    Not Collective

2133:    Input Parameters:
2134: +  mat - the matrix
2135: .  nrow, irow - number of rows and their local indices
2136: .  ncol, icol - number of columns and their local indices
2137: .  y -  a logically two-dimensional array of values
2138: -  addv - either INSERT_VALUES or ADD_VALUES, where
2139:    ADD_VALUES adds values to any existing entries, and
2140:    INSERT_VALUES replaces existing entries with new values

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

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

2148:    Calls to MatSetValuesLocal() with the INSERT_VALUES and ADD_VALUES
2149:    options cannot be mixed without intervening calls to the assembly
2150:    routines.

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

2155:    Level: intermediate

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

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

2163: .seealso:  MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetLocalToGlobalMapping(),
2164:            MatSetValueLocal()
2165: @*/
2166: PetscErrorCode MatSetValuesLocal(Mat mat,PetscInt nrow,const PetscInt irow[],PetscInt ncol,const PetscInt icol[],const PetscScalar y[],InsertMode addv)
2167: {

2173:   MatCheckPreallocated(mat,1);
2174:   if (!nrow || !ncol) return(0); /* no values to insert */
2178:   if (mat->insertmode == NOT_SET_VALUES) {
2179:     mat->insertmode = addv;
2180:   }
2181: #if defined(PETSC_USE_DEBUG)
2182:   else if (mat->insertmode != addv) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values");
2183:   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2184:   if (!mat->ops->setvalueslocal && !mat->ops->setvalues) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
2185: #endif

2187:   if (mat->assembled) {
2188:     mat->was_assembled = PETSC_TRUE;
2189:     mat->assembled     = PETSC_FALSE;
2190:   }
2191:   PetscLogEventBegin(MAT_SetValues,mat,0,0,0);
2192:   if (mat->ops->setvalueslocal) {
2193:     (*mat->ops->setvalueslocal)(mat,nrow,irow,ncol,icol,y,addv);
2194:   } else {
2195:     PetscInt buf[8192],*bufr=0,*bufc=0,*irowm,*icolm;
2196:     if ((nrow+ncol) <= (PetscInt)(sizeof(buf)/sizeof(PetscInt))) {
2197:       irowm = buf; icolm = buf+nrow;
2198:     } else {
2199:       PetscMalloc2(nrow,&bufr,ncol,&bufc);
2200:       irowm = bufr; icolm = bufc;
2201:     }
2202:     ISLocalToGlobalMappingApply(mat->rmap->mapping,nrow,irow,irowm);
2203:     ISLocalToGlobalMappingApply(mat->cmap->mapping,ncol,icol,icolm);
2204:     MatSetValues(mat,nrow,irowm,ncol,icolm,y,addv);
2205:     PetscFree2(bufr,bufc);
2206:   }
2207:   PetscLogEventEnd(MAT_SetValues,mat,0,0,0);
2208: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
2209:   if (mat->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED) {
2210:     mat->valid_GPU_matrix = PETSC_OFFLOAD_CPU;
2211:   }
2212: #endif
2213:   return(0);
2214: }

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

2220:    Not Collective

2222:    Input Parameters:
2223: +  x - the matrix
2224: .  nrow, irow - number of rows and their local indices
2225: .  ncol, icol - number of columns and their local indices
2226: .  y -  a logically two-dimensional array of values
2227: -  addv - either INSERT_VALUES or ADD_VALUES, where
2228:    ADD_VALUES adds values to any existing entries, and
2229:    INSERT_VALUES replaces existing entries with new values

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

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

2238:    Calls to MatSetValuesBlockedLocal() with the INSERT_VALUES and ADD_VALUES
2239:    options cannot be mixed without intervening calls to the assembly
2240:    routines.

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

2245:    Level: intermediate

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

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

2253: .seealso:  MatSetBlockSize(), MatSetLocalToGlobalMapping(), MatAssemblyBegin(), MatAssemblyEnd(),
2254:            MatSetValuesLocal(),  MatSetValuesBlocked()
2255: @*/
2256: PetscErrorCode MatSetValuesBlockedLocal(Mat mat,PetscInt nrow,const PetscInt irow[],PetscInt ncol,const PetscInt icol[],const PetscScalar y[],InsertMode addv)
2257: {

2263:   MatCheckPreallocated(mat,1);
2264:   if (!nrow || !ncol) return(0); /* no values to insert */
2268:   if (mat->insertmode == NOT_SET_VALUES) {
2269:     mat->insertmode = addv;
2270:   }
2271: #if defined(PETSC_USE_DEBUG)
2272:   else if (mat->insertmode != addv) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values");
2273:   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2274:   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);
2275: #endif

2277:   if (mat->assembled) {
2278:     mat->was_assembled = PETSC_TRUE;
2279:     mat->assembled     = PETSC_FALSE;
2280:   }
2281:   PetscLogEventBegin(MAT_SetValues,mat,0,0,0);
2282:   if (mat->ops->setvaluesblockedlocal) {
2283:     (*mat->ops->setvaluesblockedlocal)(mat,nrow,irow,ncol,icol,y,addv);
2284:   } else {
2285:     PetscInt buf[8192],*bufr=0,*bufc=0,*irowm,*icolm;
2286:     if ((nrow+ncol) <= (PetscInt)(sizeof(buf)/sizeof(PetscInt))) {
2287:       irowm = buf; icolm = buf + nrow;
2288:     } else {
2289:       PetscMalloc2(nrow,&bufr,ncol,&bufc);
2290:       irowm = bufr; icolm = bufc;
2291:     }
2292:     ISLocalToGlobalMappingApplyBlock(mat->rmap->mapping,nrow,irow,irowm);
2293:     ISLocalToGlobalMappingApplyBlock(mat->cmap->mapping,ncol,icol,icolm);
2294:     MatSetValuesBlocked(mat,nrow,irowm,ncol,icolm,y,addv);
2295:     PetscFree2(bufr,bufc);
2296:   }
2297:   PetscLogEventEnd(MAT_SetValues,mat,0,0,0);
2298: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
2299:   if (mat->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED) {
2300:     mat->valid_GPU_matrix = PETSC_OFFLOAD_CPU;
2301:   }
2302: #endif
2303:   return(0);
2304: }

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

2309:    Collective on Mat and Vec

2311:    Input Parameters:
2312: +  mat - the matrix
2313: -  x   - the vector to be multiplied

2315:    Output Parameters:
2316: .  y - the result

2318:    Notes:
2319:    The vectors x and y cannot be the same.  I.e., one cannot
2320:    call MatMult(A,y,y).

2322:    Level: developer

2324:    Concepts: matrix-vector product

2326: .seealso: MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd()
2327: @*/
2328: PetscErrorCode MatMultDiagonalBlock(Mat mat,Vec x,Vec y)
2329: {


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

2343:   if (!mat->ops->multdiagonalblock) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"This matrix type does not have a multiply defined");
2344:   (*mat->ops->multdiagonalblock)(mat,x,y);
2345:   PetscObjectStateIncrease((PetscObject)y);
2346:   return(0);
2347: }

2349: /* --------------------------------------------------------*/
2350: /*@
2351:    MatMult - Computes the matrix-vector product, y = Ax.

2353:    Neighbor-wise Collective on Mat and Vec

2355:    Input Parameters:
2356: +  mat - the matrix
2357: -  x   - the vector to be multiplied

2359:    Output Parameters:
2360: .  y - the result

2362:    Notes:
2363:    The vectors x and y cannot be the same.  I.e., one cannot
2364:    call MatMult(A,y,y).

2366:    Level: beginner

2368:    Concepts: matrix-vector product

2370: .seealso: MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd()
2371: @*/
2372: PetscErrorCode MatMult(Mat mat,Vec x,Vec y)
2373: {

2381:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2382:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2383:   if (x == y) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors");
2384: #if !defined(PETSC_HAVE_CONSTRAINTS)
2385:   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);
2386:   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);
2387:   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);
2388: #endif
2389:   VecSetErrorIfLocked(y,3);
2390:   if (mat->erroriffailure) {VecValidValues(x,2,PETSC_TRUE);}
2391:   MatCheckPreallocated(mat,1);

2393:   VecLockReadPush(x);
2394:   if (!mat->ops->mult) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"This matrix type does not have a multiply defined");
2395:   PetscLogEventBegin(MAT_Mult,mat,x,y,0);
2396:   (*mat->ops->mult)(mat,x,y);
2397:   PetscLogEventEnd(MAT_Mult,mat,x,y,0);
2398:   if (mat->erroriffailure) {VecValidValues(y,3,PETSC_FALSE);}
2399:   VecLockReadPop(x);
2400:   return(0);
2401: }

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

2406:    Neighbor-wise Collective on Mat and Vec

2408:    Input Parameters:
2409: +  mat - the matrix
2410: -  x   - the vector to be multiplied

2412:    Output Parameters:
2413: .  y - the result

2415:    Notes:
2416:    The vectors x and y cannot be the same.  I.e., one cannot
2417:    call MatMultTranspose(A,y,y).

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

2422:    Level: beginner

2424:    Concepts: matrix vector product^transpose

2426: .seealso: MatMult(), MatMultAdd(), MatMultTransposeAdd(), MatMultHermitianTranspose(), MatTranspose()
2427: @*/
2428: PetscErrorCode MatMultTranspose(Mat mat,Vec x,Vec y)
2429: {


2438:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2439:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2440:   if (x == y) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors");
2441: #if !defined(PETSC_HAVE_CONSTRAINTS)
2442:   if (mat->rmap->N != x->map->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %D %D",mat->rmap->N,x->map->N);
2443:   if (mat->cmap->N != y->map->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %D %D",mat->cmap->N,y->map->N);
2444: #endif
2445:   if (mat->erroriffailure) {VecValidValues(x,2,PETSC_TRUE);}
2446:   MatCheckPreallocated(mat,1);

2448:   if (!mat->ops->multtranspose) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"This matrix type does not have a multiply transpose defined");
2449:   PetscLogEventBegin(MAT_MultTranspose,mat,x,y,0);
2450:   VecLockReadPush(x);
2451:   (*mat->ops->multtranspose)(mat,x,y);
2452:   VecLockReadPop(x);
2453:   PetscLogEventEnd(MAT_MultTranspose,mat,x,y,0);
2454:   PetscObjectStateIncrease((PetscObject)y);
2455:   if (mat->erroriffailure) {VecValidValues(y,3,PETSC_FALSE);}
2456:   return(0);
2457: }

2459: /*@
2460:    MatMultHermitianTranspose - Computes matrix Hermitian transpose times a vector.

2462:    Neighbor-wise Collective on Mat and Vec

2464:    Input Parameters:
2465: +  mat - the matrix
2466: -  x   - the vector to be multilplied

2468:    Output Parameters:
2469: .  y - the result

2471:    Notes:
2472:    The vectors x and y cannot be the same.  I.e., one cannot
2473:    call MatMultHermitianTranspose(A,y,y).

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

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

2479:    Level: beginner

2481:    Concepts: matrix vector product^transpose

2483: .seealso: MatMult(), MatMultAdd(), MatMultHermitianTransposeAdd(), MatMultTranspose()
2484: @*/
2485: PetscErrorCode MatMultHermitianTranspose(Mat mat,Vec x,Vec y)
2486: {
2488:   Vec            w;


2496:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2497:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2498:   if (x == y) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors");
2499: #if !defined(PETSC_HAVE_CONSTRAINTS)
2500:   if (mat->rmap->N != x->map->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %D %D",mat->rmap->N,x->map->N);
2501:   if (mat->cmap->N != y->map->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %D %D",mat->cmap->N,y->map->N);
2502: #endif
2503:   MatCheckPreallocated(mat,1);

2505:   PetscLogEventBegin(MAT_MultHermitianTranspose,mat,x,y,0);
2506:   if (mat->ops->multhermitiantranspose) {
2507:     VecLockReadPush(x);
2508:     (*mat->ops->multhermitiantranspose)(mat,x,y);
2509:     VecLockReadPop(x);
2510:   } else {
2511:     VecDuplicate(x,&w);
2512:     VecCopy(x,w);
2513:     VecConjugate(w);
2514:     MatMultTranspose(mat,w,y);
2515:     VecDestroy(&w);
2516:     VecConjugate(y);
2517:   }
2518:   PetscLogEventEnd(MAT_MultHermitianTranspose,mat,x,y,0);
2519:   PetscObjectStateIncrease((PetscObject)y);
2520:   return(0);
2521: }

2523: /*@
2524:     MatMultAdd -  Computes v3 = v2 + A * v1.

2526:     Neighbor-wise Collective on Mat and Vec

2528:     Input Parameters:
2529: +   mat - the matrix
2530: -   v1, v2 - the vectors

2532:     Output Parameters:
2533: .   v3 - the result

2535:     Notes:
2536:     The vectors v1 and v3 cannot be the same.  I.e., one cannot
2537:     call MatMultAdd(A,v1,v2,v1).

2539:     Level: beginner

2541:     Concepts: matrix vector product^addition

2543: .seealso: MatMultTranspose(), MatMult(), MatMultTransposeAdd()
2544: @*/
2545: PetscErrorCode MatMultAdd(Mat mat,Vec v1,Vec v2,Vec v3)
2546: {


2556:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2557:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2558:   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);
2559:   /* 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);
2560:      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); */
2561:   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);
2562:   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);
2563:   if (v1 == v3) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"v1 and v3 must be different vectors");
2564:   MatCheckPreallocated(mat,1);

2566:   if (!mat->ops->multadd) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"No MatMultAdd() for matrix type '%s'",((PetscObject)mat)->type_name);
2567:   PetscLogEventBegin(MAT_MultAdd,mat,v1,v2,v3);
2568:   VecLockReadPush(v1);
2569:   (*mat->ops->multadd)(mat,v1,v2,v3);
2570:   VecLockReadPop(v1);
2571:   PetscLogEventEnd(MAT_MultAdd,mat,v1,v2,v3);
2572:   PetscObjectStateIncrease((PetscObject)v3);
2573:   return(0);
2574: }

2576: /*@
2577:    MatMultTransposeAdd - Computes v3 = v2 + A' * v1.

2579:    Neighbor-wise Collective on Mat and Vec

2581:    Input Parameters:
2582: +  mat - the matrix
2583: -  v1, v2 - the vectors

2585:    Output Parameters:
2586: .  v3 - the result

2588:    Notes:
2589:    The vectors v1 and v3 cannot be the same.  I.e., one cannot
2590:    call MatMultTransposeAdd(A,v1,v2,v1).

2592:    Level: beginner

2594:    Concepts: matrix vector product^transpose and addition

2596: .seealso: MatMultTranspose(), MatMultAdd(), MatMult()
2597: @*/
2598: PetscErrorCode MatMultTransposeAdd(Mat mat,Vec v1,Vec v2,Vec v3)
2599: {


2609:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2610:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2611:   if (!mat->ops->multtransposeadd) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
2612:   if (v1 == v3) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"v1 and v3 must be different vectors");
2613:   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);
2614:   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);
2615:   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);
2616:   MatCheckPreallocated(mat,1);

2618:   PetscLogEventBegin(MAT_MultTransposeAdd,mat,v1,v2,v3);
2619:   VecLockReadPush(v1);
2620:   (*mat->ops->multtransposeadd)(mat,v1,v2,v3);
2621:   VecLockReadPop(v1);
2622:   PetscLogEventEnd(MAT_MultTransposeAdd,mat,v1,v2,v3);
2623:   PetscObjectStateIncrease((PetscObject)v3);
2624:   return(0);
2625: }

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

2630:    Neighbor-wise Collective on Mat and Vec

2632:    Input Parameters:
2633: +  mat - the matrix
2634: -  v1, v2 - the vectors

2636:    Output Parameters:
2637: .  v3 - the result

2639:    Notes:
2640:    The vectors v1 and v3 cannot be the same.  I.e., one cannot
2641:    call MatMultHermitianTransposeAdd(A,v1,v2,v1).

2643:    Level: beginner

2645:    Concepts: matrix vector product^transpose and addition

2647: .seealso: MatMultHermitianTranspose(), MatMultTranspose(), MatMultAdd(), MatMult()
2648: @*/
2649: PetscErrorCode MatMultHermitianTransposeAdd(Mat mat,Vec v1,Vec v2,Vec v3)
2650: {


2660:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2661:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2662:   if (v1 == v3) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"v1 and v3 must be different vectors");
2663:   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);
2664:   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);
2665:   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);
2666:   MatCheckPreallocated(mat,1);

2668:   PetscLogEventBegin(MAT_MultHermitianTransposeAdd,mat,v1,v2,v3);
2669:   VecLockReadPush(v1);
2670:   if (mat->ops->multhermitiantransposeadd) {
2671:     (*mat->ops->multhermitiantransposeadd)(mat,v1,v2,v3);
2672:   } else {
2673:     Vec w,z;
2674:     VecDuplicate(v1,&w);
2675:     VecCopy(v1,w);
2676:     VecConjugate(w);
2677:     VecDuplicate(v3,&z);
2678:     MatMultTranspose(mat,w,z);
2679:     VecDestroy(&w);
2680:     VecConjugate(z);
2681:     if (v2 != v3) {
2682:       VecWAXPY(v3,1.0,v2,z);
2683:     } else {
2684:       VecAXPY(v3,1.0,z);
2685:     }
2686:     VecDestroy(&z);
2687:   }
2688:   VecLockReadPop(v1);
2689:   PetscLogEventEnd(MAT_MultHermitianTransposeAdd,mat,v1,v2,v3);
2690:   PetscObjectStateIncrease((PetscObject)v3);
2691:   return(0);
2692: }

2694: /*@
2695:    MatMultConstrained - The inner multiplication routine for a
2696:    constrained matrix P^T A P.

2698:    Neighbor-wise Collective on Mat and Vec

2700:    Input Parameters:
2701: +  mat - the matrix
2702: -  x   - the vector to be multilplied

2704:    Output Parameters:
2705: .  y - the result

2707:    Notes:
2708:    The vectors x and y cannot be the same.  I.e., one cannot
2709:    call MatMult(A,y,y).

2711:    Level: beginner

2713: .keywords: matrix, multiply, matrix-vector product, constraint
2714: .seealso: MatMult(), MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd()
2715: @*/
2716: PetscErrorCode MatMultConstrained(Mat mat,Vec x,Vec y)
2717: {

2724:   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2725:   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2726:   if (x == y) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors");
2727:   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);
2728:   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);
2729:   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);

2731:   PetscLogEventBegin(MAT_MultConstrained,mat,x,y,0);
2732:   VecLockReadPush(x);
2733:   (*mat->ops->multconstrained)(mat,x,y);
2734:   VecLockReadPop(x);
2735:   PetscLogEventEnd(MAT_MultConstrained,mat,x,y,0);
2736:   PetscObjectStateIncrease((PetscObject)y);
2737:   return(0);
2738: }

2740: /*@
2741:    MatMultTransposeConstrained - The inner multiplication routine for a
2742:    constrained matrix P^T A^T P.

2744:    Neighbor-wise Collective on Mat and Vec

2746:    Input Parameters:
2747: +  mat - the matrix
2748: -  x   - the vector to be multilplied

2750:    Output Parameters:
2751: .  y - the result

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

2757:    Level: beginner

2759: .keywords: matrix, multiply, matrix-vector product, constraint
2760: .seealso: MatMult(), MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd()
2761: @*/
2762: PetscErrorCode MatMultTransposeConstrained(Mat mat,Vec x,Vec y)
2763: {

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

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

2783: /*@C
2784:    MatGetFactorType - gets the type of factorization it is

2786:    Not Collective

2788:    Input Parameters:
2789: .  mat - the matrix

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

2794:    Level: intermediate

2796: .seealso: MatFactorType, MatGetFactor(), MatSetFactorType()
2797: @*/
2798: PetscErrorCode MatGetFactorType(Mat mat,MatFactorType *t)
2799: {
2804:   *t = mat->factortype;
2805:   return(0);
2806: }

2808: /*@C
2809:    MatSetFactorType - sets the type of factorization it is

2811:    Logically Collective on Mat

2813:    Input Parameters:
2814: +  mat - the matrix
2815: -  t - the type, one of MAT_FACTOR_NONE, MAT_FACTOR_LU, MAT_FACTOR_CHOLESKY, MAT_FACTOR_ILU, MAT_FACTOR_ICC,MAT_FACTOR_ILUDT

2817:    Level: intermediate

2819: .seealso: MatFactorType, MatGetFactor(), MatGetFactorType()
2820: @*/
2821: PetscErrorCode MatSetFactorType(Mat mat, MatFactorType t)
2822: {
2826:   mat->factortype = t;
2827:   return(0);
2828: }

2830: /* ------------------------------------------------------------*/
2831: /*@C
2832:    MatGetInfo - Returns information about matrix storage (number of
2833:    nonzeros, memory, etc.).

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

2837:    Input Parameters:
2838: .  mat - the matrix

2840:    Output Parameters:
2841: +  flag - flag indicating the type of parameters to be returned
2842:    (MAT_LOCAL - local matrix, MAT_GLOBAL_MAX - maximum over all processors,
2843:    MAT_GLOBAL_SUM - sum over all processors)
2844: -  info - matrix information context

2846:    Notes:
2847:    The MatInfo context contains a variety of matrix data, including
2848:    number of nonzeros allocated and used, number of mallocs during
2849:    matrix assembly, etc.  Additional information for factored matrices
2850:    is provided (such as the fill ratio, number of mallocs during
2851:    factorization, etc.).  Much of this info is printed to PETSC_STDOUT
2852:    when using the runtime options
2853: $       -info -mat_view ::ascii_info

2855:    Example for C/C++ Users:
2856:    See the file ${PETSC_DIR}/include/petscmat.h for a complete list of
2857:    data within the MatInfo context.  For example,
2858: .vb
2859:       MatInfo info;
2860:       Mat     A;
2861:       double  mal, nz_a, nz_u;

2863:       MatGetInfo(A,MAT_LOCAL,&info);
2864:       mal  = info.mallocs;
2865:       nz_a = info.nz_allocated;
2866: .ve

2868:    Example for Fortran Users:
2869:    Fortran users should declare info as a double precision
2870:    array of dimension MAT_INFO_SIZE, and then extract the parameters
2871:    of interest.  See the file ${PETSC_DIR}/include/petsc/finclude/petscmat.h
2872:    a complete list of parameter names.
2873: .vb
2874:       double  precision info(MAT_INFO_SIZE)
2875:       double  precision mal, nz_a
2876:       Mat     A
2877:       integer ierr

2879:       call MatGetInfo(A,MAT_LOCAL,info,ierr)
2880:       mal = info(MAT_INFO_MALLOCS)
2881:       nz_a = info(MAT_INFO_NZ_ALLOCATED)
2882: .ve

2884:     Level: intermediate

2886:     Concepts: matrices^getting information on

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

2891: .seealso: MatStashGetInfo()

2893: @*/
2894: PetscErrorCode MatGetInfo(Mat mat,MatInfoType flag,MatInfo *info)
2895: {

2902:   if (!mat->ops->getinfo) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
2903:   MatCheckPreallocated(mat,1);
2904:   (*mat->ops->getinfo)(mat,flag,info);
2905:   return(0);
2906: }

2908: /*
2909:    This is used by external packages where it is not easy to get the info from the actual
2910:    matrix factorization.
2911: */
2912: PetscErrorCode MatGetInfo_External(Mat A,MatInfoType flag,MatInfo *info)
2913: {

2917:   PetscMemzero(info,sizeof(MatInfo));
2918:   return(0);
2919: }

2921: /* ----------------------------------------------------------*/

2923: /*@C
2924:    MatLUFactor - Performs in-place LU factorization of matrix.

2926:    Collective on Mat

2928:    Input Parameters:
2929: +  mat - the matrix
2930: .  row - row permutation
2931: .  col - column permutation
2932: -  info - options for factorization, includes
2933: $          fill - expected fill as ratio of original fill.
2934: $          dtcol - pivot tolerance (0 no pivot, 1 full column pivoting)
2935: $                   Run with the option -info to determine an optimal value to use

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

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

2945:    Level: developer

2947:    Concepts: matrices^LU factorization

2949: .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor(),
2950:           MatGetOrdering(), MatSetUnfactored(), MatFactorInfo, MatGetFactor()

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

2955: @*/
2956: PetscErrorCode MatLUFactor(Mat mat,IS row,IS col,const MatFactorInfo *info)
2957: {
2959:   MatFactorInfo  tinfo;

2967:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2968:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2969:   if (!mat->ops->lufactor) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
2970:   MatCheckPreallocated(mat,1);
2971:   if (!info) {
2972:     MatFactorInfoInitialize(&tinfo);
2973:     info = &tinfo;
2974:   }

2976:   PetscLogEventBegin(MAT_LUFactor,mat,row,col,0);
2977:   (*mat->ops->lufactor)(mat,row,col,info);
2978:   PetscLogEventEnd(MAT_LUFactor,mat,row,col,0);
2979:   PetscObjectStateIncrease((PetscObject)mat);
2980:   return(0);
2981: }

2983: /*@C
2984:    MatILUFactor - Performs in-place ILU factorization of matrix.

2986:    Collective on Mat

2988:    Input Parameters:
2989: +  mat - the matrix
2990: .  row - row permutation
2991: .  col - column permutation
2992: -  info - structure containing
2993: $      levels - number of levels of fill.
2994: $      expected fill - as ratio of original fill.
2995: $      1 or 0 - indicating force fill on diagonal (improves robustness for matrices
2996:                 missing diagonal entries)

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

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

3006:    Level: developer

3008:    Concepts: matrices^ILU factorization

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

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

3015: @*/
3016: PetscErrorCode MatILUFactor(Mat mat,IS row,IS col,const MatFactorInfo *info)
3017: {

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

3032:   PetscLogEventBegin(MAT_ILUFactor,mat,row,col,0);
3033:   (*mat->ops->ilufactor)(mat,row,col,info);
3034:   PetscLogEventEnd(MAT_ILUFactor,mat,row,col,0);
3035:   PetscObjectStateIncrease((PetscObject)mat);
3036:   return(0);
3037: }

3039: /*@C
3040:    MatLUFactorSymbolic - Performs symbolic LU factorization of matrix.
3041:    Call this routine before calling MatLUFactorNumeric().

3043:    Collective on Mat

3045:    Input Parameters:
3046: +  fact - the factor matrix obtained with MatGetFactor()
3047: .  mat - the matrix
3048: .  row, col - row and column permutations
3049: -  info - options for factorization, includes
3050: $          fill - expected fill as ratio of original fill.
3051: $          dtcol - pivot tolerance (0 no pivot, 1 full column pivoting)
3052: $                   Run with the option -info to determine an optimal value to use


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

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

3062:    Level: developer

3064:    Concepts: matrices^LU symbolic factorization

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

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

3071: @*/
3072: PetscErrorCode MatLUFactorSymbolic(Mat fact,Mat mat,IS row,IS col,const MatFactorInfo *info)
3073: {

3083:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3084:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3085:   if (!(fact)->ops->lufactorsymbolic) {
3086:     MatSolverType spackage;
3087:     MatFactorGetSolverType(fact,&spackage);
3088:     SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Matrix type %s symbolic LU using solver package %s",((PetscObject)mat)->type_name,spackage);
3089:   }
3090:   MatCheckPreallocated(mat,2);

3092:   PetscLogEventBegin(MAT_LUFactorSymbolic,mat,row,col,0);
3093:   (fact->ops->lufactorsymbolic)(fact,mat,row,col,info);
3094:   PetscLogEventEnd(MAT_LUFactorSymbolic,mat,row,col,0);
3095:   PetscObjectStateIncrease((PetscObject)fact);
3096:   return(0);
3097: }

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

3103:    Collective on Mat

3105:    Input Parameters:
3106: +  fact - the factor matrix obtained with MatGetFactor()
3107: .  mat - the matrix
3108: -  info - options for factorization

3110:    Notes:
3111:    See MatLUFactor() for in-place factorization.  See
3112:    MatCholeskyFactorNumeric() for the symmetric, positive definite case.

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

3118:    Level: developer

3120:    Concepts: matrices^LU numeric factorization

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

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

3127: @*/
3128: PetscErrorCode MatLUFactorNumeric(Mat fact,Mat mat,const MatFactorInfo *info)
3129: {

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

3140:   if (!(fact)->ops->lufactornumeric) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s numeric LU",((PetscObject)mat)->type_name);
3141:   MatCheckPreallocated(mat,2);
3142:   PetscLogEventBegin(MAT_LUFactorNumeric,mat,fact,0,0);
3143:   (fact->ops->lufactornumeric)(fact,mat,info);
3144:   PetscLogEventEnd(MAT_LUFactorNumeric,mat,fact,0,0);
3145:   MatViewFromOptions(fact,NULL,"-mat_factor_view");
3146:   PetscObjectStateIncrease((PetscObject)fact);
3147:   return(0);
3148: }

3150: /*@C
3151:    MatCholeskyFactor - Performs in-place Cholesky factorization of a
3152:    symmetric matrix.

3154:    Collective on Mat

3156:    Input Parameters:
3157: +  mat - the matrix
3158: .  perm - row and column permutations
3159: -  f - expected fill as ratio of original fill

3161:    Notes:
3162:    See MatLUFactor() for the nonsymmetric case.  See also
3163:    MatCholeskyFactorSymbolic(), and MatCholeskyFactorNumeric().

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

3169:    Level: developer

3171:    Concepts: matrices^Cholesky factorization

3173: .seealso: MatLUFactor(), MatCholeskyFactorSymbolic(), MatCholeskyFactorNumeric()
3174:           MatGetOrdering()

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

3179: @*/
3180: PetscErrorCode MatCholeskyFactor(Mat mat,IS perm,const MatFactorInfo *info)
3181: {

3189:   if (mat->rmap->N != mat->cmap->N) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONG,"Matrix must be square");
3190:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3191:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3192:   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);
3193:   MatCheckPreallocated(mat,1);

3195:   PetscLogEventBegin(MAT_CholeskyFactor,mat,perm,0,0);
3196:   (*mat->ops->choleskyfactor)(mat,perm,info);
3197:   PetscLogEventEnd(MAT_CholeskyFactor,mat,perm,0,0);
3198:   PetscObjectStateIncrease((PetscObject)mat);
3199:   return(0);
3200: }

3202: /*@C
3203:    MatCholeskyFactorSymbolic - Performs symbolic Cholesky factorization
3204:    of a symmetric matrix.

3206:    Collective on Mat

3208:    Input Parameters:
3209: +  fact - the factor matrix obtained with MatGetFactor()
3210: .  mat - the matrix
3211: .  perm - row and column permutations
3212: -  info - options for factorization, includes
3213: $          fill - expected fill as ratio of original fill.
3214: $          dtcol - pivot tolerance (0 no pivot, 1 full column pivoting)
3215: $                   Run with the option -info to determine an optimal value to use

3217:    Notes:
3218:    See MatLUFactorSymbolic() for the nonsymmetric case.  See also
3219:    MatCholeskyFactor() and MatCholeskyFactorNumeric().

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

3225:    Level: developer

3227:    Concepts: matrices^Cholesky symbolic factorization

3229: .seealso: MatLUFactorSymbolic(), MatCholeskyFactor(), MatCholeskyFactorNumeric()
3230:           MatGetOrdering()

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

3235: @*/
3236: PetscErrorCode MatCholeskyFactorSymbolic(Mat fact,Mat mat,IS perm,const MatFactorInfo *info)
3237: {

3246:   if (mat->rmap->N != mat->cmap->N) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONG,"Matrix must be square");
3247:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3248:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3249:   if (!(fact)->ops->choleskyfactorsymbolic) {
3250:     MatSolverType spackage;
3251:     MatFactorGetSolverType(fact,&spackage);
3252:     SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s symbolic factor Cholesky using solver package %s",((PetscObject)mat)->type_name,spackage);
3253:   }
3254:   MatCheckPreallocated(mat,2);

3256:   PetscLogEventBegin(MAT_CholeskyFactorSymbolic,mat,perm,0,0);
3257:   (fact->ops->choleskyfactorsymbolic)(fact,mat,perm,info);
3258:   PetscLogEventEnd(MAT_CholeskyFactorSymbolic,mat,perm,0,0);
3259:   PetscObjectStateIncrease((PetscObject)fact);
3260:   return(0);
3261: }

3263: /*@C
3264:    MatCholeskyFactorNumeric - Performs numeric Cholesky factorization
3265:    of a symmetric matrix. Call this routine after first calling
3266:    MatCholeskyFactorSymbolic().

3268:    Collective on Mat

3270:    Input Parameters:
3271: +  fact - the factor matrix obtained with MatGetFactor()
3272: .  mat - the initial matrix
3273: .  info - options for factorization
3274: -  fact - the symbolic factor of mat


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

3282:    Level: developer

3284:    Concepts: matrices^Cholesky numeric factorization

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

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

3291: @*/
3292: PetscErrorCode MatCholeskyFactorNumeric(Mat fact,Mat mat,const MatFactorInfo *info)
3293: {

3301:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3302:   if (!(fact)->ops->choleskyfactornumeric) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s numeric factor Cholesky",((PetscObject)mat)->type_name);
3303:   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);
3304:   MatCheckPreallocated(mat,2);

3306:   PetscLogEventBegin(MAT_CholeskyFactorNumeric,mat,fact,0,0);
3307:   (fact->ops->choleskyfactornumeric)(fact,mat,info);
3308:   PetscLogEventEnd(MAT_CholeskyFactorNumeric,mat,fact,0,0);
3309:   MatViewFromOptions(fact,NULL,"-mat_factor_view");
3310:   PetscObjectStateIncrease((PetscObject)fact);
3311:   return(0);
3312: }

3314: /* ----------------------------------------------------------------*/
3315: /*@
3316:    MatSolve - Solves A x = b, given a factored matrix.

3318:    Neighbor-wise Collective on Mat and Vec

3320:    Input Parameters:
3321: +  mat - the factored matrix
3322: -  b - the right-hand-side vector

3324:    Output Parameter:
3325: .  x - the result vector

3327:    Notes:
3328:    The vectors b and x cannot be the same.  I.e., one cannot
3329:    call MatSolve(A,x,x).

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

3336:    Level: developer

3338:    Concepts: matrices^triangular solves

3340: .seealso: MatSolveAdd(), MatSolveTranspose(), MatSolveTransposeAdd()
3341: @*/
3342: PetscErrorCode MatSolve(Mat mat,Vec b,Vec x)
3343: {

3353:   if (x == b) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"x and b must be different vectors");
3354:   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);
3355:   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);
3356:   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);
3357:   if (!mat->rmap->N && !mat->cmap->N) return(0);
3358:   if (!mat->ops->solve) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
3359:   MatCheckPreallocated(mat,1);

3361:   PetscLogEventBegin(MAT_Solve,mat,b,x,0);
3362:   if (mat->factorerrortype) {
3363:     PetscInfo1(mat,"MatFactorError %D\n",mat->factorerrortype);
3364:     VecSetInf(x);
3365:   } else {
3366:     if (!mat->ops->solve) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
3367:     (*mat->ops->solve)(mat,b,x);
3368:   }
3369:   PetscLogEventEnd(MAT_Solve,mat,b,x,0);
3370:   PetscObjectStateIncrease((PetscObject)x);
3371:   return(0);
3372: }

3374: static PetscErrorCode MatMatSolve_Basic(Mat A,Mat B,Mat X, PetscBool trans)
3375: {
3377:   Vec            b,x;
3378:   PetscInt       m,N,i;
3379:   PetscScalar    *bb,*xx;
3380:   PetscBool      flg;

3383:   PetscObjectTypeCompareAny((PetscObject)B,&flg,MATSEQDENSE,MATMPIDENSE,NULL);
3384:   if (!flg) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONG,"Matrix B must be MATDENSE matrix");
3385:   PetscObjectTypeCompareAny((PetscObject)X,&flg,MATSEQDENSE,MATMPIDENSE,NULL);
3386:   if (!flg) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONG,"Matrix X must be MATDENSE matrix");

3388:   MatDenseGetArray(B,&bb);
3389:   MatDenseGetArray(X,&xx);
3390:   MatGetLocalSize(B,&m,NULL);  /* number local rows */
3391:   MatGetSize(B,NULL,&N);       /* total columns in dense matrix */
3392:   MatCreateVecs(A,&x,&b);
3393:   for (i=0; i<N; i++) {
3394:     VecPlaceArray(b,bb + i*m);
3395:     VecPlaceArray(x,xx + i*m);
3396:     if (trans) {
3397:       MatSolveTranspose(A,b,x);
3398:     } else {
3399:       MatSolve(A,b,x);
3400:     }
3401:     VecResetArray(x);
3402:     VecResetArray(b);
3403:   }
3404:   VecDestroy(&b);
3405:   VecDestroy(&x);
3406:   MatDenseRestoreArray(B,&bb);
3407:   MatDenseRestoreArray(X,&xx);
3408:   return(0);
3409: }

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

3414:    Neighbor-wise Collective on Mat

3416:    Input Parameters:
3417: +  A - the factored matrix
3418: -  B - the right-hand-side matrix  (dense matrix)

3420:    Output Parameter:
3421: .  X - the result matrix (dense matrix)

3423:    Notes:
3424:    The matrices b and x cannot be the same.  I.e., one cannot
3425:    call MatMatSolve(A,x,x).

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

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

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

3438:    Level: developer

3440:    Concepts: matrices^triangular solves

3442: .seealso: MatMatSolveTranspose(), MatLUFactor(), MatCholeskyFactor()
3443: @*/
3444: PetscErrorCode MatMatSolve(Mat A,Mat B,Mat X)
3445: {

3455:   if (X == B) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_IDN,"X and B must be different matrices");
3456:   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);
3457:   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);
3458:   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");
3459:   if (!A->rmap->N && !A->cmap->N) return(0);
3460:   if (!A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
3461:   MatCheckPreallocated(A,1);

3463:   PetscLogEventBegin(MAT_MatSolve,A,B,X,0);
3464:   if (!A->ops->matsolve) {
3465:     PetscInfo1(A,"Mat type %s using basic MatMatSolve\n",((PetscObject)A)->type_name);
3466:     MatMatSolve_Basic(A,B,X,PETSC_FALSE);
3467:   } else {
3468:     (*A->ops->matsolve)(A,B,X);
3469:   }
3470:   PetscLogEventEnd(MAT_MatSolve,A,B,X,0);
3471:   PetscObjectStateIncrease((PetscObject)X);
3472:   return(0);
3473: }

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

3478:    Neighbor-wise Collective on Mat

3480:    Input Parameters:
3481: +  A - the factored matrix
3482: -  B - the right-hand-side matrix  (dense matrix)

3484:    Output Parameter:
3485: .  X - the result matrix (dense matrix)

3487:    Notes:
3488:    The matrices B and X cannot be the same.  I.e., one cannot
3489:    call MatMatSolveTranspose(A,X,X).

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

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

3499:    Level: developer

3501:    Concepts: matrices^triangular solves

3503: .seealso: MatMatSolve(), MatLUFactor(), MatCholeskyFactor()
3504: @*/
3505: PetscErrorCode MatMatSolveTranspose(Mat A,Mat B,Mat X)
3506: {

3516:   if (X == B) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_IDN,"X and B must be different matrices");
3517:   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);
3518:   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);
3519:   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);
3520:   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");
3521:   if (!A->rmap->N && !A->cmap->N) return(0);
3522:   if (!A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
3523:   MatCheckPreallocated(A,1);

3525:   PetscLogEventBegin(MAT_MatSolve,A,B,X,0);
3526:   if (!A->ops->matsolvetranspose) {
3527:     PetscInfo1(A,"Mat type %s using basic MatMatSolveTranspose\n",((PetscObject)A)->type_name);
3528:     MatMatSolve_Basic(A,B,X,PETSC_TRUE);
3529:   } else {
3530:     (*A->ops->matsolvetranspose)(A,B,X);
3531:   }
3532:   PetscLogEventEnd(MAT_MatSolve,A,B,X,0);
3533:   PetscObjectStateIncrease((PetscObject)X);
3534:   return(0);
3535: }

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

3540:    Neighbor-wise Collective on Mat

3542:    Input Parameters:
3543: +  A - the factored matrix
3544: -  Bt - the transpose of right-hand-side matrix

3546:    Output Parameter:
3547: .  X - the result matrix (dense matrix)

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

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

3557:    Level: developer

3559:    Concepts: matrices^triangular solves

3561: .seealso: MatMatSolve(), MatMatSolveTranspose(), MatLUFactor(), MatCholeskyFactor()
3562: @*/
3563: PetscErrorCode MatMatTransposeSolve(Mat A,Mat Bt,Mat X)
3564: {


3575:   if (X == Bt) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_IDN,"X and B must be different matrices");
3576:   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);
3577:   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);
3578:   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");
3579:   if (!A->rmap->N && !A->cmap->N) return(0);
3580:   if (!A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
3581:   MatCheckPreallocated(A,1);

3583:   if (!A->ops->mattransposesolve) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Mat type %s",((PetscObject)A)->type_name);
3584:   PetscLogEventBegin(MAT_MatTrSolve,A,Bt,X,0);
3585:   (*A->ops->mattransposesolve)(A,Bt,X);
3586:   PetscLogEventEnd(MAT_MatTrSolve,A,Bt,X,0);
3587:   PetscObjectStateIncrease((PetscObject)X);
3588:   return(0);
3589: }

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

3595:    Neighbor-wise Collective on Mat and Vec

3597:    Input Parameters:
3598: +  mat - the factored matrix
3599: -  b - the right-hand-side vector

3601:    Output Parameter:
3602: .  x - the result vector

3604:    Notes:
3605:    MatSolve() should be used for most applications, as it performs
3606:    a forward solve followed by a backward solve.

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

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

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

3621:    Level: developer

3623:    Concepts: matrices^forward solves

3625: .seealso: MatSolve(), MatBackwardSolve()
3626: @*/
3627: PetscErrorCode MatForwardSolve(Mat mat,Vec b,Vec x)
3628: {

3638:   if (x == b) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"x and b must be different vectors");
3639:   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);
3640:   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);
3641:   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);
3642:   if (!mat->rmap->N && !mat->cmap->N) return(0);
3643:   if (!mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
3644:   MatCheckPreallocated(mat,1);

3646:   if (!mat->ops->forwardsolve) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
3647:   PetscLogEventBegin(MAT_ForwardSolve,mat,b,x,0);
3648:   (*mat->ops->forwardsolve)(mat,b,x);
3649:   PetscLogEventEnd(MAT_ForwardSolve,mat,b,x,0);
3650:   PetscObjectStateIncrease((PetscObject)x);
3651:   return(0);
3652: }

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

3658:    Neighbor-wise Collective on Mat and Vec

3660:    Input Parameters:
3661: +  mat - the factored matrix
3662: -  b - the right-hand-side vector

3664:    Output Parameter:
3665: .  x - the result vector

3667:    Notes:
3668:    MatSolve() should be used for most applications, as it performs
3669:    a forward solve followed by a backward solve.

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

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

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

3684:    Level: developer

3686:    Concepts: matrices^backward solves

3688: .seealso: MatSolve(), MatForwardSolve()
3689: @*/
3690: PetscErrorCode MatBackwardSolve(Mat mat,Vec b,Vec x)
3691: {

3701:   if (x == b) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"x and b must be different vectors");
3702:   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);
3703:   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);
3704:   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);
3705:   if (!mat->rmap->N && !mat->cmap->N) return(0);
3706:   if (!mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
3707:   MatCheckPreallocated(mat,1);

3709:   if (!mat->ops->backwardsolve) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
3710:   PetscLogEventBegin(MAT_BackwardSolve,mat,b,x,0);
3711:   (*mat->ops->backwardsolve)(mat,b,x);
3712:   PetscLogEventEnd(MAT_BackwardSolve,mat,b,x,0);
3713:   PetscObjectStateIncrease((PetscObject)x);
3714:   return(0);
3715: }

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

3720:    Neighbor-wise Collective on Mat and Vec

3722:    Input Parameters:
3723: +  mat - the factored matrix
3724: .  b - the right-hand-side vector
3725: -  y - the vector to be added to

3727:    Output Parameter:
3728: .  x - the result vector

3730:    Notes:
3731:    The vectors b and x cannot be the same.  I.e., one cannot
3732:    call MatSolveAdd(A,x,y,x).

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

3738:    Level: developer

3740:    Concepts: matrices^triangular solves

3742: .seealso: MatSolve(), MatSolveTranspose(), MatSolveTransposeAdd()
3743: @*/
3744: PetscErrorCode MatSolveAdd(Mat mat,Vec b,Vec y,Vec x)
3745: {
3746:   PetscScalar    one = 1.0;
3747:   Vec            tmp;

3759:   if (x == b) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"x and b must be different vectors");
3760:   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);
3761:   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);
3762:   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);
3763:   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);
3764:   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);
3765:   if (!mat->rmap->N && !mat->cmap->N) return(0);
3766:   if (!mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
3767:   MatCheckPreallocated(mat,1);

3769:   PetscLogEventBegin(MAT_SolveAdd,mat,b,x,y);
3770:   if (mat->ops->solveadd) {
3771:     (*mat->ops->solveadd)(mat,b,y,x);
3772:   } else {
3773:     /* do the solve then the add manually */
3774:     if (x != y) {
3775:       MatSolve(mat,b,x);
3776:       VecAXPY(x,one,y);
3777:     } else {
3778:       VecDuplicate(x,&tmp);
3779:       PetscLogObjectParent((PetscObject)mat,(PetscObject)tmp);
3780:       VecCopy(x,tmp);
3781:       MatSolve(mat,b,x);
3782:       VecAXPY(x,one,tmp);
3783:       VecDestroy(&tmp);
3784:     }
3785:   }
3786:   PetscLogEventEnd(MAT_SolveAdd,mat,b,x,y);
3787:   PetscObjectStateIncrease((PetscObject)x);
3788:   return(0);
3789: }

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

3794:    Neighbor-wise Collective on Mat and Vec

3796:    Input Parameters:
3797: +  mat - the factored matrix
3798: -  b - the right-hand-side vector

3800:    Output Parameter:
3801: .  x - the result vector

3803:    Notes:
3804:    The vectors b and x cannot be the same.  I.e., one cannot
3805:    call MatSolveTranspose(A,x,x).

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

3811:    Level: developer

3813:    Concepts: matrices^triangular solves

3815: .seealso: MatSolve(), MatSolveAdd(), MatSolveTransposeAdd()
3816: @*/
3817: PetscErrorCode MatSolveTranspose(Mat mat,Vec b,Vec x)
3818: {

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

3847: /*@
3848:    MatSolveTransposeAdd - Computes x = y + inv(Transpose(A)) b, given a
3849:                       factored matrix.

3851:    Neighbor-wise Collective on Mat and Vec

3853:    Input Parameters:
3854: +  mat - the factored matrix
3855: .  b - the right-hand-side vector
3856: -  y - the vector to be added to

3858:    Output Parameter:
3859: .  x - the result vector

3861:    Notes:
3862:    The vectors b and x cannot be the same.  I.e., one cannot
3863:    call MatSolveTransposeAdd(A,x,y,x).

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

3869:    Level: developer

3871:    Concepts: matrices^triangular solves

3873: .seealso: MatSolve(), MatSolveAdd(), MatSolveTranspose()
3874: @*/
3875: PetscErrorCode MatSolveTransposeAdd(Mat mat,Vec b,Vec y,Vec x)
3876: {
3877:   PetscScalar    one = 1.0;
3879:   Vec            tmp;

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

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

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

3930:    Neighbor-wise Collective on Mat and Vec

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

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

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

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

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

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

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

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

3976:    Vectors x and b CANNOT be the same

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

3980:    Level: developer

3982:    Concepts: matrices^relaxation
3983:    Concepts: matrices^SOR
3984:    Concepts: matrices^Gauss-Seidel

3986: @*/
3987: PetscErrorCode MatSOR(Mat mat,Vec b,PetscReal omega,MatSORType flag,PetscReal shift,PetscInt its,PetscInt lits,Vec x)
3988: {

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

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

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

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

4041: /*@
4042:    MatCopy - Copys a matrix to another matrix.

4044:    Collective on Mat

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

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

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

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

4061:    Level: intermediate

4063:    Concepts: matrices^copying

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

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

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

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

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

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

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

4109:    Collective on Mat

4111:    Input Parameters:
4112: +  mat - the matrix
4113: .  newtype - new matrix type.  Use MATSAME to create a new matrix of the
4114:    same type as the original matrix.
4115: -  reuse - denotes if the destination matrix is to be created or reused.
4116:    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
4117:    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).

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

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

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

4131:    Level: intermediate

4133:    Concepts: matrices^converting between storage formats

4135: .seealso: MatCopy(), MatDuplicate()
4136: @*/
4137: PetscErrorCode MatConvert(Mat mat, MatType newtype,MatReuse reuse,Mat *M)
4138: {
4140:   PetscBool      sametype,issame,flg;
4141:   char           convname[256],mtype[256];
4142:   Mat            B;

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

4152:   PetscOptionsGetString(((PetscObject)mat)->options,((PetscObject)mat)->prefix,"-matconvert_type",mtype,256,&flg);
4153:   if (flg) {
4154:     newtype = mtype;
4155:   }
4156:   PetscObjectTypeCompare((PetscObject)mat,newtype,&sametype);
4157:   PetscStrcmp(newtype,"same",&issame);
4158:   if ((reuse == MAT_INPLACE_MATRIX) && (mat != *M)) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"MAT_INPLACE_MATRIX requires same input and output matrix");
4159:   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");

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

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

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

4210:     /* 2)  See if a specialized converter is known to the desired matrix class. */
4211:     MatCreate(PetscObjectComm((PetscObject)mat),&B);
4212:     MatSetSizes(B,mat->rmap->n,mat->cmap->n,mat->rmap->N,mat->cmap->N);
4213:     MatSetType(B,newtype);
4214:     for (i=0; i<3; i++) {
4215:       PetscStrncpy(convname,"MatConvert_",sizeof(convname));
4216:       PetscStrlcat(convname,((PetscObject)mat)->type_name,sizeof(convname));
4217:       PetscStrlcat(convname,"_",sizeof(convname));
4218:       PetscStrlcat(convname,prefix[i],sizeof(convname));
4219:       PetscStrlcat(convname,newtype,sizeof(convname));
4220:       PetscStrlcat(convname,"_C",sizeof(convname));
4221:       PetscObjectQueryFunction((PetscObject)B,convname,&conv);
4222:       if (conv) {
4223:         MatDestroy(&B);
4224:         goto foundconv;
4225:       }
4226:     }

4228:     /* 3) See if a good general converter is registered for the desired class */
4229:     conv = B->ops->convertfrom;
4230:     MatDestroy(&B);
4231:     if (conv) goto foundconv;

4233:     /* 4) See if a good general converter is known for the current matrix */
4234:     if (mat->ops->convert) {
4235:       conv = mat->ops->convert;
4236:     }
4237:     if (conv) goto foundconv;

4239:     /* 5) Use a really basic converter. */
4240:     conv = MatConvert_Basic;

4242: foundconv:
4243:     PetscLogEventBegin(MAT_Convert,mat,0,0,0);
4244:     (*conv)(mat,newtype,reuse,M);
4245:     if (mat->rmap->mapping && mat->cmap->mapping && !(*M)->rmap->mapping && !(*M)->cmap->mapping) {
4246:       /* the block sizes must be same if the mappings are copied over */
4247:       (*M)->rmap->bs = mat->rmap->bs;
4248:       (*M)->cmap->bs = mat->cmap->bs;
4249:       PetscObjectReference((PetscObject)mat->rmap->mapping);
4250:       PetscObjectReference((PetscObject)mat->cmap->mapping);
4251:       (*M)->rmap->mapping = mat->rmap->mapping;
4252:       (*M)->cmap->mapping = mat->cmap->mapping;
4253:     }
4254:     (*M)->stencil.dim = mat->stencil.dim;
4255:     (*M)->stencil.noc = mat->stencil.noc;
4256:     for (i=0; i<=mat->stencil.dim; i++) {
4257:       (*M)->stencil.dims[i]   = mat->stencil.dims[i];
4258:       (*M)->stencil.starts[i] = mat->stencil.starts[i];
4259:     }
4260:     PetscLogEventEnd(MAT_Convert,mat,0,0,0);
4261:   }
4262:   PetscObjectStateIncrease((PetscObject)*M);

4264:   /* Copy Mat options */
4265:   if (mat->symmetric) {MatSetOption(*M,MAT_SYMMETRIC,PETSC_TRUE);}
4266:   if (mat->hermitian) {MatSetOption(*M,MAT_HERMITIAN,PETSC_TRUE);}
4267:   return(0);
4268: }

4270: /*@C
4271:    MatFactorGetSolverType - Returns name of the package providing the factorization routines

4273:    Not Collective

4275:    Input Parameter:
4276: .  mat - the matrix, must be a factored matrix

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

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

4285:    Level: intermediate

4287: .seealso: MatCopy(), MatDuplicate(), MatGetFactorAvailable(), MatGetFactor()
4288: @*/
4289: PetscErrorCode MatFactorGetSolverType(Mat mat, MatSolverType *type)
4290: {
4291:   PetscErrorCode ierr, (*conv)(Mat,MatSolverType*);

4296:   if (!mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Only for factored matrix");
4297:   PetscObjectQueryFunction((PetscObject)mat,"MatFactorGetSolverType_C",&conv);
4298:   if (!conv) {
4299:     *type = MATSOLVERPETSC;
4300:   } else {
4301:     (*conv)(mat,type);
4302:   }
4303:   return(0);
4304: }

4306: typedef struct _MatSolverTypeForSpecifcType* MatSolverTypeForSpecifcType;
4307: struct _MatSolverTypeForSpecifcType {
4308:   MatType                        mtype;
4309:   PetscErrorCode                 (*getfactor[4])(Mat,MatFactorType,Mat*);
4310:   MatSolverTypeForSpecifcType next;
4311: };

4313: typedef struct _MatSolverTypeHolder* MatSolverTypeHolder;
4314: struct _MatSolverTypeHolder {
4315:   char                           *name;
4316:   MatSolverTypeForSpecifcType handlers;
4317:   MatSolverTypeHolder         next;
4318: };

4320: static MatSolverTypeHolder MatSolverTypeHolders = NULL;

4322: /*@C
4323:    MatSolvePackageRegister - Registers a MatSolverType that works for a particular matrix type

4325:    Input Parameters:
4326: +    package - name of the package, for example petsc or superlu
4327: .    mtype - the matrix type that works with this package
4328: .    ftype - the type of factorization supported by the package
4329: -    getfactor - routine that will create the factored matrix ready to be used

4331:     Level: intermediate

4333: .seealso: MatCopy(), MatDuplicate(), MatGetFactorAvailable()
4334: @*/
4335: PetscErrorCode MatSolverTypeRegister(MatSolverType package,MatType mtype,MatFactorType ftype,PetscErrorCode (*getfactor)(Mat,MatFactorType,Mat*))
4336: {
4337:   PetscErrorCode              ierr;
4338:   MatSolverTypeHolder         next = MatSolverTypeHolders,prev;
4339:   PetscBool                   flg;
4340:   MatSolverTypeForSpecifcType inext,iprev = NULL;

4343:   MatInitializePackage();
4344:   if (!next) {
4345:     PetscNew(&MatSolverTypeHolders);
4346:     PetscStrallocpy(package,&MatSolverTypeHolders->name);
4347:     PetscNew(&MatSolverTypeHolders->handlers);
4348:     PetscStrallocpy(mtype,(char **)&MatSolverTypeHolders->handlers->mtype);
4349:     MatSolverTypeHolders->handlers->getfactor[(int)ftype-1] = getfactor;
4350:     return(0);
4351:   }
4352:   while (next) {
4353:     PetscStrcasecmp(package,next->name,&flg);
4354:     if (flg) {
4355:       if (!next->handlers) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"MatSolverTypeHolder is missing handlers");
4356:       inext = next->handlers;
4357:       while (inext) {
4358:         PetscStrcasecmp(mtype,inext->mtype,&flg);
4359:         if (flg) {
4360:           inext->getfactor[(int)ftype-1] = getfactor;
4361:           return(0);
4362:         }
4363:         iprev = inext;
4364:         inext = inext->next;
4365:       }
4366:       PetscNew(&iprev->next);
4367:       PetscStrallocpy(mtype,(char **)&iprev->next->mtype);
4368:       iprev->next->getfactor[(int)ftype-1] = getfactor;
4369:       return(0);
4370:     }
4371:     prev = next;
4372:     next = next->next;
4373:   }
4374:   PetscNew(&prev->next);
4375:   PetscStrallocpy(package,&prev->next->name);
4376:   PetscNew(&prev->next->handlers);
4377:   PetscStrallocpy(mtype,(char **)&prev->next->handlers->mtype);
4378:   prev->next->handlers->getfactor[(int)ftype-1] = getfactor;
4379:   return(0);
4380: }

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

4385:    Input Parameters:
4386: +    package - name of the package, for example petsc or superlu
4387: .    ftype - the type of factorization supported by the package
4388: -    mtype - the matrix type that works with this package

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

4395:     Level: intermediate

4397: .seealso: MatCopy(), MatDuplicate(), MatGetFactorAvailable()
4398: @*/
4399: PetscErrorCode MatSolverTypeGet(MatSolverType package,MatType mtype,MatFactorType ftype,PetscBool *foundpackage,PetscBool *foundmtype,PetscErrorCode (**getfactor)(Mat,MatFactorType,Mat*))
4400: {
4401:   PetscErrorCode                 ierr;
4402:   MatSolverTypeHolder         next = MatSolverTypeHolders;
4403:   PetscBool                      flg;
4404:   MatSolverTypeForSpecifcType inext;

4407:   if (foundpackage) *foundpackage = PETSC_FALSE;
4408:   if (foundmtype)   *foundmtype   = PETSC_FALSE;
4409:   if (getfactor)    *getfactor    = NULL;

4411:   if (package) {
4412:     while (next) {
4413:       PetscStrcasecmp(package,next->name,&flg);
4414:       if (flg) {
4415:         if (foundpackage) *foundpackage = PETSC_TRUE;
4416:         inext = next->handlers;
4417:         while (inext) {
4418:           PetscStrbeginswith(mtype,inext->mtype,&flg);
4419:           if (flg) {
4420:             if (foundmtype) *foundmtype = PETSC_TRUE;
4421:             if (getfactor)  *getfactor  = inext->getfactor[(int)ftype-1];
4422:             return(0);
4423:           }
4424:           inext = inext->next;
4425:         }
4426:       }
4427:       next = next->next;
4428:     }
4429:   } else {
4430:     while (next) {
4431:       inext = next->handlers;
4432:       while (inext) {
4433:         PetscStrbeginswith(mtype,inext->mtype,&flg);
4434:         if (flg && inext->getfactor[(int)ftype-1]) {
4435:           if (foundpackage) *foundpackage = PETSC_TRUE;
4436:           if (foundmtype)   *foundmtype   = PETSC_TRUE;
4437:           if (getfactor)    *getfactor    = inext->getfactor[(int)ftype-1];
4438:           return(0);
4439:         }
4440:         inext = inext->next;
4441:       }
4442:       next = next->next;
4443:     }
4444:   }
4445:   return(0);
4446: }

4448: PetscErrorCode MatSolverTypeDestroy(void)
4449: {
4450:   PetscErrorCode              ierr;
4451:   MatSolverTypeHolder         next = MatSolverTypeHolders,prev;
4452:   MatSolverTypeForSpecifcType inext,iprev;

4455:   while (next) {
4456:     PetscFree(next->name);
4457:     inext = next->handlers;
4458:     while (inext) {
4459:       PetscFree(inext->mtype);
4460:       iprev = inext;
4461:       inext = inext->next;
4462:       PetscFree(iprev);
4463:     }
4464:     prev = next;
4465:     next = next->next;
4466:     PetscFree(prev);
4467:   }
4468:   MatSolverTypeHolders = NULL;
4469:   return(0);
4470: }

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

4475:    Collective on Mat

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

4482:    Output Parameters:
4483: .  f - the factor matrix used with MatXXFactorSymbolic() calls

4485:    Notes:
4486:       Some PETSc matrix formats have alternative solvers available that are contained in alternative packages
4487:      such as pastix, superlu, mumps etc.

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

4491:    Level: intermediate

4493: .seealso: MatCopy(), MatDuplicate(), MatGetFactorAvailable()
4494: @*/
4495: PetscErrorCode MatGetFactor(Mat mat, MatSolverType type,MatFactorType ftype,Mat *f)
4496: {
4497:   PetscErrorCode ierr,(*conv)(Mat,MatFactorType,Mat*);
4498:   PetscBool      foundpackage,foundmtype;


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

4507:   MatSolverTypeGet(type,((PetscObject)mat)->type_name,ftype,&foundpackage,&foundmtype,&conv);
4508:   if (!foundpackage) {
4509:     if (type) {
4510:       SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_MISSING_FACTOR,"Could not locate solver package %s. Perhaps you must ./configure with --download-%s",type,type);
4511:     } else {
4512:       SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_MISSING_FACTOR,"Could not locate a solver package. Perhaps you must ./configure with --download-<package>");
4513:     }
4514:   }

4516:   if (!foundmtype) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_MISSING_FACTOR,"MatSolverType %s does not support matrix type %s",type,((PetscObject)mat)->type_name);
4517:   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);

4519: #if defined(PETSC_USE_COMPLEX)
4520:   if (mat->hermitian && !mat->symmetric && (ftype == MAT_FACTOR_CHOLESKY||ftype == MAT_FACTOR_ICC)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Hermitian CHOLESKY or ICC Factor is not supported");
4521: #endif

4523:   (*conv)(mat,ftype,f);
4524:   return(0);
4525: }

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

4530:    Not Collective

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

4537:    Output Parameter:
4538: .    flg - PETSC_TRUE if the factorization is available

4540:    Notes:
4541:       Some PETSc matrix formats have alternative solvers available that are contained in alternative packages
4542:      such as pastix, superlu, mumps etc.

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

4546:    Level: intermediate

4548: .seealso: MatCopy(), MatDuplicate(), MatGetFactor()
4549: @*/
4550: PetscErrorCode MatGetFactorAvailable(Mat mat, MatSolverType type,MatFactorType ftype,PetscBool  *flg)
4551: {
4552:   PetscErrorCode ierr, (*gconv)(Mat,MatFactorType,Mat*);


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

4561:   *flg = PETSC_FALSE;
4562:   MatSolverTypeGet(type,((PetscObject)mat)->type_name,ftype,NULL,NULL,&gconv);
4563:   if (gconv) {
4564:     *flg = PETSC_TRUE;
4565:   }
4566:   return(0);
4567: }

4569:  #include <petscdmtypes.h>

4571: /*@
4572:    MatDuplicate - Duplicates a matrix including the non-zero structure.

4574:    Collective on Mat

4576:    Input Parameters:
4577: +  mat - the matrix
4578: -  op - One of MAT_DO_NOT_COPY_VALUES, MAT_COPY_VALUES, or MAT_SHARE_NONZERO_PATTERN.
4579:         See the manual page for MatDuplicateOption for an explanation of these options.

4581:    Output Parameter:
4582: .  M - pointer to place new matrix

4584:    Level: intermediate

4586:    Concepts: matrices^duplicating

4588:    Notes:
4589:     You cannot change the nonzero pattern for the parent or child matrix if you use MAT_SHARE_NONZERO_PATTERN.
4590:     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.

4592: .seealso: MatCopy(), MatConvert(), MatDuplicateOption
4593: @*/
4594: PetscErrorCode MatDuplicate(Mat mat,MatDuplicateOption op,Mat *M)
4595: {
4597:   Mat            B;
4598:   PetscInt       i;
4599:   DM             dm;
4600:   void           (*viewf)(void);

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

4610:   *M = 0;
4611:   if (!mat->ops->duplicate) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Not written for this matrix type");
4612:   PetscLogEventBegin(MAT_Convert,mat,0,0,0);
4613:   (*mat->ops->duplicate)(mat,op,M);
4614:   B    = *M;

4616:   MatGetOperation(mat,MATOP_VIEW,&viewf);
4617:   if (viewf) {
4618:     MatSetOperation(B,MATOP_VIEW,viewf);
4619:   }

4621:   B->stencil.dim = mat->stencil.dim;
4622:   B->stencil.noc = mat->stencil.noc;
4623:   for (i=0; i<=mat->stencil.dim; i++) {
4624:     B->stencil.dims[i]   = mat->stencil.dims[i];
4625:     B->stencil.starts[i] = mat->stencil.starts[i];
4626:   }

4628:   B->nooffproczerorows = mat->nooffproczerorows;
4629:   B->nooffprocentries  = mat->nooffprocentries;

4631:   PetscObjectQuery((PetscObject) mat, "__PETSc_dm", (PetscObject*) &dm);
4632:   if (dm) {
4633:     PetscObjectCompose((PetscObject) B, "__PETSc_dm", (PetscObject) dm);
4634:   }
4635:   PetscLogEventEnd(MAT_Convert,mat,0,0,0);
4636:   PetscObjectStateIncrease((PetscObject)B);
4637:   return(0);
4638: }

4640: /*@
4641:    MatGetDiagonal - Gets the diagonal of a matrix.

4643:    Logically Collective on Mat and Vec

4645:    Input Parameters:
4646: +  mat - the matrix
4647: -  v - the vector for storing the diagonal

4649:    Output Parameter:
4650: .  v - the diagonal of the matrix

4652:    Level: intermediate

4654:    Note:
4655:    Currently only correct in parallel for square matrices.

4657:    Concepts: matrices^accessing diagonals

4659: .seealso: MatGetRow(), MatCreateSubMatrices(), MatCreateSubMatrix(), MatGetRowMaxAbs()
4660: @*/
4661: PetscErrorCode MatGetDiagonal(Mat mat,Vec v)
4662: {

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

4673:   (*mat->ops->getdiagonal)(mat,v);
4674:   PetscObjectStateIncrease((PetscObject)v);
4675:   return(0);
4676: }

4678: /*@C
4679:    MatGetRowMin - Gets the minimum value (of the real part) of each
4680:         row of the matrix

4682:    Logically Collective on Mat and Vec

4684:    Input Parameters:
4685: .  mat - the matrix

4687:    Output Parameter:
4688: +  v - the vector for storing the maximums
4689: -  idx - the indices of the column found for each row (optional)

4691:    Level: intermediate

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

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

4699:    Concepts: matrices^getting row maximums

4701: .seealso: MatGetDiagonal(), MatCreateSubMatrices(), MatCreateSubMatrix(), MatGetRowMaxAbs(),
4702:           MatGetRowMax()
4703: @*/
4704: PetscErrorCode MatGetRowMin(Mat mat,Vec v,PetscInt idx[])
4705: {

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

4716:   (*mat->ops->getrowmin)(mat,v,idx);
4717:   PetscObjectStateIncrease((PetscObject)v);
4718:   return(0);
4719: }

4721: /*@C
4722:    MatGetRowMinAbs - Gets the minimum value (in absolute value) of each
4723:         row of the matrix

4725:    Logically Collective on Mat and Vec

4727:    Input Parameters:
4728: .  mat - the matrix

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

4734:    Level: intermediate

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

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

4742:    Concepts: matrices^getting row maximums

4744: .seealso: MatGetDiagonal(), MatCreateSubMatrices(), MatCreateSubMatrix(), MatGetRowMax(), MatGetRowMaxAbs(), MatGetRowMin()
4745: @*/
4746: PetscErrorCode MatGetRowMinAbs(Mat mat,Vec v,PetscInt idx[])
4747: {

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

4759:   (*mat->ops->getrowminabs)(mat,v,idx);
4760:   PetscObjectStateIncrease((PetscObject)v);
4761:   return(0);
4762: }

4764: /*@C
4765:    MatGetRowMax - Gets the maximum value (of the real part) of each
4766:         row of the matrix

4768:    Logically Collective on Mat and Vec

4770:    Input Parameters:
4771: .  mat - the matrix

4773:    Output Parameter:
4774: +  v - the vector for storing the maximums
4775: -  idx - the indices of the column found for each row (optional)

4777:    Level: intermediate

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

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

4785:    Concepts: matrices^getting row maximums

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

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

4801:   (*mat->ops->getrowmax)(mat,v,idx);
4802:   PetscObjectStateIncrease((PetscObject)v);
4803:   return(0);
4804: }

4806: /*@C
4807:    MatGetRowMaxAbs - Gets the maximum value (in absolute value) of each
4808:         row of the matrix

4810:    Logically Collective on Mat and Vec

4812:    Input Parameters:
4813: .  mat - the matrix

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

4819:    Level: intermediate

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

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

4827:    Concepts: matrices^getting row maximums

4829: .seealso: MatGetDiagonal(), MatCreateSubMatrices(), MatCreateSubMatrix(), MatGetRowMax(), MatGetRowMin()
4830: @*/
4831: PetscErrorCode MatGetRowMaxAbs(Mat mat,Vec v,PetscInt idx[])
4832: {

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

4844:   (*mat->ops->getrowmaxabs)(mat,v,idx);
4845:   PetscObjectStateIncrease((PetscObject)v);
4846:   return(0);
4847: }

4849: /*@
4850:    MatGetRowSum - Gets the sum of each row of the matrix

4852:    Logically or Neighborhood Collective on Mat and Vec

4854:    Input Parameters:
4855: .  mat - the matrix

4857:    Output Parameter:
4858: .  v - the vector for storing the sum of rows

4860:    Level: intermediate

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

4865:    Concepts: matrices^getting row sums

4867: .seealso: MatGetDiagonal(), MatCreateSubMatrices(), MatCreateSubMatrix(), MatGetRowMax(), MatGetRowMin()
4868: @*/
4869: PetscErrorCode MatGetRowSum(Mat mat, Vec v)
4870: {
4871:   Vec            ones;

4878:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
4879:   MatCheckPreallocated(mat,1);
4880:   MatCreateVecs(mat,&ones,NULL);
4881:   VecSet(ones,1.);
4882:   MatMult(mat,ones,v);
4883:   VecDestroy(&ones);
4884:   return(0);
4885: }

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

4890:    Collective on Mat

4892:    Input Parameter:
4893: +  mat - the matrix to transpose
4894: -  reuse - either MAT_INITIAL_MATRIX, MAT_REUSE_MATRIX, or MAT_INPLACE_MATRIX

4896:    Output Parameters:
4897: .  B - the transpose

4899:    Notes:
4900:      If you use MAT_INPLACE_MATRIX then you must pass in &mat for B

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

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

4906:    Level: intermediate

4908:    Concepts: matrices^transposing

4910: .seealso: MatMultTranspose(), MatMultTransposeAdd(), MatIsTranspose(), MatReuse
4911: @*/
4912: PetscErrorCode MatTranspose(Mat mat,MatReuse reuse,Mat *B)
4913: {

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

4926:   PetscLogEventBegin(MAT_Transpose,mat,0,0,0);
4927:   (*mat->ops->transpose)(mat,reuse,B);
4928:   PetscLogEventEnd(MAT_Transpose,mat,0,0,0);
4929:   if (B) {PetscObjectStateIncrease((PetscObject)*B);}
4930:   return(0);
4931: }

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

4937:    Collective on Mat

4939:    Input Parameter:
4940: +  A - the matrix to test
4941: -  B - the matrix to test against, this can equal the first parameter

4943:    Output Parameters:
4944: .  flg - the result

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

4951:    Level: intermediate

4953:    Concepts: matrices^transposing, matrix^symmetry

4955: .seealso: MatTranspose(), MatIsSymmetric(), MatIsHermitian()
4956: @*/
4957: PetscErrorCode MatIsTranspose(Mat A,Mat B,PetscReal tol,PetscBool  *flg)
4958: {
4959:   PetscErrorCode ierr,(*f)(Mat,Mat,PetscReal,PetscBool*),(*g)(Mat,Mat,PetscReal,PetscBool*);

4965:   PetscObjectQueryFunction((PetscObject)A,"MatIsTranspose_C",&f);
4966:   PetscObjectQueryFunction((PetscObject)B,"MatIsTranspose_C",&g);
4967:   *flg = PETSC_FALSE;
4968:   if (f && g) {
4969:     if (f == g) {
4970:       (*f)(A,B,tol,flg);
4971:     } else SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_NOTSAMETYPE,"Matrices do not have the same comparator for symmetry test");
4972:   } else {
4973:     MatType mattype;
4974:     if (!f) {
4975:       MatGetType(A,&mattype);
4976:     } else {
4977:       MatGetType(B,&mattype);
4978:     }
4979:     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type <%s> does not support checking for transpose",mattype);
4980:   }
4981:   return(0);
4982: }

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

4987:    Collective on Mat

4989:    Input Parameter:
4990: +  mat - the matrix to transpose and complex conjugate
4991: -  reuse - MAT_INITIAL_MATRIX to create a new matrix, MAT_INPLACE_MATRIX to reuse the first argument to store the transpose

4993:    Output Parameters:
4994: .  B - the Hermitian

4996:    Level: intermediate

4998:    Concepts: matrices^transposing, complex conjugatex

5000: .seealso: MatTranspose(), MatMultTranspose(), MatMultTransposeAdd(), MatIsTranspose(), MatReuse
5001: @*/
5002: PetscErrorCode MatHermitianTranspose(Mat mat,MatReuse reuse,Mat *B)
5003: {

5007:   MatTranspose(mat,reuse,B);
5008: #if defined(PETSC_USE_COMPLEX)
5009:   MatConjugate(*B);
5010: #endif
5011:   return(0);
5012: }

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

5017:    Collective on Mat

5019:    Input Parameter:
5020: +  A - the matrix to test
5021: -  B - the matrix to test against, this can equal the first parameter

5023:    Output Parameters:
5024: .  flg - the result

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

5031:    Level: intermediate

5033:    Concepts: matrices^transposing, matrix^symmetry

5035: .seealso: MatTranspose(), MatIsSymmetric(), MatIsHermitian(), MatIsTranspose()
5036: @*/
5037: PetscErrorCode MatIsHermitianTranspose(Mat A,Mat B,PetscReal tol,PetscBool  *flg)
5038: {
5039:   PetscErrorCode ierr,(*f)(Mat,Mat,PetscReal,PetscBool*),(*g)(Mat,Mat,PetscReal,PetscBool*);

5045:   PetscObjectQueryFunction((PetscObject)A,"MatIsHermitianTranspose_C",&f);
5046:   PetscObjectQueryFunction((PetscObject)B,"MatIsHermitianTranspose_C",&g);
5047:   if (f && g) {
5048:     if (f==g) {
5049:       (*f)(A,B,tol,flg);
5050:     } else SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_NOTSAMETYPE,"Matrices do not have the same comparator for Hermitian test");
5051:   }
5052:   return(0);
5053: }

5055: /*@
5056:    MatPermute - Creates a new matrix with rows and columns permuted from the
5057:    original.

5059:    Collective on Mat

5061:    Input Parameters:
5062: +  mat - the matrix to permute
5063: .  row - row permutation, each processor supplies only the permutation for its rows
5064: -  col - column permutation, each processor supplies only the permutation for its columns

5066:    Output Parameters:
5067: .  B - the permuted matrix

5069:    Level: advanced

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

5075:    Concepts: matrices^permuting

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

5079: @*/
5080: PetscErrorCode MatPermute(Mat mat,IS row,IS col,Mat *B)
5081: {

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

5095:   (*mat->ops->permute)(mat,row,col,B);
5096:   PetscObjectStateIncrease((PetscObject)*B);
5097:   return(0);
5098: }

5100: /*@
5101:    MatEqual - Compares two matrices.

5103:    Collective on Mat

5105:    Input Parameters:
5106: +  A - the first matrix
5107: -  B - the second matrix

5109:    Output Parameter:
5110: .  flg - PETSC_TRUE if the matrices are equal; PETSC_FALSE otherwise.

5112:    Level: intermediate

5114:    Concepts: matrices^equality between
5115: @*/
5116: PetscErrorCode MatEqual(Mat A,Mat B,PetscBool  *flg)
5117: {

5127:   MatCheckPreallocated(B,2);
5128:   if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
5129:   if (!B->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
5130:   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);
5131:   if (!A->ops->equal) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Mat type %s",((PetscObject)A)->type_name);
5132:   if (!B->ops->equal) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Mat type %s",((PetscObject)B)->type_name);
5133:   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);
5134:   MatCheckPreallocated(A,1);

5136:   (*A->ops->equal)(A,B,flg);
5137:   return(0);
5138: }

5140: /*@
5141:    MatDiagonalScale - Scales a matrix on the left and right by diagonal
5142:    matrices that are stored as vectors.  Either of the two scaling
5143:    matrices can be NULL.

5145:    Collective on Mat

5147:    Input Parameters:
5148: +  mat - the matrix to be scaled
5149: .  l - the left scaling vector (or NULL)
5150: -  r - the right scaling vector (or NULL)

5152:    Notes:
5153:    MatDiagonalScale() computes A = LAR, where
5154:    L = a diagonal matrix (stored as a vector), R = a diagonal matrix (stored as a vector)
5155:    The L scales the rows of the matrix, the R scales the columns of the matrix.

5157:    Level: intermediate

5159:    Concepts: matrices^diagonal scaling
5160:    Concepts: diagonal scaling of matrices

5162: .seealso: MatScale(), MatShift(), MatDiagonalSet()
5163: @*/
5164: PetscErrorCode MatDiagonalScale(Mat mat,Vec l,Vec r)
5165: {

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

5178:   PetscLogEventBegin(MAT_Scale,mat,0,0,0);
5179:   (*mat->ops->diagonalscale)(mat,l,r);
5180:   PetscLogEventEnd(MAT_Scale,mat,0,0,0);
5181:   PetscObjectStateIncrease((PetscObject)mat);
5182: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
5183:   if (mat->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED) {
5184:     mat->valid_GPU_matrix = PETSC_OFFLOAD_CPU;
5185:   }
5186: #endif
5187:   return(0);
5188: }

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

5193:     Logically Collective on Mat

5195:     Input Parameters:
5196: +   mat - the matrix to be scaled
5197: -   a  - the scaling value

5199:     Output Parameter:
5200: .   mat - the scaled matrix

5202:     Level: intermediate

5204:     Concepts: matrices^scaling all entries

5206: .seealso: MatDiagonalScale()
5207: @*/
5208: PetscErrorCode MatScale(Mat mat,PetscScalar a)
5209: {

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

5221:   PetscLogEventBegin(MAT_Scale,mat,0,0,0);
5222:   if (a != (PetscScalar)1.0) {
5223:     (*mat->ops->scale)(mat,a);
5224:     PetscObjectStateIncrease((PetscObject)mat);
5225: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
5226:     if (mat->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED) {
5227:       mat->valid_GPU_matrix = PETSC_OFFLOAD_CPU;
5228:     }
5229: #endif
5230:   }
5231:   PetscLogEventEnd(MAT_Scale,mat,0,0,0);
5232:   return(0);
5233: }

5235: /*@
5236:    MatNorm - Calculates various norms of a matrix.

5238:    Collective on Mat

5240:    Input Parameters:
5241: +  mat - the matrix
5242: -  type - the type of norm, NORM_1, NORM_FROBENIUS, NORM_INFINITY

5244:    Output Parameters:
5245: .  nrm - the resulting norm

5247:    Level: intermediate

5249:    Concepts: matrices^norm
5250:    Concepts: norm^of matrix
5251: @*/
5252: PetscErrorCode MatNorm(Mat mat,NormType type,PetscReal *nrm)
5253: {


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

5266:   (*mat->ops->norm)(mat,type,nrm);
5267:   return(0);
5268: }

5270: /*
5271:      This variable is used to prevent counting of MatAssemblyBegin() that
5272:    are called from within a MatAssemblyEnd().
5273: */
5274: static PetscInt MatAssemblyEnd_InUse = 0;
5275: /*@
5276:    MatAssemblyBegin - Begins assembling the matrix.  This routine should
5277:    be called after completing all calls to MatSetValues().

5279:    Collective on Mat

5281:    Input Parameters:
5282: +  mat - the matrix
5283: -  type - type of assembly, either MAT_FLUSH_ASSEMBLY or MAT_FINAL_ASSEMBLY

5285:    Notes:
5286:    MatSetValues() generally caches the values.  The matrix is ready to
5287:    use only after MatAssemblyBegin() and MatAssemblyEnd() have been called.
5288:    Use MAT_FLUSH_ASSEMBLY when switching between ADD_VALUES and INSERT_VALUES
5289:    in MatSetValues(); use MAT_FINAL_ASSEMBLY for the final assembly before
5290:    using the matrix.

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

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

5300:    Level: beginner

5302:    Concepts: matrices^assembling

5304: .seealso: MatAssemblyEnd(), MatSetValues(), MatAssembled()
5305: @*/
5306: PetscErrorCode MatAssemblyBegin(Mat mat,MatAssemblyType type)
5307: {

5313:   MatCheckPreallocated(mat,1);
5314:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix.\nDid you forget to call MatSetUnfactored()?");
5315:   if (mat->assembled) {
5316:     mat->was_assembled = PETSC_TRUE;
5317:     mat->assembled     = PETSC_FALSE;
5318:   }
5319:   if (!MatAssemblyEnd_InUse) {
5320:     PetscLogEventBegin(MAT_AssemblyBegin,mat,0,0,0);
5321:     if (mat->ops->assemblybegin) {(*mat->ops->assemblybegin)(mat,type);}
5322:     PetscLogEventEnd(MAT_AssemblyBegin,mat,0,0,0);
5323:   } else if (mat->ops->assemblybegin) {
5324:     (*mat->ops->assemblybegin)(mat,type);
5325:   }
5326:   return(0);
5327: }

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

5333:    Not Collective

5335:    Input Parameter:
5336: .  mat - the matrix

5338:    Output Parameter:
5339: .  assembled - PETSC_TRUE or PETSC_FALSE

5341:    Level: advanced

5343:    Concepts: matrices^assembled?

5345: .seealso: MatAssemblyEnd(), MatSetValues(), MatAssemblyBegin()
5346: @*/
5347: PetscErrorCode MatAssembled(Mat mat,PetscBool  *assembled)
5348: {
5352:   *assembled = mat->assembled;
5353:   return(0);
5354: }

5356: /*@
5357:    MatAssemblyEnd - Completes assembling the matrix.  This routine should
5358:    be called after MatAssemblyBegin().

5360:    Collective on Mat

5362:    Input Parameters:
5363: +  mat - the matrix
5364: -  type - type of assembly, either MAT_FLUSH_ASSEMBLY or MAT_FINAL_ASSEMBLY

5366:    Options Database Keys:
5367: +  -mat_view ::ascii_info - Prints info on matrix at conclusion of MatEndAssembly()
5368: .  -mat_view ::ascii_info_detail - Prints more detailed info
5369: .  -mat_view - Prints matrix in ASCII format
5370: .  -mat_view ::ascii_matlab - Prints matrix in Matlab format
5371: .  -mat_view draw - PetscDraws nonzero structure of matrix, using MatView() and PetscDrawOpenX().
5372: .  -display <name> - Sets display name (default is host)
5373: .  -draw_pause <sec> - Sets number of seconds to pause after display
5374: .  -mat_view socket - Sends matrix to socket, can be accessed from Matlab (See Users-Manual: Chapter 12 Using MATLAB with PETSc )
5375: .  -viewer_socket_machine <machine> - Machine to use for socket
5376: .  -viewer_socket_port <port> - Port number to use for socket
5377: -  -mat_view binary:filename[:append] - Save matrix to file in binary format

5379:    Notes:
5380:    MatSetValues() generally caches the values.  The matrix is ready to
5381:    use only after MatAssemblyBegin() and MatAssemblyEnd() have been called.
5382:    Use MAT_FLUSH_ASSEMBLY when switching between ADD_VALUES and INSERT_VALUES
5383:    in MatSetValues(); use MAT_FINAL_ASSEMBLY for the final assembly before
5384:    using the matrix.

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

5390:    Level: beginner

5392: .seealso: MatAssemblyBegin(), MatSetValues(), PetscDrawOpenX(), PetscDrawCreate(), MatView(), MatAssembled(), PetscViewerSocketOpen()
5393: @*/
5394: PetscErrorCode MatAssemblyEnd(Mat mat,MatAssemblyType type)
5395: {
5396:   PetscErrorCode  ierr;
5397:   static PetscInt inassm = 0;
5398:   PetscBool       flg    = PETSC_FALSE;


5404:   inassm++;
5405:   MatAssemblyEnd_InUse++;
5406:   if (MatAssemblyEnd_InUse == 1) { /* Do the logging only the first time through */
5407:     PetscLogEventBegin(MAT_AssemblyEnd,mat,0,0,0);
5408:     if (mat->ops->assemblyend) {
5409:       (*mat->ops->assemblyend)(mat,type);
5410:     }
5411:     PetscLogEventEnd(MAT_AssemblyEnd,mat,0,0,0);
5412:   } else if (mat->ops->assemblyend) {
5413:     (*mat->ops->assemblyend)(mat,type);
5414:   }

5416:   /* Flush assembly is not a true assembly */
5417:   if (type != MAT_FLUSH_ASSEMBLY) {
5418:     mat->assembled = PETSC_TRUE; mat->num_ass++;
5419:   }
5420:   mat->insertmode = NOT_SET_VALUES;
5421:   MatAssemblyEnd_InUse--;
5422:   PetscObjectStateIncrease((PetscObject)mat);
5423:   if (!mat->symmetric_eternal) {
5424:     mat->symmetric_set              = PETSC_FALSE;
5425:     mat->hermitian_set              = PETSC_FALSE;
5426:     mat->structurally_symmetric_set = PETSC_FALSE;
5427:   }
5428: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
5429:   if (mat->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED) {
5430:     mat->valid_GPU_matrix = PETSC_OFFLOAD_CPU;
5431:   }
5432: #endif
5433:   if (inassm == 1 && type != MAT_FLUSH_ASSEMBLY) {
5434:     MatViewFromOptions(mat,NULL,"-mat_view");

5436:     if (mat->checksymmetryonassembly) {
5437:       MatIsSymmetric(mat,mat->checksymmetrytol,&flg);
5438:       if (flg) {
5439:         PetscPrintf(PetscObjectComm((PetscObject)mat),"Matrix is symmetric (tolerance %g)\n",(double)mat->checksymmetrytol);
5440:       } else {
5441:         PetscPrintf(PetscObjectComm((PetscObject)mat),"Matrix is not symmetric (tolerance %g)\n",(double)mat->checksymmetrytol);
5442:       }
5443:     }
5444:     if (mat->nullsp && mat->checknullspaceonassembly) {
5445:       MatNullSpaceTest(mat->nullsp,mat,NULL);
5446:     }
5447:   }
5448:   inassm--;
5449:   return(0);
5450: }

5452: /*@
5453:    MatSetOption - Sets a parameter option for a matrix. Some options
5454:    may be specific to certain storage formats.  Some options
5455:    determine how values will be inserted (or added). Sorted,
5456:    row-oriented input will generally assemble the fastest. The default
5457:    is row-oriented.

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

5461:    Input Parameters:
5462: +  mat - the matrix
5463: .  option - the option, one of those listed below (and possibly others),
5464: -  flg - turn the option on (PETSC_TRUE) or off (PETSC_FALSE)

5466:   Options Describing Matrix Structure:
5467: +    MAT_SPD - symmetric positive definite
5468: .    MAT_SYMMETRIC - symmetric in terms of both structure and value
5469: .    MAT_HERMITIAN - transpose is the complex conjugation
5470: .    MAT_STRUCTURALLY_SYMMETRIC - symmetric nonzero structure
5471: -    MAT_SYMMETRY_ETERNAL - if you would like the symmetry/Hermitian flag
5472:                             you set to be kept with all future use of the matrix
5473:                             including after MatAssemblyBegin/End() which could
5474:                             potentially change the symmetry structure, i.e. you
5475:                             KNOW the matrix will ALWAYS have the property you set.


5478:    Options For Use with MatSetValues():
5479:    Insert a logically dense subblock, which can be
5480: .    MAT_ROW_ORIENTED - row-oriented (default)

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

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

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

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

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

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

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

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

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

5534:    MAT_USE_HASH_TABLE indicates that a hash table be used to improve the
5535:    searches during matrix assembly. When this flag is set, the hash table
5536:    is created during the first Matrix Assembly. This hash table is
5537:    used the next time through, during MatSetVaules()/MatSetVaulesBlocked()
5538:    to improve the searching of indices. MAT_NEW_NONZERO_LOCATIONS flag
5539:    should be used with MAT_USE_HASH_TABLE flag. This option is currently
5540:    supported by MATMPIBAIJ format only.

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

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

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

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

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

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

5559:    Level: intermediate

5561:    Concepts: matrices^setting options

5563: .seealso:  MatOption, Mat

5565: @*/
5566: PetscErrorCode MatSetOption(Mat mat,MatOption op,PetscBool flg)
5567: {

5573:   if (op > 0) {
5576:   }

5578:   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);
5579:   if (!((PetscObject)mat)->type_name) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_TYPENOTSET,"Cannot set options until type and size have been set, see MatSetType() and MatSetSizes()");

5581:   switch (op) {
5582:   case MAT_NO_OFF_PROC_ENTRIES:
5583:     mat->nooffprocentries = flg;
5584:     return(0);
5585:     break;
5586:   case MAT_SUBSET_OFF_PROC_ENTRIES:
5587:     mat->subsetoffprocentries = flg;
5588:     return(0);
5589:   case MAT_NO_OFF_PROC_ZERO_ROWS:
5590:     mat->nooffproczerorows = flg;
5591:     return(0);
5592:     break;
5593:   case MAT_SPD:
5594:     mat->spd_set = PETSC_TRUE;
5595:     mat->spd     = flg;
5596:     if (flg) {
5597:       mat->symmetric                  = PETSC_TRUE;
5598:       mat->structurally_symmetric     = PETSC_TRUE;
5599:       mat->symmetric_set              = PETSC_TRUE;
5600:       mat->structurally_symmetric_set = PETSC_TRUE;
5601:     }
5602:     break;
5603:   case MAT_SYMMETRIC:
5604:     mat->symmetric = flg;
5605:     if (flg) mat->structurally_symmetric = PETSC_TRUE;
5606:     mat->symmetric_set              = PETSC_TRUE;
5607:     mat->structurally_symmetric_set = flg;
5608: #if !defined(PETSC_USE_COMPLEX)
5609:     mat->hermitian     = flg;
5610:     mat->hermitian_set = PETSC_TRUE;
5611: #endif
5612:     break;
5613:   case MAT_HERMITIAN:
5614:     mat->hermitian = flg;
5615:     if (flg) mat->structurally_symmetric = PETSC_TRUE;
5616:     mat->hermitian_set              = PETSC_TRUE;
5617:     mat->structurally_symmetric_set = flg;
5618: #if !defined(PETSC_USE_COMPLEX)
5619:     mat->symmetric     = flg;
5620:     mat->symmetric_set = PETSC_TRUE;
5621: #endif
5622:     break;
5623:   case MAT_STRUCTURALLY_SYMMETRIC:
5624:     mat->structurally_symmetric     = flg;
5625:     mat->structurally_symmetric_set = PETSC_TRUE;
5626:     break;
5627:   case MAT_SYMMETRY_ETERNAL:
5628:     mat->symmetric_eternal = flg;
5629:     break;
5630:   case MAT_STRUCTURE_ONLY:
5631:     mat->structure_only = flg;
5632:     break;
5633:   default:
5634:     break;
5635:   }
5636:   if (mat->ops->setoption) {
5637:     (*mat->ops->setoption)(mat,op,flg);
5638:   }
5639:   return(0);
5640: }

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

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

5647:    Input Parameters:
5648: +  mat - the matrix
5649: -  option - the option, this only responds to certain options, check the code for which ones

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

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

5657:    Level: intermediate

5659:    Concepts: matrices^setting options

5661: .seealso:  MatOption, MatSetOption()

5663: @*/
5664: PetscErrorCode MatGetOption(Mat mat,MatOption op,PetscBool *flg)
5665: {

5670:   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);
5671:   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()");

5673:   switch (op) {
5674:   case MAT_NO_OFF_PROC_ENTRIES:
5675:     *flg = mat->nooffprocentries;
5676:     break;
5677:   case MAT_NO_OFF_PROC_ZERO_ROWS:
5678:     *flg = mat->nooffproczerorows;
5679:     break;
5680:   case MAT_SYMMETRIC:
5681:     *flg = mat->symmetric;
5682:     break;
5683:   case MAT_HERMITIAN:
5684:     *flg = mat->hermitian;
5685:     break;
5686:   case MAT_STRUCTURALLY_SYMMETRIC:
5687:     *flg = mat->structurally_symmetric;
5688:     break;
5689:   case MAT_SYMMETRY_ETERNAL:
5690:     *flg = mat->symmetric_eternal;
5691:     break;
5692:   case MAT_SPD:
5693:     *flg = mat->spd;
5694:     break;
5695:   default:
5696:     break;
5697:   }
5698:   return(0);
5699: }

5701: /*@
5702:    MatZeroEntries - Zeros all entries of a matrix.  For sparse matrices
5703:    this routine retains the old nonzero structure.

5705:    Logically Collective on Mat

5707:    Input Parameters:
5708: .  mat - the matrix

5710:    Level: intermediate

5712:    Notes:
5713:     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.
5714:    See the Performance chapter of the users manual for information on preallocating matrices.

5716:    Concepts: matrices^zeroing

5718: .seealso: MatZeroRows()
5719: @*/
5720: PetscErrorCode MatZeroEntries(Mat mat)
5721: {

5727:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
5728:   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");
5729:   if (!mat->ops->zeroentries) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
5730:   MatCheckPreallocated(mat,1);

5732:   PetscLogEventBegin(MAT_ZeroEntries,mat,0,0,0);
5733:   (*mat->ops->zeroentries)(mat);
5734:   PetscLogEventEnd(MAT_ZeroEntries,mat,0,0,0);
5735:   PetscObjectStateIncrease((PetscObject)mat);
5736: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
5737:   if (mat->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED) {
5738:     mat->valid_GPU_matrix = PETSC_OFFLOAD_CPU;
5739:   }
5740: #endif
5741:   return(0);
5742: }

5744: /*@
5745:    MatZeroRowsColumns - Zeros all entries (except possibly the main diagonal)
5746:    of a set of rows and columns of a matrix.

5748:    Collective on Mat

5750:    Input Parameters:
5751: +  mat - the matrix
5752: .  numRows - the number of rows to remove
5753: .  rows - the global row indices
5754: .  diag - value put in all diagonals of eliminated rows (0.0 will even eliminate diagonal entry)
5755: .  x - optional vector of solutions for zeroed rows (other entries in vector are not used)
5756: -  b - optional vector of right hand side, that will be adjusted by provided solution

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

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

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

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

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

5775:    Level: intermediate

5777:    Concepts: matrices^zeroing rows

5779: .seealso: MatZeroRowsIS(), MatZeroRows(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(),
5780:           MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil()
5781: @*/
5782: PetscErrorCode MatZeroRowsColumns(Mat mat,PetscInt numRows,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
5783: {

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

5795:   (*mat->ops->zerorowscolumns)(mat,numRows,rows,diag,x,b);
5796:   MatViewFromOptions(mat,NULL,"-mat_view");
5797:   PetscObjectStateIncrease((PetscObject)mat);
5798: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
5799:   if (mat->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED) {
5800:     mat->valid_GPU_matrix = PETSC_OFFLOAD_CPU;
5801:   }
5802: #endif
5803:   return(0);
5804: }

5806: /*@
5807:    MatZeroRowsColumnsIS - Zeros all entries (except possibly the main diagonal)
5808:    of a set of rows and columns of a matrix.

5810:    Collective on Mat

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

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

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

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

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

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

5836:    Level: intermediate

5838:    Concepts: matrices^zeroing rows

5840: .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(),
5841:           MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRows(), MatZeroRowsColumnsStencil()
5842: @*/
5843: PetscErrorCode MatZeroRowsColumnsIS(Mat mat,IS is,PetscScalar diag,Vec x,Vec b)
5844: {
5846:   PetscInt       numRows;
5847:   const PetscInt *rows;

5854:   ISGetLocalSize(is,&numRows);
5855:   ISGetIndices(is,&rows);
5856:   MatZeroRowsColumns(mat,numRows,rows,diag,x,b);
5857:   ISRestoreIndices(is,&rows);
5858:   return(0);
5859: }

5861: /*@
5862:    MatZeroRows - Zeros all entries (except possibly the main diagonal)
5863:    of a set of rows of a matrix.

5865:    Collective on Mat

5867:    Input Parameters:
5868: +  mat - the matrix
5869: .  numRows - the number of rows to remove
5870: .  rows - the global row indices
5871: .  diag - value put in all diagonals of eliminated rows (0.0 will even eliminate diagonal entry)
5872: .  x - optional vector of solutions for zeroed rows (other entries in vector are not used)
5873: -  b - optional vector of right hand side, that will be adjusted by provided solution

5875:    Notes:
5876:    For the AIJ and BAIJ matrix formats this removes the old nonzero structure,
5877:    but does not release memory.  For the dense and block diagonal
5878:    formats this does not alter the nonzero structure.

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

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

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

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

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

5899:    Level: intermediate

5901:    Concepts: matrices^zeroing rows

5903: .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(),
5904:           MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil()
5905: @*/
5906: PetscErrorCode MatZeroRows(Mat mat,PetscInt numRows,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
5907: {

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

5919:   (*mat->ops->zerorows)(mat,numRows,rows,diag,x,b);
5920:   MatViewFromOptions(mat,NULL,"-mat_view");
5921:   PetscObjectStateIncrease((PetscObject)mat);
5922: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
5923:   if (mat->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED) {
5924:     mat->valid_GPU_matrix = PETSC_OFFLOAD_CPU;
5925:   }
5926: #endif
5927:   return(0);
5928: }

5930: /*@
5931:    MatZeroRowsIS - Zeros all entries (except possibly the main diagonal)
5932:    of a set of rows of a matrix.

5934:    Collective on Mat

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

5943:    Notes:
5944:    For the AIJ and BAIJ matrix formats this removes the old nonzero structure,
5945:    but does not release memory.  For the dense and block diagonal
5946:    formats this does not alter the nonzero structure.

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

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

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

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

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

5967:    Level: intermediate

5969:    Concepts: matrices^zeroing rows

5971: .seealso: MatZeroRows(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(),
5972:           MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil()
5973: @*/
5974: PetscErrorCode MatZeroRowsIS(Mat mat,IS is,PetscScalar diag,Vec x,Vec b)
5975: {
5976:   PetscInt       numRows;
5977:   const PetscInt *rows;

5984:   ISGetLocalSize(is,&numRows);
5985:   ISGetIndices(is,&rows);
5986:   MatZeroRows(mat,numRows,rows,diag,x,b);
5987:   ISRestoreIndices(is,&rows);
5988:   return(0);
5989: }

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

5995:    Collective on Mat

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

6005:    Notes:
6006:    For the AIJ and BAIJ matrix formats this removes the old nonzero structure,
6007:    but does not release memory.  For the dense and block diagonal
6008:    formats this does not alter the nonzero structure.

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

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

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

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

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

6028:    In Fortran idxm and idxn should be declared as
6029: $     MatStencil idxm(4,m)
6030:    and the values inserted using
6031: $    idxm(MatStencil_i,1) = i
6032: $    idxm(MatStencil_j,1) = j
6033: $    idxm(MatStencil_k,1) = k
6034: $    idxm(MatStencil_c,1) = c
6035:    etc

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

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

6045:    Level: intermediate

6047:    Concepts: matrices^zeroing rows

6049: .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsl(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(),
6050:           MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil()
6051: @*/
6052: PetscErrorCode MatZeroRowsStencil(Mat mat,PetscInt numRows,const MatStencil rows[],PetscScalar diag,Vec x,Vec b)
6053: {
6054:   PetscInt       dim     = mat->stencil.dim;
6055:   PetscInt       sdim    = dim - (1 - (PetscInt) mat->stencil.noc);
6056:   PetscInt       *dims   = mat->stencil.dims+1;
6057:   PetscInt       *starts = mat->stencil.starts;
6058:   PetscInt       *dxm    = (PetscInt*) rows;
6059:   PetscInt       *jdxm, i, j, tmp, numNewRows = 0;


6067:   PetscMalloc1(numRows, &jdxm);
6068:   for (i = 0; i < numRows; ++i) {
6069:     /* Skip unused dimensions (they are ordered k, j, i, c) */
6070:     for (j = 0; j < 3-sdim; ++j) dxm++;
6071:     /* Local index in X dir */
6072:     tmp = *dxm++ - starts[0];
6073:     /* Loop over remaining dimensions */
6074:     for (j = 0; j < dim-1; ++j) {
6075:       /* If nonlocal, set index to be negative */
6076:       if ((*dxm++ - starts[j+1]) < 0 || tmp < 0) tmp = PETSC_MIN_INT;
6077:       /* Update local index */
6078:       else tmp = tmp*dims[j] + *(dxm-1) - starts[j+1];
6079:     }
6080:     /* Skip component slot if necessary */
6081:     if (mat->stencil.noc) dxm++;
6082:     /* Local row number */
6083:     if (tmp >= 0) {
6084:       jdxm[numNewRows++] = tmp;
6085:     }
6086:   }
6087:   MatZeroRowsLocal(mat,numNewRows,jdxm,diag,x,b);
6088:   PetscFree(jdxm);
6089:   return(0);
6090: }

6092: /*@
6093:    MatZeroRowsColumnsStencil - Zeros all row and column entries (except possibly the main diagonal)
6094:    of a set of rows and columns of a matrix.

6096:    Collective on Mat

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

6106:    Notes:
6107:    For the AIJ and BAIJ matrix formats this removes the old nonzero structure,
6108:    but does not release memory.  For the dense and block diagonal
6109:    formats this does not alter the nonzero structure.

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

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

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

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

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

6129:    In Fortran idxm and idxn should be declared as
6130: $     MatStencil idxm(4,m)
6131:    and the values inserted using
6132: $    idxm(MatStencil_i,1) = i
6133: $    idxm(MatStencil_j,1) = j
6134: $    idxm(MatStencil_k,1) = k
6135: $    idxm(MatStencil_c,1) = c
6136:    etc

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

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

6146:    Level: intermediate

6148:    Concepts: matrices^zeroing rows

6150: .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(),
6151:           MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRows()
6152: @*/
6153: PetscErrorCode MatZeroRowsColumnsStencil(Mat mat,PetscInt numRows,const MatStencil rows[],PetscScalar diag,Vec x,Vec b)
6154: {
6155:   PetscInt       dim     = mat->stencil.dim;
6156:   PetscInt       sdim    = dim - (1 - (PetscInt) mat->stencil.noc);
6157:   PetscInt       *dims   = mat->stencil.dims+1;
6158:   PetscInt       *starts = mat->stencil.starts;
6159:   PetscInt       *dxm    = (PetscInt*) rows;
6160:   PetscInt       *jdxm, i, j, tmp, numNewRows = 0;


6168:   PetscMalloc1(numRows, &jdxm);
6169:   for (i = 0; i < numRows; ++i) {
6170:     /* Skip unused dimensions (they are ordered k, j, i, c) */
6171:     for (j = 0; j < 3-sdim; ++j) dxm++;
6172:     /* Local index in X dir */
6173:     tmp = *dxm++ - starts[0];
6174:     /* Loop over remaining dimensions */
6175:     for (j = 0; j < dim-1; ++j) {
6176:       /* If nonlocal, set index to be negative */
6177:       if ((*dxm++ - starts[j+1]) < 0 || tmp < 0) tmp = PETSC_MIN_INT;
6178:       /* Update local index */
6179:       else tmp = tmp*dims[j] + *(dxm-1) - starts[j+1];
6180:     }
6181:     /* Skip component slot if necessary */
6182:     if (mat->stencil.noc) dxm++;
6183:     /* Local row number */
6184:     if (tmp >= 0) {
6185:       jdxm[numNewRows++] = tmp;
6186:     }
6187:   }
6188:   MatZeroRowsColumnsLocal(mat,numNewRows,jdxm,diag,x,b);
6189:   PetscFree(jdxm);
6190:   return(0);
6191: }

6193: /*@C
6194:    MatZeroRowsLocal - Zeros all entries (except possibly the main diagonal)
6195:    of a set of rows of a matrix; using local numbering of rows.

6197:    Collective on Mat

6199:    Input Parameters:
6200: +  mat - the matrix
6201: .  numRows - the number of rows to remove
6202: .  rows - the global row indices
6203: .  diag - value put in all diagonals of eliminated rows
6204: .  x - optional vector of solutions for zeroed rows (other entries in vector are not used)
6205: -  b - optional vector of right hand side, that will be adjusted by provided solution

6207:    Notes:
6208:    Before calling MatZeroRowsLocal(), the user must first set the
6209:    local-to-global mapping by calling MatSetLocalToGlobalMapping().

6211:    For the AIJ matrix formats this removes the old nonzero structure,
6212:    but does not release memory.  For the dense and block diagonal
6213:    formats this does not alter the nonzero structure.

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

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

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

6226:    Level: intermediate

6228:    Concepts: matrices^zeroing

6230: .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRows(), MatSetOption(),
6231:           MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil()
6232: @*/
6233: PetscErrorCode MatZeroRowsLocal(Mat mat,PetscInt numRows,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
6234: {

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

6245:   if (mat->ops->zerorowslocal) {
6246:     (*mat->ops->zerorowslocal)(mat,numRows,rows,diag,x,b);
6247:   } else {
6248:     IS             is, newis;
6249:     const PetscInt *newRows;

6251:     if (!mat->rmap->mapping) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Need to provide local to global mapping to matrix first");
6252:     ISCreateGeneral(PETSC_COMM_SELF,numRows,rows,PETSC_COPY_VALUES,&is);
6253:     ISLocalToGlobalMappingApplyIS(mat->rmap->mapping,is,&newis);
6254:     ISGetIndices(newis,&newRows);
6255:     (*mat->ops->zerorows)(mat,numRows,newRows,diag,x,b);
6256:     ISRestoreIndices(newis,&newRows);
6257:     ISDestroy(&newis);
6258:     ISDestroy(&is);
6259:   }
6260:   PetscObjectStateIncrease((PetscObject)mat);
6261: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
6262:   if (mat->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED) {
6263:     mat->valid_GPU_matrix = PETSC_OFFLOAD_CPU;
6264:   }
6265: #endif
6266:   return(0);
6267: }

6269: /*@
6270:    MatZeroRowsLocalIS - Zeros all entries (except possibly the main diagonal)
6271:    of a set of rows of a matrix; using local numbering of rows.

6273:    Collective on Mat

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

6282:    Notes:
6283:    Before calling MatZeroRowsLocalIS(), the user must first set the
6284:    local-to-global mapping by calling MatSetLocalToGlobalMapping().

6286:    For the AIJ matrix formats this removes the old nonzero structure,
6287:    but does not release memory.  For the dense and block diagonal
6288:    formats this does not alter the nonzero structure.

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

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

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

6301:    Level: intermediate

6303:    Concepts: matrices^zeroing

6305: .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRows(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(),
6306:           MatZeroRowsColumnsLocal(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil()
6307: @*/
6308: PetscErrorCode MatZeroRowsLocalIS(Mat mat,IS is,PetscScalar diag,Vec x,Vec b)
6309: {
6311:   PetscInt       numRows;
6312:   const PetscInt *rows;

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

6322:   ISGetLocalSize(is,&numRows);
6323:   ISGetIndices(is,&rows);
6324:   MatZeroRowsLocal(mat,numRows,rows,diag,x,b);
6325:   ISRestoreIndices(is,&rows);
6326:   return(0);
6327: }

6329: /*@
6330:    MatZeroRowsColumnsLocal - Zeros all entries (except possibly the main diagonal)
6331:    of a set of rows and columns of a matrix; using local numbering of rows.

6333:    Collective on Mat

6335:    Input Parameters:
6336: +  mat - the matrix
6337: .  numRows - the number of rows to remove
6338: .  rows - the global row indices
6339: .  diag - value put in all diagonals of eliminated rows
6340: .  x - optional vector of solutions for zeroed rows (other entries in vector are not used)
6341: -  b - optional vector of right hand side, that will be adjusted by provided solution

6343:    Notes:
6344:    Before calling MatZeroRowsColumnsLocal(), the user must first set the
6345:    local-to-global mapping by calling MatSetLocalToGlobalMapping().

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

6351:    Level: intermediate

6353:    Concepts: matrices^zeroing

6355: .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(),
6356:           MatZeroRows(), MatZeroRowsColumnsLocalIS(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil()
6357: @*/
6358: PetscErrorCode MatZeroRowsColumnsLocal(Mat mat,PetscInt numRows,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
6359: {
6361:   IS             is, newis;
6362:   const PetscInt *newRows;

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

6372:   if (!mat->cmap->mapping) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Need to provide local to global mapping to matrix first");
6373:   ISCreateGeneral(PETSC_COMM_SELF,numRows,rows,PETSC_COPY_VALUES,&is);
6374:   ISLocalToGlobalMappingApplyIS(mat->cmap->mapping,is,&newis);
6375:   ISGetIndices(newis,&newRows);
6376:   (*mat->ops->zerorowscolumns)(mat,numRows,newRows,diag,x,b);
6377:   ISRestoreIndices(newis,&newRows);
6378:   ISDestroy(&newis);
6379:   ISDestroy(&is);
6380:   PetscObjectStateIncrease((PetscObject)mat);
6381: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
6382:   if (mat->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED) {
6383:     mat->valid_GPU_matrix = PETSC_OFFLOAD_CPU;
6384:   }
6385: #endif
6386:   return(0);
6387: }

6389: /*@
6390:    MatZeroRowsColumnsLocalIS - Zeros all entries (except possibly the main diagonal)
6391:    of a set of rows and columns of a matrix; using local numbering of rows.

6393:    Collective on Mat

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

6402:    Notes:
6403:    Before calling MatZeroRowsColumnsLocalIS(), the user must first set the
6404:    local-to-global mapping by calling MatSetLocalToGlobalMapping().

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

6410:    Level: intermediate

6412:    Concepts: matrices^zeroing

6414: .seealso: MatZeroRowsIS(), MatZeroRowsColumns(), MatZeroRowsLocalIS(), MatZeroRowsStencil(), MatZeroEntries(), MatZeroRowsLocal(), MatSetOption(),
6415:           MatZeroRowsColumnsLocal(), MatZeroRows(), MatZeroRowsColumnsIS(), MatZeroRowsColumnsStencil()
6416: @*/
6417: PetscErrorCode MatZeroRowsColumnsLocalIS(Mat mat,IS is,PetscScalar diag,Vec x,Vec b)
6418: {
6420:   PetscInt       numRows;
6421:   const PetscInt *rows;

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

6431:   ISGetLocalSize(is,&numRows);
6432:   ISGetIndices(is,&rows);
6433:   MatZeroRowsColumnsLocal(mat,numRows,rows,diag,x,b);
6434:   ISRestoreIndices(is,&rows);
6435:   return(0);
6436: }

6438: /*@C
6439:    MatGetSize - Returns the numbers of rows and columns in a matrix.

6441:    Not Collective

6443:    Input Parameter:
6444: .  mat - the matrix

6446:    Output Parameters:
6447: +  m - the number of global rows
6448: -  n - the number of global columns

6450:    Note: both output parameters can be NULL on input.

6452:    Level: beginner

6454:    Concepts: matrices^size

6456: .seealso: MatGetLocalSize()
6457: @*/
6458: PetscErrorCode MatGetSize(Mat mat,PetscInt *m,PetscInt *n)
6459: {
6462:   if (m) *m = mat->rmap->N;
6463:   if (n) *n = mat->cmap->N;
6464:   return(0);
6465: }

6467: /*@C
6468:    MatGetLocalSize - Returns the number of rows and columns in a matrix
6469:    stored locally.  This information may be implementation dependent, so
6470:    use with care.

6472:    Not Collective

6474:    Input Parameters:
6475: .  mat - the matrix

6477:    Output Parameters:
6478: +  m - the number of local rows
6479: -  n - the number of local columns

6481:    Note: both output parameters can be NULL on input.

6483:    Level: beginner

6485:    Concepts: matrices^local size

6487: .seealso: MatGetSize()
6488: @*/
6489: PetscErrorCode MatGetLocalSize(Mat mat,PetscInt *m,PetscInt *n)
6490: {
6495:   if (m) *m = mat->rmap->n;
6496:   if (n) *n = mat->cmap->n;
6497:   return(0);
6498: }

6500: /*@C
6501:    MatGetOwnershipRangeColumn - Returns the range of matrix columns associated with rows of a vector one multiplies by that owned by
6502:    this processor. (The columns of the "diagonal block")

6504:    Not Collective, unless matrix has not been allocated, then collective on Mat

6506:    Input Parameters:
6507: .  mat - the matrix

6509:    Output Parameters:
6510: +  m - the global index of the first local column
6511: -  n - one more than the global index of the last local column

6513:    Notes:
6514:     both output parameters can be NULL on input.

6516:    Level: developer

6518:    Concepts: matrices^column ownership

6520: .seealso:  MatGetOwnershipRange(), MatGetOwnershipRanges(), MatGetOwnershipRangesColumn()

6522: @*/
6523: PetscErrorCode MatGetOwnershipRangeColumn(Mat mat,PetscInt *m,PetscInt *n)
6524: {
6530:   MatCheckPreallocated(mat,1);
6531:   if (m) *m = mat->cmap->rstart;
6532:   if (n) *n = mat->cmap->rend;
6533:   return(0);
6534: }

6536: /*@C
6537:    MatGetOwnershipRange - Returns the range of matrix rows owned by
6538:    this processor, assuming that the matrix is laid out with the first
6539:    n1 rows on the first processor, the next n2 rows on the second, etc.
6540:    For certain parallel layouts this range may not be well defined.

6542:    Not Collective

6544:    Input Parameters:
6545: .  mat - the matrix

6547:    Output Parameters:
6548: +  m - the global index of the first local row
6549: -  n - one more than the global index of the last local row

6551:    Note: Both output parameters can be NULL on input.
6552: $  This function requires that the matrix be preallocated. If you have not preallocated, consider using
6553: $    PetscSplitOwnership(MPI_Comm comm, PetscInt *n, PetscInt *N)
6554: $  and then MPI_Scan() to calculate prefix sums of the local sizes.

6556:    Level: beginner

6558:    Concepts: matrices^row ownership

6560: .seealso:   MatGetOwnershipRanges(), MatGetOwnershipRangeColumn(), MatGetOwnershipRangesColumn(), PetscSplitOwnership(), PetscSplitOwnershipBlock()

6562: @*/
6563: PetscErrorCode MatGetOwnershipRange(Mat mat,PetscInt *m,PetscInt *n)
6564: {
6570:   MatCheckPreallocated(mat,1);
6571:   if (m) *m = mat->rmap->rstart;
6572:   if (n) *n = mat->rmap->rend;
6573:   return(0);
6574: }

6576: /*@C
6577:    MatGetOwnershipRanges - Returns the range of matrix rows owned by
6578:    each process

6580:    Not Collective, unless matrix has not been allocated, then collective on Mat

6582:    Input Parameters:
6583: .  mat - the matrix

6585:    Output Parameters:
6586: .  ranges - start of each processors portion plus one more than the total length at the end

6588:    Level: beginner

6590:    Concepts: matrices^row ownership

6592: .seealso:   MatGetOwnershipRange(), MatGetOwnershipRangeColumn(), MatGetOwnershipRangesColumn()

6594: @*/
6595: PetscErrorCode MatGetOwnershipRanges(Mat mat,const PetscInt **ranges)
6596: {

6602:   MatCheckPreallocated(mat,1);
6603:   PetscLayoutGetRanges(mat->rmap,ranges);
6604:   return(0);
6605: }

6607: /*@C
6608:    MatGetOwnershipRangesColumn - Returns the range of matrix columns associated with rows of a vector one multiplies by that owned by
6609:    this processor. (The columns of the "diagonal blocks" for each process)

6611:    Not Collective, unless matrix has not been allocated, then collective on Mat

6613:    Input Parameters:
6614: .  mat - the matrix

6616:    Output Parameters:
6617: .  ranges - start of each processors portion plus one more then the total length at the end

6619:    Level: beginner

6621:    Concepts: matrices^column ownership

6623: .seealso:   MatGetOwnershipRange(), MatGetOwnershipRangeColumn(), MatGetOwnershipRanges()

6625: @*/
6626: PetscErrorCode MatGetOwnershipRangesColumn(Mat mat,const PetscInt **ranges)
6627: {

6633:   MatCheckPreallocated(mat,1);
6634:   PetscLayoutGetRanges(mat->cmap,ranges);
6635:   return(0);
6636: }

6638: /*@C
6639:    MatGetOwnershipIS - Get row and column ownership as index sets

6641:    Not Collective

6643:    Input Arguments:
6644: .  A - matrix of type Elemental

6646:    Output Arguments:
6647: +  rows - rows in which this process owns elements
6648: .  cols - columns in which this process owns elements

6650:    Level: intermediate

6652: .seealso: MatGetOwnershipRange(), MatGetOwnershipRangeColumn(), MatSetValues(), MATELEMENTAL
6653: @*/
6654: PetscErrorCode MatGetOwnershipIS(Mat A,IS *rows,IS *cols)
6655: {
6656:   PetscErrorCode ierr,(*f)(Mat,IS*,IS*);

6659:   MatCheckPreallocated(A,1);
6660:   PetscObjectQueryFunction((PetscObject)A,"MatGetOwnershipIS_C",&f);
6661:   if (f) {
6662:     (*f)(A,rows,cols);
6663:   } else {   /* Create a standard row-based partition, each process is responsible for ALL columns in their row block */
6664:     if (rows) {ISCreateStride(PETSC_COMM_SELF,A->rmap->n,A->rmap->rstart,1,rows);}
6665:     if (cols) {ISCreateStride(PETSC_COMM_SELF,A->cmap->N,0,1,cols);}
6666:   }
6667:   return(0);
6668: }

6670: /*@C
6671:    MatILUFactorSymbolic - Performs symbolic ILU factorization of a matrix.
6672:    Uses levels of fill only, not drop tolerance. Use MatLUFactorNumeric()
6673:    to complete the factorization.

6675:    Collective on Mat

6677:    Input Parameters:
6678: +  mat - the matrix
6679: .  row - row permutation
6680: .  column - column permutation
6681: -  info - structure containing
6682: $      levels - number of levels of fill.
6683: $      expected fill - as ratio of original fill.
6684: $      1 or 0 - indicating force fill on diagonal (improves robustness for matrices
6685:                 missing diagonal entries)

6687:    Output Parameters:
6688: .  fact - new matrix that has been symbolically factored

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

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

6697:    Level: developer

6699:   Concepts: matrices^symbolic LU factorization
6700:   Concepts: matrices^factorization
6701:   Concepts: LU^symbolic factorization

6703: .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor()
6704:           MatGetOrdering(), MatFactorInfo

6706:     Note: this uses the definition of level of fill as in Y. Saad, 2003

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

6711:    References:
6712:      Y. Saad, Iterative methods for sparse linear systems Philadelphia: Society for Industrial and Applied Mathematics, 2003
6713: @*/
6714: PetscErrorCode MatILUFactorSymbolic(Mat fact,Mat mat,IS row,IS col,const MatFactorInfo *info)
6715: {

6725:   if (info->levels < 0) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"Levels of fill negative %D",(PetscInt)info->levels);
6726:   if (info->fill < 1.0) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"Expected fill less than 1.0 %g",(double)info->fill);
6727:   if (!(fact)->ops->ilufactorsymbolic) {
6728:     MatSolverType spackage;
6729:     MatFactorGetSolverType(fact,&spackage);
6730:     SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Matrix type %s symbolic ILU using solver package %s",((PetscObject)mat)->type_name,spackage);
6731:   }
6732:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
6733:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
6734:   MatCheckPreallocated(mat,2);

6736:   PetscLogEventBegin(MAT_ILUFactorSymbolic,mat,row,col,0);
6737:   (fact->ops->ilufactorsymbolic)(fact,mat,row,col,info);
6738:   PetscLogEventEnd(MAT_ILUFactorSymbolic,mat,row,col,0);
6739:   return(0);
6740: }

6742: /*@C
6743:    MatICCFactorSymbolic - Performs symbolic incomplete
6744:    Cholesky factorization for a symmetric matrix.  Use
6745:    MatCholeskyFactorNumeric() to complete the factorization.

6747:    Collective on Mat

6749:    Input Parameters:
6750: +  mat - the matrix
6751: .  perm - row and column permutation
6752: -  info - structure containing
6753: $      levels - number of levels of fill.
6754: $      expected fill - as ratio of original fill.

6756:    Output Parameter:
6757: .  fact - the factored matrix

6759:    Notes:
6760:    Most users should employ the KSP interface for linear solvers
6761:    instead of working directly with matrix algebra routines such as this.
6762:    See, e.g., KSPCreate().

6764:    Level: developer

6766:   Concepts: matrices^symbolic incomplete Cholesky factorization
6767:   Concepts: matrices^factorization
6768:   Concepts: Cholsky^symbolic factorization

6770: .seealso: MatCholeskyFactorNumeric(), MatCholeskyFactor(), MatFactorInfo

6772:     Note: this uses the definition of level of fill as in Y. Saad, 2003

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

6777:    References:
6778:      Y. Saad, Iterative methods for sparse linear systems Philadelphia: Society for Industrial and Applied Mathematics, 2003
6779: @*/
6780: PetscErrorCode MatICCFactorSymbolic(Mat fact,Mat mat,IS perm,const MatFactorInfo *info)
6781: {

6790:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
6791:   if (info->levels < 0) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"Levels negative %D",(PetscInt) info->levels);
6792:   if (info->fill < 1.0) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"Expected fill less than 1.0 %g",(double)info->fill);
6793:   if (!(fact)->ops->iccfactorsymbolic) {
6794:     MatSolverType spackage;
6795:     MatFactorGetSolverType(fact,&spackage);
6796:     SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Matrix type %s symbolic ICC using solver package %s",((PetscObject)mat)->type_name,spackage);
6797:   }
6798:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
6799:   MatCheckPreallocated(mat,2);

6801:   PetscLogEventBegin(MAT_ICCFactorSymbolic,mat,perm,0,0);
6802:   (fact->ops->iccfactorsymbolic)(fact,mat,perm,info);
6803:   PetscLogEventEnd(MAT_ICCFactorSymbolic,mat,perm,0,0);
6804:   return(0);
6805: }

6807: /*@C
6808:    MatCreateSubMatrices - Extracts several submatrices from a matrix. If submat
6809:    points to an array of valid matrices, they may be reused to store the new
6810:    submatrices.

6812:    Collective on Mat

6814:    Input Parameters:
6815: +  mat - the matrix
6816: .  n   - the number of submatrixes to be extracted (on this processor, may be zero)
6817: .  irow, icol - index sets of rows and columns to extract
6818: -  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX

6820:    Output Parameter:
6821: .  submat - the array of submatrices

6823:    Notes:
6824:    MatCreateSubMatrices() can extract ONLY sequential submatrices
6825:    (from both sequential and parallel matrices). Use MatCreateSubMatrix()
6826:    to extract a parallel submatrix.

6828:    Some matrix types place restrictions on the row and column
6829:    indices, such as that they be sorted or that they be equal to each other.

6831:    The index sets may not have duplicate entries.

6833:    When extracting submatrices from a parallel matrix, each processor can
6834:    form a different submatrix by setting the rows and columns of its
6835:    individual index sets according to the local submatrix desired.

6837:    When finished using the submatrices, the user should destroy
6838:    them with MatDestroySubMatrices().

6840:    MAT_REUSE_MATRIX can only be used when the nonzero structure of the
6841:    original matrix has not changed from that last call to MatCreateSubMatrices().

6843:    This routine creates the matrices in submat; you should NOT create them before
6844:    calling it. It also allocates the array of matrix pointers submat.

6846:    For BAIJ matrices the index sets must respect the block structure, that is if they
6847:    request one row/column in a block, they must request all rows/columns that are in
6848:    that block. For example, if the block size is 2 you cannot request just row 0 and
6849:    column 0.

6851:    Fortran Note:
6852:    The Fortran interface is slightly different from that given below; it
6853:    requires one to pass in  as submat a Mat (integer) array of size at least n+1.

6855:    Level: advanced

6857:    Concepts: matrices^accessing submatrices
6858:    Concepts: submatrices

6860: .seealso: MatDestroySubMatrices(), MatCreateSubMatrix(), MatGetRow(), MatGetDiagonal(), MatReuse
6861: @*/
6862: PetscErrorCode MatCreateSubMatrices(Mat mat,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *submat[])
6863: {
6865:   PetscInt       i;
6866:   PetscBool      eq;

6871:   if (n) {
6876:   }
6878:   if (n && scall == MAT_REUSE_MATRIX) {
6881:   }
6882:   if (!mat->ops->createsubmatrices) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
6883:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
6884:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
6885:   MatCheckPreallocated(mat,1);

6887:   PetscLogEventBegin(MAT_CreateSubMats,mat,0,0,0);
6888:   (*mat->ops->createsubmatrices)(mat,n,irow,icol,scall,submat);
6889:   PetscLogEventEnd(MAT_CreateSubMats,mat,0,0,0);
6890:   for (i=0; i<n; i++) {
6891:     (*submat)[i]->factortype = MAT_FACTOR_NONE;  /* in case in place factorization was previously done on submatrix */
6892:     if (mat->symmetric || mat->structurally_symmetric || mat->hermitian) {
6893:       ISEqual(irow[i],icol[i],&eq);
6894:       if (eq) {
6895:         if (mat->symmetric) {
6896:           MatSetOption((*submat)[i],MAT_SYMMETRIC,PETSC_TRUE);
6897:         } else if (mat->hermitian) {
6898:           MatSetOption((*submat)[i],MAT_HERMITIAN,PETSC_TRUE);
6899:         } else if (mat->structurally_symmetric) {
6900:           MatSetOption((*submat)[i],MAT_STRUCTURALLY_SYMMETRIC,PETSC_TRUE);
6901:         }
6902:       }
6903:     }
6904:   }
6905:   return(0);
6906: }

6908: /*@C
6909:    MatCreateSubMatricesMPI - Extracts MPI submatrices across a sub communicator of mat (by pairs of IS that may live on subcomms).

6911:    Collective on Mat

6913:    Input Parameters:
6914: +  mat - the matrix
6915: .  n   - the number of submatrixes to be extracted
6916: .  irow, icol - index sets of rows and columns to extract
6917: -  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX

6919:    Output Parameter:
6920: .  submat - the array of submatrices

6922:    Level: advanced

6924:    Concepts: matrices^accessing submatrices
6925:    Concepts: submatrices

6927: .seealso: MatCreateSubMatrices(), MatCreateSubMatrix(), MatGetRow(), MatGetDiagonal(), MatReuse
6928: @*/
6929: PetscErrorCode MatCreateSubMatricesMPI(Mat mat,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *submat[])
6930: {
6932:   PetscInt       i;
6933:   PetscBool      eq;

6938:   if (n) {
6943:   }
6945:   if (n && scall == MAT_REUSE_MATRIX) {
6948:   }
6949:   if (!mat->ops->createsubmatricesmpi) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
6950:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
6951:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
6952:   MatCheckPreallocated(mat,1);

6954:   PetscLogEventBegin(MAT_CreateSubMats,mat,0,0,0);
6955:   (*mat->ops->createsubmatricesmpi)(mat,n,irow,icol,scall,submat);
6956:   PetscLogEventEnd(MAT_CreateSubMats,mat,0,0,0);
6957:   for (i=0; i<n; i++) {
6958:     if (mat->symmetric || mat->structurally_symmetric || mat->hermitian) {
6959:       ISEqual(irow[i],icol[i],&eq);
6960:       if (eq) {
6961:         if (mat->symmetric) {
6962:           MatSetOption((*submat)[i],MAT_SYMMETRIC,PETSC_TRUE);
6963:         } else if (mat->hermitian) {
6964:           MatSetOption((*submat)[i],MAT_HERMITIAN,PETSC_TRUE);
6965:         } else if (mat->structurally_symmetric) {
6966:           MatSetOption((*submat)[i],MAT_STRUCTURALLY_SYMMETRIC,PETSC_TRUE);
6967:         }
6968:       }
6969:     }
6970:   }
6971:   return(0);
6972: }

6974: /*@C
6975:    MatDestroyMatrices - Destroys an array of matrices.

6977:    Collective on Mat

6979:    Input Parameters:
6980: +  n - the number of local matrices
6981: -  mat - the matrices (note that this is a pointer to the array of matrices)

6983:    Level: advanced

6985:     Notes:
6986:     Frees not only the matrices, but also the array that contains the matrices
6987:            In Fortran will not free the array.

6989: .seealso: MatCreateSubMatrices() MatDestroySubMatrices()
6990: @*/
6991: PetscErrorCode MatDestroyMatrices(PetscInt n,Mat *mat[])
6992: {
6994:   PetscInt       i;

6997:   if (!*mat) return(0);
6998:   if (n < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Trying to destroy negative number of matrices %D",n);

7001:   for (i=0; i<n; i++) {
7002:     MatDestroy(&(*mat)[i]);
7003:   }

7005:   /* memory is allocated even if n = 0 */
7006:   PetscFree(*mat);
7007:   return(0);
7008: }

7010: /*@C
7011:    MatDestroySubMatrices - Destroys a set of matrices obtained with MatCreateSubMatrices().

7013:    Collective on Mat

7015:    Input Parameters:
7016: +  n - the number of local matrices
7017: -  mat - the matrices (note that this is a pointer to the array of matrices, just to match the calling
7018:                        sequence of MatCreateSubMatrices())

7020:    Level: advanced

7022:     Notes:
7023:     Frees not only the matrices, but also the array that contains the matrices
7024:            In Fortran will not free the array.

7026: .seealso: MatCreateSubMatrices()
7027: @*/
7028: PetscErrorCode MatDestroySubMatrices(PetscInt n,Mat *mat[])
7029: {
7031:   Mat            mat0;

7034:   if (!*mat) return(0);
7035:   /* mat[] is an array of length n+1, see MatCreateSubMatrices_xxx() */
7036:   if (n < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Trying to destroy negative number of matrices %D",n);

7039:   mat0 = (*mat)[0];
7040:   if (mat0 && mat0->ops->destroysubmatrices) {
7041:     (mat0->ops->destroysubmatrices)(n,mat);
7042:   } else {
7043:     MatDestroyMatrices(n,mat);
7044:   }
7045:   return(0);
7046: }

7048: /*@C
7049:    MatGetSeqNonzeroStructure - Extracts the sequential nonzero structure from a matrix.

7051:    Collective on Mat

7053:    Input Parameters:
7054: .  mat - the matrix

7056:    Output Parameter:
7057: .  matstruct - the sequential matrix with the nonzero structure of mat

7059:   Level: intermediate

7061: .seealso: MatDestroySeqNonzeroStructure(), MatCreateSubMatrices(), MatDestroyMatrices()
7062: @*/
7063: PetscErrorCode MatGetSeqNonzeroStructure(Mat mat,Mat *matstruct)
7064: {


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

7075:   if (!mat->ops->getseqnonzerostructure) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Not for matrix type %s\n",((PetscObject)mat)->type_name);
7076:   PetscLogEventBegin(MAT_GetSeqNonzeroStructure,mat,0,0,0);
7077:   (*mat->ops->getseqnonzerostructure)(mat,matstruct);
7078:   PetscLogEventEnd(MAT_GetSeqNonzeroStructure,mat,0,0,0);
7079:   return(0);
7080: }

7082: /*@C
7083:    MatDestroySeqNonzeroStructure - Destroys matrix obtained with MatGetSeqNonzeroStructure().

7085:    Collective on Mat

7087:    Input Parameters:
7088: .  mat - the matrix (note that this is a pointer to the array of matrices, just to match the calling
7089:                        sequence of MatGetSequentialNonzeroStructure())

7091:    Level: advanced

7093:     Notes:
7094:     Frees not only the matrices, but also the array that contains the matrices

7096: .seealso: MatGetSeqNonzeroStructure()
7097: @*/
7098: PetscErrorCode MatDestroySeqNonzeroStructure(Mat *mat)
7099: {

7104:   MatDestroy(mat);
7105:   return(0);
7106: }

7108: /*@
7109:    MatIncreaseOverlap - Given a set of submatrices indicated by index sets,
7110:    replaces the index sets by larger ones that represent submatrices with
7111:    additional overlap.

7113:    Collective on Mat

7115:    Input Parameters:
7116: +  mat - the matrix
7117: .  n   - the number of index sets
7118: .  is  - the array of index sets (these index sets will changed during the call)
7119: -  ov  - the additional overlap requested

7121:    Options Database:
7122: .  -mat_increase_overlap_scalable - use a scalable algorithm to compute the overlap (supported by MPIAIJ matrix)

7124:    Level: developer

7126:    Concepts: overlap
7127:    Concepts: ASM^computing overlap

7129: .seealso: MatCreateSubMatrices()
7130: @*/
7131: PetscErrorCode MatIncreaseOverlap(Mat mat,PetscInt n,IS is[],PetscInt ov)
7132: {

7138:   if (n < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Must have one or more domains, you have %D",n);
7139:   if (n) {
7142:   }
7143:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
7144:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
7145:   MatCheckPreallocated(mat,1);

7147:   if (!ov) return(0);
7148:   if (!mat->ops->increaseoverlap) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
7149:   PetscLogEventBegin(MAT_IncreaseOverlap,mat,0,0,0);
7150:   (*mat->ops->increaseoverlap)(mat,n,is,ov);
7151:   PetscLogEventEnd(MAT_IncreaseOverlap,mat,0,0,0);
7152:   return(0);
7153: }


7156: PetscErrorCode MatIncreaseOverlapSplit_Single(Mat,IS*,PetscInt);

7158: /*@
7159:    MatIncreaseOverlapSplit - Given a set of submatrices indicated by index sets across
7160:    a sub communicator, replaces the index sets by larger ones that represent submatrices with
7161:    additional overlap.

7163:    Collective on Mat

7165:    Input Parameters:
7166: +  mat - the matrix
7167: .  n   - the number of index sets
7168: .  is  - the array of index sets (these index sets will changed during the call)
7169: -  ov  - the additional overlap requested

7171:    Options Database:
7172: .  -mat_increase_overlap_scalable - use a scalable algorithm to compute the overlap (supported by MPIAIJ matrix)

7174:    Level: developer

7176:    Concepts: overlap
7177:    Concepts: ASM^computing overlap

7179: .seealso: MatCreateSubMatrices()
7180: @*/
7181: PetscErrorCode MatIncreaseOverlapSplit(Mat mat,PetscInt n,IS is[],PetscInt ov)
7182: {
7183:   PetscInt       i;

7189:   if (n < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Must have one or more domains, you have %D",n);
7190:   if (n) {
7193:   }
7194:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
7195:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
7196:   MatCheckPreallocated(mat,1);
7197:   if (!ov) return(0);
7198:   PetscLogEventBegin(MAT_IncreaseOverlap,mat,0,0,0);
7199:   for(i=0; i<n; i++){
7200:          MatIncreaseOverlapSplit_Single(mat,&is[i],ov);
7201:   }
7202:   PetscLogEventEnd(MAT_IncreaseOverlap,mat,0,0,0);
7203:   return(0);
7204: }




7209: /*@
7210:    MatGetBlockSize - Returns the matrix block size.

7212:    Not Collective

7214:    Input Parameter:
7215: .  mat - the matrix

7217:    Output Parameter:
7218: .  bs - block size

7220:    Notes:
7221:     Block row formats are MATSEQBAIJ, MATMPIBAIJ, MATSEQSBAIJ, MATMPISBAIJ. These formats ALWAYS have square block storage in the matrix.

7223:    If the block size has not been set yet this routine returns 1.

7225:    Level: intermediate

7227:    Concepts: matrices^block size

7229: .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSizes()
7230: @*/
7231: PetscErrorCode MatGetBlockSize(Mat mat,PetscInt *bs)
7232: {
7236:   *bs = PetscAbs(mat->rmap->bs);
7237:   return(0);
7238: }

7240: /*@
7241:    MatGetBlockSizes - Returns the matrix block row and column sizes.

7243:    Not Collective

7245:    Input Parameter:
7246: .  mat - the matrix

7248:    Output Parameter:
7249: .  rbs - row block size
7250: .  cbs - column block size

7252:    Notes:
7253:     Block row formats are MATSEQBAIJ, MATMPIBAIJ, MATSEQSBAIJ, MATMPISBAIJ. These formats ALWAYS have square block storage in the matrix.
7254:     If you pass a different block size for the columns than the rows, the row block size determines the square block storage.

7256:    If a block size has not been set yet this routine returns 1.

7258:    Level: intermediate

7260:    Concepts: matrices^block size

7262: .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize(), MatSetBlockSize(), MatSetBlockSizes()
7263: @*/
7264: PetscErrorCode MatGetBlockSizes(Mat mat,PetscInt *rbs, PetscInt *cbs)
7265: {
7270:   if (rbs) *rbs = PetscAbs(mat->rmap->bs);
7271:   if (cbs) *cbs = PetscAbs(mat->cmap->bs);
7272:   return(0);
7273: }

7275: /*@
7276:    MatSetBlockSize - Sets the matrix block size.

7278:    Logically Collective on Mat

7280:    Input Parameters:
7281: +  mat - the matrix
7282: -  bs - block size

7284:    Notes:
7285:     Block row formats are MATSEQBAIJ, MATMPIBAIJ, MATSEQSBAIJ, MATMPISBAIJ. These formats ALWAYS have square block storage in the matrix.
7286:     This must be called before MatSetUp() or MatXXXSetPreallocation() (or will default to 1) and the block size cannot be changed later.

7288:     For MATMPIAIJ and MATSEQAIJ matrix formats, this function can be called at a later stage, provided that the specified block size
7289:     is compatible with the matrix local sizes.

7291:    Level: intermediate

7293:    Concepts: matrices^block size

7295: .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize(), MatSetBlockSizes(), MatGetBlockSizes()
7296: @*/
7297: PetscErrorCode MatSetBlockSize(Mat mat,PetscInt bs)
7298: {

7304:   MatSetBlockSizes(mat,bs,bs);
7305:   return(0);
7306: }

7308: /*@
7309:    MatSetVariableBlockSizes - Sets a diagonal blocks of the matrix that need not be of the same size

7311:    Logically Collective on Mat

7313:    Input Parameters:
7314: +  mat - the matrix
7315: .  nblocks - the number of blocks on this process
7316: -  bsizes - the block sizes

7318:    Notes:
7319:     Currently used by PCVPBJACOBI for SeqAIJ matrices

7321:    Level: intermediate

7323:    Concepts: matrices^block size

7325: .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize(), MatSetBlockSizes(), MatGetBlockSizes(), MatGetVariableBlockSizes()
7326: @*/
7327: PetscErrorCode MatSetVariableBlockSizes(Mat mat,PetscInt nblocks,PetscInt *bsizes)
7328: {
7330:   PetscInt       i,ncnt = 0, nlocal;

7334:   if (nblocks < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Number of local blocks must be great than or equal to zero");
7335:   MatGetLocalSize(mat,&nlocal,NULL);
7336:   for (i=0; i<nblocks; i++) ncnt += bsizes[i];
7337:   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);
7338:   PetscFree(mat->bsizes);
7339:   mat->nblocks = nblocks;
7340:   PetscMalloc1(nblocks,&mat->bsizes);
7341:   PetscMemcpy(mat->bsizes,bsizes,nblocks*sizeof(PetscInt));
7342:   return(0);
7343: }

7345: /*@C
7346:    MatGetVariableBlockSizes - Gets a diagonal blocks of the matrix that need not be of the same size

7348:    Logically Collective on Mat

7350:    Input Parameters:
7351: .  mat - the matrix

7353:    Output Parameters:
7354: +  nblocks - the number of blocks on this process
7355: -  bsizes - the block sizes

7357:    Notes: Currently not supported from Fortran

7359:    Level: intermediate

7361:    Concepts: matrices^block size

7363: .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize(), MatSetBlockSizes(), MatGetBlockSizes(), MatSetVariableBlockSizes()
7364: @*/
7365: PetscErrorCode MatGetVariableBlockSizes(Mat mat,PetscInt *nblocks,const PetscInt **bsizes)
7366: {
7369:   *nblocks = mat->nblocks;
7370:   *bsizes  = mat->bsizes;
7371:   return(0);
7372: }

7374: /*@
7375:    MatSetBlockSizes - Sets the matrix block row and column sizes.

7377:    Logically Collective on Mat

7379:    Input Parameters:
7380: +  mat - the matrix
7381: -  rbs - row block size
7382: -  cbs - column block size

7384:    Notes:
7385:     Block row formats are MATSEQBAIJ, MATMPIBAIJ, MATSEQSBAIJ, MATMPISBAIJ. These formats ALWAYS have square block storage in the matrix.
7386:     If you pass a different block size for the columns than the rows, the row block size determines the square block storage.
7387:     This must be called before MatSetUp() or MatXXXSetPreallocation() (or will default to 1) and the block size cannot be changed later

7389:     For MATMPIAIJ and MATSEQAIJ matrix formats, this function can be called at a later stage, provided that the specified block sizes
7390:     are compatible with the matrix local sizes.

7392:     The row and column block size determine the blocksize of the "row" and "column" vectors returned by MatCreateVecs().

7394:    Level: intermediate

7396:    Concepts: matrices^block size

7398: .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize(), MatSetBlockSize(), MatGetBlockSizes()
7399: @*/
7400: PetscErrorCode MatSetBlockSizes(Mat mat,PetscInt rbs,PetscInt cbs)
7401: {

7408:   if (mat->ops->setblocksizes) {
7409:     (*mat->ops->setblocksizes)(mat,rbs,cbs);
7410:   }
7411:   if (mat->rmap->refcnt) {
7412:     ISLocalToGlobalMapping l2g = NULL;
7413:     PetscLayout            nmap = NULL;

7415:     PetscLayoutDuplicate(mat->rmap,&nmap);
7416:     if (mat->rmap->mapping) {
7417:       ISLocalToGlobalMappingDuplicate(mat->rmap->mapping,&l2g);
7418:     }
7419:     PetscLayoutDestroy(&mat->rmap);
7420:     mat->rmap = nmap;
7421:     mat->rmap->mapping = l2g;
7422:   }
7423:   if (mat->cmap->refcnt) {
7424:     ISLocalToGlobalMapping l2g = NULL;
7425:     PetscLayout            nmap = NULL;

7427:     PetscLayoutDuplicate(mat->cmap,&nmap);
7428:     if (mat->cmap->mapping) {
7429:       ISLocalToGlobalMappingDuplicate(mat->cmap->mapping,&l2g);
7430:     }
7431:     PetscLayoutDestroy(&mat->cmap);
7432:     mat->cmap = nmap;
7433:     mat->cmap->mapping = l2g;
7434:   }
7435:   PetscLayoutSetBlockSize(mat->rmap,rbs);
7436:   PetscLayoutSetBlockSize(mat->cmap,cbs);
7437:   return(0);
7438: }

7440: /*@
7441:    MatSetBlockSizesFromMats - Sets the matrix block row and column sizes to match a pair of matrices

7443:    Logically Collective on Mat

7445:    Input Parameters:
7446: +  mat - the matrix
7447: .  fromRow - matrix from which to copy row block size
7448: -  fromCol - matrix from which to copy column block size (can be same as fromRow)

7450:    Level: developer

7452:    Concepts: matrices^block size

7454: .seealso: MatCreateSeqBAIJ(), MatCreateBAIJ(), MatGetBlockSize(), MatSetBlockSizes()
7455: @*/
7456: PetscErrorCode MatSetBlockSizesFromMats(Mat mat,Mat fromRow,Mat fromCol)
7457: {

7464:   if (fromRow->rmap->bs > 0) {PetscLayoutSetBlockSize(mat->rmap,fromRow->rmap->bs);}
7465:   if (fromCol->cmap->bs > 0) {PetscLayoutSetBlockSize(mat->cmap,fromCol->cmap->bs);}
7466:   return(0);
7467: }

7469: /*@
7470:    MatResidual - Default routine to calculate the residual.

7472:    Collective on Mat and Vec

7474:    Input Parameters:
7475: +  mat - the matrix
7476: .  b   - the right-hand-side
7477: -  x   - the approximate solution

7479:    Output Parameter:
7480: .  r - location to store the residual

7482:    Level: developer

7484: .keywords: MG, default, multigrid, residual

7486: .seealso: PCMGSetResidual()
7487: @*/
7488: PetscErrorCode MatResidual(Mat mat,Vec b,Vec x,Vec r)
7489: {

7498:   MatCheckPreallocated(mat,1);
7499:   PetscLogEventBegin(MAT_Residual,mat,0,0,0);
7500:   if (!mat->ops->residual) {
7501:     MatMult(mat,x,r);
7502:     VecAYPX(r,-1.0,b);
7503:   } else {
7504:     (*mat->ops->residual)(mat,b,x,r);
7505:   }
7506:   PetscLogEventEnd(MAT_Residual,mat,0,0,0);
7507:   return(0);
7508: }

7510: /*@C
7511:     MatGetRowIJ - Returns the compressed row storage i and j indices for sequential matrices.

7513:    Collective on Mat

7515:     Input Parameters:
7516: +   mat - the matrix
7517: .   shift -  0 or 1 indicating we want the indices starting at 0 or 1
7518: .   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be   symmetrized
7519: -   inodecompressed - PETSC_TRUE or PETSC_FALSE  indicating if the nonzero structure of the
7520:                  inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is
7521:                  always used.

7523:     Output Parameters:
7524: +   n - number of rows in the (possibly compressed) matrix
7525: .   ia - the row pointers; that is ia[0] = 0, ia[row] = ia[row-1] + number of elements in that row of the matrix
7526: .   ja - the column indices
7527: -   done - indicates if the routine actually worked and returned appropriate ia[] and ja[] arrays; callers
7528:            are responsible for handling the case when done == PETSC_FALSE and ia and ja are not set

7530:     Level: developer

7532:     Notes:
7533:     You CANNOT change any of the ia[] or ja[] values.

7535:     Use MatRestoreRowIJ() when you are finished accessing the ia[] and ja[] values.

7537:     Fortran Notes:
7538:     In Fortran use
7539: $
7540: $      PetscInt ia(1), ja(1)
7541: $      PetscOffset iia, jja
7542: $      call MatGetRowIJ(mat,shift,symmetric,inodecompressed,n,ia,iia,ja,jja,done,ierr)
7543: $      ! Access the ith and jth entries via ia(iia + i) and ja(jja + j)

7545:      or
7546: $
7547: $    PetscInt, pointer :: ia(:),ja(:)
7548: $    call MatGetRowIJF90(mat,shift,symmetric,inodecompressed,n,ia,ja,done,ierr)
7549: $    ! Access the ith and jth entries via ia(i) and ja(j)

7551: .seealso: MatGetColumnIJ(), MatRestoreRowIJ(), MatSeqAIJGetArray()
7552: @*/
7553: PetscErrorCode MatGetRowIJ(Mat mat,PetscInt shift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
7554: {

7564:   MatCheckPreallocated(mat,1);
7565:   if (!mat->ops->getrowij) *done = PETSC_FALSE;
7566:   else {
7567:     *done = PETSC_TRUE;
7568:     PetscLogEventBegin(MAT_GetRowIJ,mat,0,0,0);
7569:     (*mat->ops->getrowij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);
7570:     PetscLogEventEnd(MAT_GetRowIJ,mat,0,0,0);
7571:   }
7572:   return(0);
7573: }

7575: /*@C
7576:     MatGetColumnIJ - Returns the compressed column storage i and j indices for sequential matrices.

7578:     Collective on Mat

7580:     Input Parameters:
7581: +   mat - the matrix
7582: .   shift - 1 or zero indicating we want the indices starting at 0 or 1
7583: .   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be
7584:                 symmetrized
7585: .   inodecompressed - PETSC_TRUE or PETSC_FALSE indicating if the nonzero structure of the
7586:                  inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is
7587:                  always used.
7588: .   n - number of columns in the (possibly compressed) matrix
7589: .   ia - the column pointers; that is ia[0] = 0, ia[col] = i[col-1] + number of elements in that col of the matrix
7590: -   ja - the row indices

7592:     Output Parameters:
7593: .   done - PETSC_TRUE or PETSC_FALSE, indicating whether the values have been returned

7595:     Level: developer

7597: .seealso: MatGetRowIJ(), MatRestoreColumnIJ()
7598: @*/
7599: PetscErrorCode MatGetColumnIJ(Mat mat,PetscInt shift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
7600: {

7610:   MatCheckPreallocated(mat,1);
7611:   if (!mat->ops->getcolumnij) *done = PETSC_FALSE;
7612:   else {
7613:     *done = PETSC_TRUE;
7614:     (*mat->ops->getcolumnij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);
7615:   }
7616:   return(0);
7617: }

7619: /*@C
7620:     MatRestoreRowIJ - Call after you are completed with the ia,ja indices obtained with
7621:     MatGetRowIJ().

7623:     Collective on Mat

7625:     Input Parameters:
7626: +   mat - the matrix
7627: .   shift - 1 or zero indicating we want the indices starting at 0 or 1
7628: .   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be
7629:                 symmetrized
7630: .   inodecompressed -  PETSC_TRUE or PETSC_FALSE indicating if the nonzero structure of the
7631:                  inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is
7632:                  always used.
7633: .   n - size of (possibly compressed) matrix
7634: .   ia - the row pointers
7635: -   ja - the column indices

7637:     Output Parameters:
7638: .   done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned

7640:     Note:
7641:     This routine zeros out n, ia, and ja. This is to prevent accidental
7642:     us of the array after it has been restored. If you pass NULL, it will
7643:     not zero the pointers.  Use of ia or ja after MatRestoreRowIJ() is invalid.

7645:     Level: developer

7647: .seealso: MatGetRowIJ(), MatRestoreColumnIJ()
7648: @*/
7649: PetscErrorCode MatRestoreRowIJ(Mat mat,PetscInt shift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
7650: {

7659:   MatCheckPreallocated(mat,1);

7661:   if (!mat->ops->restorerowij) *done = PETSC_FALSE;
7662:   else {
7663:     *done = PETSC_TRUE;
7664:     (*mat->ops->restorerowij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);
7665:     if (n)  *n = 0;
7666:     if (ia) *ia = NULL;
7667:     if (ja) *ja = NULL;
7668:   }
7669:   return(0);
7670: }

7672: /*@C
7673:     MatRestoreColumnIJ - Call after you are completed with the ia,ja indices obtained with
7674:     MatGetColumnIJ().

7676:     Collective on Mat

7678:     Input Parameters:
7679: +   mat - the matrix
7680: .   shift - 1 or zero indicating we want the indices starting at 0 or 1
7681: -   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be
7682:                 symmetrized
7683: -   inodecompressed - PETSC_TRUE or PETSC_FALSE indicating if the nonzero structure of the
7684:                  inodes or the nonzero elements is wanted. For BAIJ matrices the compressed version is
7685:                  always used.

7687:     Output Parameters:
7688: +   n - size of (possibly compressed) matrix
7689: .   ia - the column pointers
7690: .   ja - the row indices
7691: -   done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned

7693:     Level: developer

7695: .seealso: MatGetColumnIJ(), MatRestoreRowIJ()
7696: @*/
7697: PetscErrorCode MatRestoreColumnIJ(Mat mat,PetscInt shift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
7698: {

7707:   MatCheckPreallocated(mat,1);

7709:   if (!mat->ops->restorecolumnij) *done = PETSC_FALSE;
7710:   else {
7711:     *done = PETSC_TRUE;
7712:     (*mat->ops->restorecolumnij)(mat,shift,symmetric,inodecompressed,n,ia,ja,done);
7713:     if (n)  *n = 0;
7714:     if (ia) *ia = NULL;
7715:     if (ja) *ja = NULL;
7716:   }
7717:   return(0);
7718: }

7720: /*@C
7721:     MatColoringPatch -Used inside matrix coloring routines that
7722:     use MatGetRowIJ() and/or MatGetColumnIJ().

7724:     Collective on Mat

7726:     Input Parameters:
7727: +   mat - the matrix
7728: .   ncolors - max color value
7729: .   n   - number of entries in colorarray
7730: -   colorarray - array indicating color for each column

7732:     Output Parameters:
7733: .   iscoloring - coloring generated using colorarray information

7735:     Level: developer

7737: .seealso: MatGetRowIJ(), MatGetColumnIJ()

7739: @*/
7740: PetscErrorCode MatColoringPatch(Mat mat,PetscInt ncolors,PetscInt n,ISColoringValue colorarray[],ISColoring *iscoloring)
7741: {

7749:   MatCheckPreallocated(mat,1);

7751:   if (!mat->ops->coloringpatch) {
7752:     ISColoringCreate(PetscObjectComm((PetscObject)mat),ncolors,n,colorarray,PETSC_OWN_POINTER,iscoloring);
7753:   } else {
7754:     (*mat->ops->coloringpatch)(mat,ncolors,n,colorarray,iscoloring);
7755:   }
7756:   return(0);
7757: }


7760: /*@
7761:    MatSetUnfactored - Resets a factored matrix to be treated as unfactored.

7763:    Logically Collective on Mat

7765:    Input Parameter:
7766: .  mat - the factored matrix to be reset

7768:    Notes:
7769:    This routine should be used only with factored matrices formed by in-place
7770:    factorization via ILU(0) (or by in-place LU factorization for the MATSEQDENSE
7771:    format).  This option can save memory, for example, when solving nonlinear
7772:    systems with a matrix-free Newton-Krylov method and a matrix-based, in-place
7773:    ILU(0) preconditioner.

7775:    Note that one can specify in-place ILU(0) factorization by calling
7776: .vb
7777:      PCType(pc,PCILU);
7778:      PCFactorSeUseInPlace(pc);
7779: .ve
7780:    or by using the options -pc_type ilu -pc_factor_in_place

7782:    In-place factorization ILU(0) can also be used as a local
7783:    solver for the blocks within the block Jacobi or additive Schwarz
7784:    methods (runtime option: -sub_pc_factor_in_place).  See Users-Manual: ch_pc
7785:    for details on setting local solver options.

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

7791:    Level: developer

7793: .seealso: PCFactorSetUseInPlace(), PCFactorGetUseInPlace()

7795:    Concepts: matrices^unfactored

7797: @*/
7798: PetscErrorCode MatSetUnfactored(Mat mat)
7799: {

7805:   MatCheckPreallocated(mat,1);
7806:   mat->factortype = MAT_FACTOR_NONE;
7807:   if (!mat->ops->setunfactored) return(0);
7808:   (*mat->ops->setunfactored)(mat);
7809:   return(0);
7810: }

7812: /*MC
7813:     MatDenseGetArrayF90 - Accesses a matrix array from Fortran90.

7815:     Synopsis:
7816:     MatDenseGetArrayF90(Mat x,{Scalar, pointer :: xx_v(:,:)},integer ierr)

7818:     Not collective

7820:     Input Parameter:
7821: .   x - matrix

7823:     Output Parameters:
7824: +   xx_v - the Fortran90 pointer to the array
7825: -   ierr - error code

7827:     Example of Usage:
7828: .vb
7829:       PetscScalar, pointer xx_v(:,:)
7830:       ....
7831:       call MatDenseGetArrayF90(x,xx_v,ierr)
7832:       a = xx_v(3)
7833:       call MatDenseRestoreArrayF90(x,xx_v,ierr)
7834: .ve

7836:     Level: advanced

7838: .seealso:  MatDenseRestoreArrayF90(), MatDenseGetArray(), MatDenseRestoreArray(), MatSeqAIJGetArrayF90()

7840:     Concepts: matrices^accessing array

7842: M*/

7844: /*MC
7845:     MatDenseRestoreArrayF90 - Restores a matrix array that has been
7846:     accessed with MatDenseGetArrayF90().

7848:     Synopsis:
7849:     MatDenseRestoreArrayF90(Mat x,{Scalar, pointer :: xx_v(:,:)},integer ierr)

7851:     Not collective

7853:     Input Parameters:
7854: +   x - matrix
7855: -   xx_v - the Fortran90 pointer to the array

7857:     Output Parameter:
7858: .   ierr - error code

7860:     Example of Usage:
7861: .vb
7862:        PetscScalar, pointer xx_v(:,:)
7863:        ....
7864:        call MatDenseGetArrayF90(x,xx_v,ierr)
7865:        a = xx_v(3)
7866:        call MatDenseRestoreArrayF90(x,xx_v,ierr)
7867: .ve

7869:     Level: advanced

7871: .seealso:  MatDenseGetArrayF90(), MatDenseGetArray(), MatDenseRestoreArray(), MatSeqAIJRestoreArrayF90()

7873: M*/


7876: /*MC
7877:     MatSeqAIJGetArrayF90 - Accesses a matrix array from Fortran90.

7879:     Synopsis:
7880:     MatSeqAIJGetArrayF90(Mat x,{Scalar, pointer :: xx_v(:)},integer ierr)

7882:     Not collective

7884:     Input Parameter:
7885: .   x - matrix

7887:     Output Parameters:
7888: +   xx_v - the Fortran90 pointer to the array
7889: -   ierr - error code

7891:     Example of Usage:
7892: .vb
7893:       PetscScalar, pointer xx_v(:)
7894:       ....
7895:       call MatSeqAIJGetArrayF90(x,xx_v,ierr)
7896:       a = xx_v(3)
7897:       call MatSeqAIJRestoreArrayF90(x,xx_v,ierr)
7898: .ve

7900:     Level: advanced

7902: .seealso:  MatSeqAIJRestoreArrayF90(), MatSeqAIJGetArray(), MatSeqAIJRestoreArray(), MatDenseGetArrayF90()

7904:     Concepts: matrices^accessing array

7906: M*/

7908: /*MC
7909:     MatSeqAIJRestoreArrayF90 - Restores a matrix array that has been
7910:     accessed with MatSeqAIJGetArrayF90().

7912:     Synopsis:
7913:     MatSeqAIJRestoreArrayF90(Mat x,{Scalar, pointer :: xx_v(:)},integer ierr)

7915:     Not collective

7917:     Input Parameters:
7918: +   x - matrix
7919: -   xx_v - the Fortran90 pointer to the array

7921:     Output Parameter:
7922: .   ierr - error code

7924:     Example of Usage:
7925: .vb
7926:        PetscScalar, pointer xx_v(:)
7927:        ....
7928:        call MatSeqAIJGetArrayF90(x,xx_v,ierr)
7929:        a = xx_v(3)
7930:        call MatSeqAIJRestoreArrayF90(x,xx_v,ierr)
7931: .ve

7933:     Level: advanced

7935: .seealso:  MatSeqAIJGetArrayF90(), MatSeqAIJGetArray(), MatSeqAIJRestoreArray(), MatDenseRestoreArrayF90()

7937: M*/


7940: /*@
7941:     MatCreateSubMatrix - Gets a single submatrix on the same number of processors
7942:                       as the original matrix.

7944:     Collective on Mat

7946:     Input Parameters:
7947: +   mat - the original matrix
7948: .   isrow - parallel IS containing the rows this processor should obtain
7949: .   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.
7950: -   cll - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX

7952:     Output Parameter:
7953: .   newmat - the new submatrix, of the same type as the old

7955:     Level: advanced

7957:     Notes:
7958:     The submatrix will be able to be multiplied with vectors using the same layout as iscol.

7960:     Some matrix types place restrictions on the row and column indices, such
7961:     as that they be sorted or that they be equal to each other.

7963:     The index sets may not have duplicate entries.

7965:       The first time this is called you should use a cll of MAT_INITIAL_MATRIX,
7966:    the MatCreateSubMatrix() routine will create the newmat for you. Any additional calls
7967:    to this routine with a mat of the same nonzero structure and with a call of MAT_REUSE_MATRIX
7968:    will reuse the matrix generated the first time.  You should call MatDestroy() on newmat when
7969:    you are finished using it.

7971:     The communicator of the newly obtained matrix is ALWAYS the same as the communicator of
7972:     the input matrix.

7974:     If iscol is NULL then all columns are obtained (not supported in Fortran).

7976:    Example usage:
7977:    Consider the following 8x8 matrix with 34 non-zero values, that is
7978:    assembled across 3 processors. Let's assume that proc0 owns 3 rows,
7979:    proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown
7980:    as follows:

7982: .vb
7983:             1  2  0  |  0  3  0  |  0  4
7984:     Proc0   0  5  6  |  7  0  0  |  8  0
7985:             9  0 10  | 11  0  0  | 12  0
7986:     -------------------------------------
7987:            13  0 14  | 15 16 17  |  0  0
7988:     Proc1   0 18  0  | 19 20 21  |  0  0
7989:             0  0  0  | 22 23  0  | 24  0
7990:     -------------------------------------
7991:     Proc2  25 26 27  |  0  0 28  | 29  0
7992:            30  0  0  | 31 32 33  |  0 34
7993: .ve

7995:     Suppose isrow = [0 1 | 4 | 6 7] and iscol = [1 2 | 3 4 5 | 6].  The resulting submatrix is

7997: .vb
7998:             2  0  |  0  3  0  |  0
7999:     Proc0   5  6  |  7  0  0  |  8
8000:     -------------------------------
8001:     Proc1  18  0  | 19 20 21  |  0
8002:     -------------------------------
8003:     Proc2  26 27  |  0  0 28  | 29
8004:             0  0  | 31 32 33  |  0
8005: .ve


8008:     Concepts: matrices^submatrices

8010: .seealso: MatCreateSubMatrices()
8011: @*/
8012: PetscErrorCode MatCreateSubMatrix(Mat mat,IS isrow,IS iscol,MatReuse cll,Mat *newmat)
8013: {
8015:   PetscMPIInt    size;
8016:   Mat            *local;
8017:   IS             iscoltmp;

8026:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8027:   if (cll == MAT_IGNORE_MATRIX) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Cannot use MAT_IGNORE_MATRIX");

8029:   MatCheckPreallocated(mat,1);
8030:   MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);

8032:   if (!iscol || isrow == iscol) {
8033:     PetscBool   stride;
8034:     PetscMPIInt grabentirematrix = 0,grab;
8035:     PetscObjectTypeCompare((PetscObject)isrow,ISSTRIDE,&stride);
8036:     if (stride) {
8037:       PetscInt first,step,n,rstart,rend;
8038:       ISStrideGetInfo(isrow,&first,&step);
8039:       if (step == 1) {
8040:         MatGetOwnershipRange(mat,&rstart,&rend);
8041:         if (rstart == first) {
8042:           ISGetLocalSize(isrow,&n);
8043:           if (n == rend-rstart) {
8044:             grabentirematrix = 1;
8045:           }
8046:         }
8047:       }
8048:     }
8049:     MPIU_Allreduce(&grabentirematrix,&grab,1,MPI_INT,MPI_MIN,PetscObjectComm((PetscObject)mat));
8050:     if (grab) {
8051:       PetscInfo(mat,"Getting entire matrix as submatrix\n");
8052:       if (cll == MAT_INITIAL_MATRIX) {
8053:         *newmat = mat;
8054:         PetscObjectReference((PetscObject)mat);
8055:       }
8056:       return(0);
8057:     }
8058:   }

8060:   if (!iscol) {
8061:     ISCreateStride(PetscObjectComm((PetscObject)mat),mat->cmap->n,mat->cmap->rstart,1,&iscoltmp);
8062:   } else {
8063:     iscoltmp = iscol;
8064:   }

8066:   /* if original matrix is on just one processor then use submatrix generated */
8067:   if (mat->ops->createsubmatrices && !mat->ops->createsubmatrix && size == 1 && cll == MAT_REUSE_MATRIX) {
8068:     MatCreateSubMatrices(mat,1,&isrow,&iscoltmp,MAT_REUSE_MATRIX,&newmat);
8069:     goto setproperties;
8070:   } else if (mat->ops->createsubmatrices && !mat->ops->createsubmatrix && size == 1) {
8071:     MatCreateSubMatrices(mat,1,&isrow,&iscoltmp,MAT_INITIAL_MATRIX,&local);
8072:     *newmat = *local;
8073:     PetscFree(local);
8074:     goto setproperties;
8075:   } else if (!mat->ops->createsubmatrix) {
8076:     /* Create a new matrix type that implements the operation using the full matrix */
8077:     PetscLogEventBegin(MAT_CreateSubMat,mat,0,0,0);
8078:     switch (cll) {
8079:     case MAT_INITIAL_MATRIX:
8080:       MatCreateSubMatrixVirtual(mat,isrow,iscoltmp,newmat);
8081:       break;
8082:     case MAT_REUSE_MATRIX:
8083:       MatSubMatrixVirtualUpdate(*newmat,mat,isrow,iscoltmp);
8084:       break;
8085:     default: SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"Invalid MatReuse, must be either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX");
8086:     }
8087:     PetscLogEventEnd(MAT_CreateSubMat,mat,0,0,0);
8088:     goto setproperties;
8089:   }

8091:   if (!mat->ops->createsubmatrix) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
8092:   PetscLogEventBegin(MAT_CreateSubMat,mat,0,0,0);
8093:   (*mat->ops->createsubmatrix)(mat,isrow,iscoltmp,cll,newmat);
8094:   PetscLogEventEnd(MAT_CreateSubMat,mat,0,0,0);

8096:   /* Propagate symmetry information for diagonal blocks */
8097: setproperties:
8098:   if (isrow == iscoltmp) {
8099:     if (mat->symmetric_set && mat->symmetric) {
8100:       MatSetOption(*newmat,MAT_SYMMETRIC,PETSC_TRUE);
8101:     }
8102:     if (mat->structurally_symmetric_set && mat->structurally_symmetric) {
8103:       MatSetOption(*newmat,MAT_STRUCTURALLY_SYMMETRIC,PETSC_TRUE);
8104:     }
8105:     if (mat->hermitian_set && mat->hermitian) {
8106:       MatSetOption(*newmat,MAT_HERMITIAN,PETSC_TRUE);
8107:     }
8108:     if (mat->spd_set && mat->spd) {
8109:       MatSetOption(*newmat,MAT_SPD,PETSC_TRUE);
8110:     }
8111:   }

8113:   if (!iscol) {ISDestroy(&iscoltmp);}
8114:   if (*newmat && cll == MAT_INITIAL_MATRIX) {PetscObjectStateIncrease((PetscObject)*newmat);}
8115:   return(0);
8116: }

8118: /*@
8119:    MatStashSetInitialSize - sets the sizes of the matrix stash, that is
8120:    used during the assembly process to store values that belong to
8121:    other processors.

8123:    Not Collective

8125:    Input Parameters:
8126: +  mat   - the matrix
8127: .  size  - the initial size of the stash.
8128: -  bsize - the initial size of the block-stash(if used).

8130:    Options Database Keys:
8131: +   -matstash_initial_size <size> or <size0,size1,...sizep-1>
8132: -   -matstash_block_initial_size <bsize>  or <bsize0,bsize1,...bsizep-1>

8134:    Level: intermediate

8136:    Notes:
8137:      The block-stash is used for values set with MatSetValuesBlocked() while
8138:      the stash is used for values set with MatSetValues()

8140:      Run with the option -info and look for output of the form
8141:      MatAssemblyBegin_MPIXXX:Stash has MM entries, uses nn mallocs.
8142:      to determine the appropriate value, MM, to use for size and
8143:      MatAssemblyBegin_MPIXXX:Block-Stash has BMM entries, uses nn mallocs.
8144:      to determine the value, BMM to use for bsize

8146:    Concepts: stash^setting matrix size
8147:    Concepts: matrices^stash

8149: .seealso: MatAssemblyBegin(), MatAssemblyEnd(), Mat, MatStashGetInfo()

8151: @*/
8152: PetscErrorCode MatStashSetInitialSize(Mat mat,PetscInt size, PetscInt bsize)
8153: {

8159:   MatStashSetInitialSize_Private(&mat->stash,size);
8160:   MatStashSetInitialSize_Private(&mat->bstash,bsize);
8161:   return(0);
8162: }

8164: /*@
8165:    MatInterpolateAdd - w = y + A*x or A'*x depending on the shape of
8166:      the matrix

8168:    Neighbor-wise Collective on Mat

8170:    Input Parameters:
8171: +  mat   - the matrix
8172: .  x,y - the vectors
8173: -  w - where the result is stored

8175:    Level: intermediate

8177:    Notes:
8178:     w may be the same vector as y.

8180:     This allows one to use either the restriction or interpolation (its transpose)
8181:     matrix to do the interpolation

8183:     Concepts: interpolation

8185: .seealso: MatMultAdd(), MatMultTransposeAdd(), MatRestrict()

8187: @*/
8188: PetscErrorCode MatInterpolateAdd(Mat A,Vec x,Vec y,Vec w)
8189: {
8191:   PetscInt       M,N,Ny;

8199:   MatCheckPreallocated(A,1);
8200:   MatGetSize(A,&M,&N);
8201:   VecGetSize(y,&Ny);
8202:   if (M == Ny) {
8203:     MatMultAdd(A,x,y,w);
8204:   } else {
8205:     MatMultTransposeAdd(A,x,y,w);
8206:   }
8207:   return(0);
8208: }

8210: /*@
8211:    MatInterpolate - y = A*x or A'*x depending on the shape of
8212:      the matrix

8214:    Neighbor-wise Collective on Mat

8216:    Input Parameters:
8217: +  mat   - the matrix
8218: -  x,y - the vectors

8220:    Level: intermediate

8222:    Notes:
8223:     This allows one to use either the restriction or interpolation (its transpose)
8224:     matrix to do the interpolation

8226:    Concepts: matrices^interpolation

8228: .seealso: MatMultAdd(), MatMultTransposeAdd(), MatRestrict()

8230: @*/
8231: PetscErrorCode MatInterpolate(Mat A,Vec x,Vec y)
8232: {
8234:   PetscInt       M,N,Ny;

8241:   MatCheckPreallocated(A,1);
8242:   MatGetSize(A,&M,&N);
8243:   VecGetSize(y,&Ny);
8244:   if (M == Ny) {
8245:     MatMult(A,x,y);
8246:   } else {
8247:     MatMultTranspose(A,x,y);
8248:   }
8249:   return(0);
8250: }

8252: /*@
8253:    MatRestrict - y = A*x or A'*x

8255:    Neighbor-wise Collective on Mat

8257:    Input Parameters:
8258: +  mat   - the matrix
8259: -  x,y - the vectors

8261:    Level: intermediate

8263:    Notes:
8264:     This allows one to use either the restriction or interpolation (its transpose)
8265:     matrix to do the restriction

8267:    Concepts: matrices^restriction

8269: .seealso: MatMultAdd(), MatMultTransposeAdd(), MatInterpolate()

8271: @*/
8272: PetscErrorCode MatRestrict(Mat A,Vec x,Vec y)
8273: {
8275:   PetscInt       M,N,Ny;

8282:   MatCheckPreallocated(A,1);

8284:   MatGetSize(A,&M,&N);
8285:   VecGetSize(y,&Ny);
8286:   if (M == Ny) {
8287:     MatMult(A,x,y);
8288:   } else {
8289:     MatMultTranspose(A,x,y);
8290:   }
8291:   return(0);
8292: }

8294: /*@
8295:    MatGetNullSpace - retrieves the null space of a matrix.

8297:    Logically Collective on Mat and MatNullSpace

8299:    Input Parameters:
8300: +  mat - the matrix
8301: -  nullsp - the null space object

8303:    Level: developer

8305:    Concepts: null space^attaching to matrix

8307: .seealso: MatCreate(), MatNullSpaceCreate(), MatSetNearNullSpace(), MatSetNullSpace()
8308: @*/
8309: PetscErrorCode MatGetNullSpace(Mat mat, MatNullSpace *nullsp)
8310: {
8314:   *nullsp = (mat->symmetric_set && mat->symmetric && !mat->nullsp) ? mat->transnullsp : mat->nullsp;
8315:   return(0);
8316: }

8318: /*@
8319:    MatSetNullSpace - attaches a null space to a matrix.

8321:    Logically Collective on Mat and MatNullSpace

8323:    Input Parameters:
8324: +  mat - the matrix
8325: -  nullsp - the null space object

8327:    Level: advanced

8329:    Notes:
8330:       This null space is used by the linear solvers. Overwrites any previous null space that may have been attached

8332:       For inconsistent singular systems (linear systems where the right hand side is not in the range of the operator) you also likely should
8333:       call MatSetTransposeNullSpace(). This allows the linear system to be solved in a least squares sense.

8335:       You can remove the null space by calling this routine with an nullsp of NULL


8338:       The fundamental theorem of linear algebra (Gilbert Strang, Introduction to Applied Mathematics, page 72) states that
8339:    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).
8340:    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
8341:    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
8342:    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).

8344:       Krylov solvers can produce the minimal norm solution to the least squares problem by utilizing MatNullSpaceRemove().

8346:     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
8347:     routine also automatically calls MatSetTransposeNullSpace().

8349:    Concepts: null space^attaching to matrix

8351: .seealso: MatCreate(), MatNullSpaceCreate(), MatSetNearNullSpace(), MatGetNullSpace(), MatSetTransposeNullSpace(), MatGetTransposeNullSpace(), MatNullSpaceRemove()
8352: @*/
8353: PetscErrorCode MatSetNullSpace(Mat mat,MatNullSpace nullsp)
8354: {

8360:   if (nullsp) {PetscObjectReference((PetscObject)nullsp);}
8361:   MatNullSpaceDestroy(&mat->nullsp);
8362:   mat->nullsp = nullsp;
8363:   if (mat->symmetric_set && mat->symmetric) {
8364:     MatSetTransposeNullSpace(mat,nullsp);
8365:   }
8366:   return(0);
8367: }

8369: /*@
8370:    MatGetTransposeNullSpace - retrieves the null space of the transpose of a matrix.

8372:    Logically Collective on Mat and MatNullSpace

8374:    Input Parameters:
8375: +  mat - the matrix
8376: -  nullsp - the null space object

8378:    Level: developer

8380:    Concepts: null space^attaching to matrix

8382: .seealso: MatCreate(), MatNullSpaceCreate(), MatSetNearNullSpace(), MatSetTransposeNullSpace(), MatSetNullSpace(), MatGetNullSpace()
8383: @*/
8384: PetscErrorCode MatGetTransposeNullSpace(Mat mat, MatNullSpace *nullsp)
8385: {
8390:   *nullsp = (mat->symmetric_set && mat->symmetric && !mat->transnullsp) ? mat->nullsp : mat->transnullsp;
8391:   return(0);
8392: }

8394: /*@
8395:    MatSetTransposeNullSpace - attaches a null space to a matrix.

8397:    Logically Collective on Mat and MatNullSpace

8399:    Input Parameters:
8400: +  mat - the matrix
8401: -  nullsp - the null space object

8403:    Level: advanced

8405:    Notes:
8406:       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.
8407:       You must also call MatSetNullSpace()


8410:       The fundamental theorem of linear algebra (Gilbert Strang, Introduction to Applied Mathematics, page 72) states that
8411:    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).
8412:    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
8413:    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
8414:    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).

8416:       Krylov solvers can produce the minimal norm solution to the least squares problem by utilizing MatNullSpaceRemove().

8418:    Concepts: null space^attaching to matrix

8420: .seealso: MatCreate(), MatNullSpaceCreate(), MatSetNearNullSpace(), MatGetNullSpace(), MatSetNullSpace(), MatGetTransposeNullSpace(), MatNullSpaceRemove()
8421: @*/
8422: PetscErrorCode MatSetTransposeNullSpace(Mat mat,MatNullSpace nullsp)
8423: {

8429:   if (nullsp) {PetscObjectReference((PetscObject)nullsp);}
8430:   MatNullSpaceDestroy(&mat->transnullsp);
8431:   mat->transnullsp = nullsp;
8432:   return(0);
8433: }

8435: /*@
8436:    MatSetNearNullSpace - attaches a null space to a matrix, which is often the null space (rigid body modes) of the operator without boundary conditions
8437:         This null space will be used to provide near null space vectors to a multigrid preconditioner built from this matrix.

8439:    Logically Collective on Mat and MatNullSpace

8441:    Input Parameters:
8442: +  mat - the matrix
8443: -  nullsp - the null space object

8445:    Level: advanced

8447:    Notes:
8448:       Overwrites any previous near null space that may have been attached

8450:       You can remove the null space by calling this routine with an nullsp of NULL

8452:    Concepts: null space^attaching to matrix

8454: .seealso: MatCreate(), MatNullSpaceCreate(), MatSetNullSpace(), MatNullSpaceCreateRigidBody(), MatGetNearNullSpace()
8455: @*/
8456: PetscErrorCode MatSetNearNullSpace(Mat mat,MatNullSpace nullsp)
8457: {

8464:   MatCheckPreallocated(mat,1);
8465:   if (nullsp) {PetscObjectReference((PetscObject)nullsp);}
8466:   MatNullSpaceDestroy(&mat->nearnullsp);
8467:   mat->nearnullsp = nullsp;
8468:   return(0);
8469: }

8471: /*@
8472:    MatGetNearNullSpace -Get null space attached with MatSetNearNullSpace()

8474:    Not Collective

8476:    Input Parameters:
8477: .  mat - the matrix

8479:    Output Parameters:
8480: .  nullsp - the null space object, NULL if not set

8482:    Level: developer

8484:    Concepts: null space^attaching to matrix

8486: .seealso: MatSetNearNullSpace(), MatGetNullSpace(), MatNullSpaceCreate()
8487: @*/
8488: PetscErrorCode MatGetNearNullSpace(Mat mat,MatNullSpace *nullsp)
8489: {
8494:   MatCheckPreallocated(mat,1);
8495:   *nullsp = mat->nearnullsp;
8496:   return(0);
8497: }

8499: /*@C
8500:    MatICCFactor - Performs in-place incomplete Cholesky factorization of matrix.

8502:    Collective on Mat

8504:    Input Parameters:
8505: +  mat - the matrix
8506: .  row - row/column permutation
8507: .  fill - expected fill factor >= 1.0
8508: -  level - level of fill, for ICC(k)

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

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

8518:    Level: developer

8520:    Concepts: matrices^incomplete Cholesky factorization
8521:    Concepts: Cholesky factorization

8523: .seealso: MatICCFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor()

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

8528: @*/
8529: PetscErrorCode MatICCFactor(Mat mat,IS row,const MatFactorInfo *info)
8530: {

8538:   if (mat->rmap->N != mat->cmap->N) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONG,"matrix must be square");
8539:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
8540:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
8541:   if (!mat->ops->iccfactor) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
8542:   MatCheckPreallocated(mat,1);
8543:   (*mat->ops->iccfactor)(mat,row,info);
8544:   PetscObjectStateIncrease((PetscObject)mat);
8545:   return(0);
8546: }

8548: /*@
8549:    MatDiagonalScaleLocal - Scales columns of a matrix given the scaling values including the
8550:          ghosted ones.

8552:    Not Collective

8554:    Input Parameters:
8555: +  mat - the matrix
8556: -  diag = the diagonal values, including ghost ones

8558:    Level: developer

8560:    Notes:
8561:     Works only for MPIAIJ and MPIBAIJ matrices

8563: .seealso: MatDiagonalScale()
8564: @*/
8565: PetscErrorCode MatDiagonalScaleLocal(Mat mat,Vec diag)
8566: {
8568:   PetscMPIInt    size;


8575:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Matrix must be already assembled");
8576:   PetscLogEventBegin(MAT_Scale,mat,0,0,0);
8577:   MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);
8578:   if (size == 1) {
8579:     PetscInt n,m;
8580:     VecGetSize(diag,&n);
8581:     MatGetSize(mat,0,&m);
8582:     if (m == n) {
8583:       MatDiagonalScale(mat,0,diag);
8584:     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only supported for sequential matrices when no ghost points/periodic conditions");
8585:   } else {
8586:     PetscUseMethod(mat,"MatDiagonalScaleLocal_C",(Mat,Vec),(mat,diag));
8587:   }
8588:   PetscLogEventEnd(MAT_Scale,mat,0,0,0);
8589:   PetscObjectStateIncrease((PetscObject)mat);
8590:   return(0);
8591: }

8593: /*@
8594:    MatGetInertia - Gets the inertia from a factored matrix

8596:    Collective on Mat

8598:    Input Parameter:
8599: .  mat - the matrix

8601:    Output Parameters:
8602: +   nneg - number of negative eigenvalues
8603: .   nzero - number of zero eigenvalues
8604: -   npos - number of positive eigenvalues

8606:    Level: advanced

8608:    Notes:
8609:     Matrix must have been factored by MatCholeskyFactor()


8612: @*/
8613: PetscErrorCode MatGetInertia(Mat mat,PetscInt *nneg,PetscInt *nzero,PetscInt *npos)
8614: {

8620:   if (!mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
8621:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Numeric factor mat is not assembled");
8622:   if (!mat->ops->getinertia) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
8623:   (*mat->ops->getinertia)(mat,nneg,nzero,npos);
8624:   return(0);
8625: }

8627: /* ----------------------------------------------------------------*/
8628: /*@C
8629:    MatSolves - Solves A x = b, given a factored matrix, for a collection of vectors

8631:    Neighbor-wise Collective on Mat and Vecs

8633:    Input Parameters:
8634: +  mat - the factored matrix
8635: -  b - the right-hand-side vectors

8637:    Output Parameter:
8638: .  x - the result vectors

8640:    Notes:
8641:    The vectors b and x cannot be the same.  I.e., one cannot
8642:    call MatSolves(A,x,x).

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

8649:    Level: developer

8651:    Concepts: matrices^triangular solves

8653: .seealso: MatSolveAdd(), MatSolveTranspose(), MatSolveTransposeAdd(), MatSolve()
8654: @*/
8655: PetscErrorCode MatSolves(Mat mat,Vecs b,Vecs x)
8656: {

8662:   if (x == b) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_IDN,"x and b must be different vectors");
8663:   if (!mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
8664:   if (!mat->rmap->N && !mat->cmap->N) return(0);

8666:   if (!mat->ops->solves) SETERRQ1(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)mat)->type_name);
8667:   MatCheckPreallocated(mat,1);
8668:   PetscLogEventBegin(MAT_Solves,mat,0,0,0);
8669:   (*mat->ops->solves)(mat,b,x);
8670:   PetscLogEventEnd(MAT_Solves,mat,0,0,0);
8671:   return(0);
8672: }

8674: /*@
8675:    MatIsSymmetric - Test whether a matrix is symmetric

8677:    Collective on Mat

8679:    Input Parameter:
8680: +  A - the matrix to test
8681: -  tol - difference between value and its transpose less than this amount counts as equal (use 0.0 for exact transpose)

8683:    Output Parameters:
8684: .  flg - the result

8686:    Notes:
8687:     For real numbers MatIsSymmetric() and MatIsHermitian() return identical results

8689:    Level: intermediate

8691:    Concepts: matrix^symmetry

8693: .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), MatIsSymmetricKnown()
8694: @*/
8695: PetscErrorCode MatIsSymmetric(Mat A,PetscReal tol,PetscBool  *flg)
8696: {


8703:   if (!A->symmetric_set) {
8704:     if (!A->ops->issymmetric) {
8705:       MatType mattype;
8706:       MatGetType(A,&mattype);
8707:       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type <%s> does not support checking for symmetric",mattype);
8708:     }
8709:     (*A->ops->issymmetric)(A,tol,flg);
8710:     if (!tol) {
8711:       A->symmetric_set = PETSC_TRUE;
8712:       A->symmetric     = *flg;
8713:       if (A->symmetric) {
8714:         A->structurally_symmetric_set = PETSC_TRUE;
8715:         A->structurally_symmetric     = PETSC_TRUE;
8716:       }
8717:     }
8718:   } else if (A->symmetric) {
8719:     *flg = PETSC_TRUE;
8720:   } else if (!tol) {
8721:     *flg = PETSC_FALSE;
8722:   } else {
8723:     if (!A->ops->issymmetric) {
8724:       MatType mattype;
8725:       MatGetType(A,&mattype);
8726:       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type <%s> does not support checking for symmetric",mattype);
8727:     }
8728:     (*A->ops->issymmetric)(A,tol,flg);
8729:   }
8730:   return(0);
8731: }

8733: /*@
8734:    MatIsHermitian - Test whether a matrix is Hermitian

8736:    Collective on Mat

8738:    Input Parameter:
8739: +  A - the matrix to test
8740: -  tol - difference between value and its transpose less than this amount counts as equal (use 0.0 for exact Hermitian)

8742:    Output Parameters:
8743: .  flg - the result

8745:    Level: intermediate

8747:    Concepts: matrix^symmetry

8749: .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(),
8750:           MatIsSymmetricKnown(), MatIsSymmetric()
8751: @*/
8752: PetscErrorCode MatIsHermitian(Mat A,PetscReal tol,PetscBool  *flg)
8753: {


8760:   if (!A->hermitian_set) {
8761:     if (!A->ops->ishermitian) {
8762:       MatType mattype;
8763:       MatGetType(A,&mattype);
8764:       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type <%s> does not support checking for hermitian",mattype);
8765:     }
8766:     (*A->ops->ishermitian)(A,tol,flg);
8767:     if (!tol) {
8768:       A->hermitian_set = PETSC_TRUE;
8769:       A->hermitian     = *flg;
8770:       if (A->hermitian) {
8771:         A->structurally_symmetric_set = PETSC_TRUE;
8772:         A->structurally_symmetric     = PETSC_TRUE;
8773:       }
8774:     }
8775:   } else if (A->hermitian) {
8776:     *flg = PETSC_TRUE;
8777:   } else if (!tol) {
8778:     *flg = PETSC_FALSE;
8779:   } else {
8780:     if (!A->ops->ishermitian) {
8781:       MatType mattype;
8782:       MatGetType(A,&mattype);
8783:       SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix of type <%s> does not support checking for hermitian",mattype);
8784:     }
8785:     (*A->ops->ishermitian)(A,tol,flg);
8786:   }
8787:   return(0);
8788: }

8790: /*@
8791:    MatIsSymmetricKnown - Checks the flag on the matrix to see if it is symmetric.

8793:    Not Collective

8795:    Input Parameter:
8796: .  A - the matrix to check

8798:    Output Parameters:
8799: +  set - if the symmetric flag is set (this tells you if the next flag is valid)
8800: -  flg - the result

8802:    Level: advanced

8804:    Concepts: matrix^symmetry

8806:    Note: Does not check the matrix values directly, so this may return unknown (set = PETSC_FALSE). Use MatIsSymmetric()
8807:          if you want it explicitly checked

8809: .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), MatIsSymmetric()
8810: @*/
8811: PetscErrorCode MatIsSymmetricKnown(Mat A,PetscBool  *set,PetscBool  *flg)
8812: {
8817:   if (A->symmetric_set) {
8818:     *set = PETSC_TRUE;
8819:     *flg = A->symmetric;
8820:   } else {
8821:     *set = PETSC_FALSE;
8822:   }
8823:   return(0);
8824: }

8826: /*@
8827:    MatIsHermitianKnown - Checks the flag on the matrix to see if it is hermitian.

8829:    Not Collective

8831:    Input Parameter:
8832: .  A - the matrix to check

8834:    Output Parameters:
8835: +  set - if the hermitian flag is set (this tells you if the next flag is valid)
8836: -  flg - the result

8838:    Level: advanced

8840:    Concepts: matrix^symmetry

8842:    Note: Does not check the matrix values directly, so this may return unknown (set = PETSC_FALSE). Use MatIsHermitian()
8843:          if you want it explicitly checked

8845: .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsStructurallySymmetric(), MatSetOption(), MatIsSymmetric()
8846: @*/
8847: PetscErrorCode MatIsHermitianKnown(Mat A,PetscBool  *set,PetscBool  *flg)
8848: {
8853:   if (A->hermitian_set) {
8854:     *set = PETSC_TRUE;
8855:     *flg = A->hermitian;
8856:   } else {
8857:     *set = PETSC_FALSE;
8858:   }
8859:   return(0);
8860: }

8862: /*@
8863:    MatIsStructurallySymmetric - Test whether a matrix is structurally symmetric

8865:    Collective on Mat

8867:    Input Parameter:
8868: .  A - the matrix to test

8870:    Output Parameters:
8871: .  flg - the result

8873:    Level: intermediate

8875:    Concepts: matrix^symmetry

8877: .seealso: MatTranspose(), MatIsTranspose(), MatIsHermitian(), MatIsSymmetric(), MatSetOption()
8878: @*/
8879: PetscErrorCode MatIsStructurallySymmetric(Mat A,PetscBool  *flg)
8880: {

8886:   if (!A->structurally_symmetric_set) {
8887:     if (!A->ops->isstructurallysymmetric) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Matrix does not support checking for structural symmetric");
8888:     (*A->ops->isstructurallysymmetric)(A,&A->structurally_symmetric);

8890:     A->structurally_symmetric_set = PETSC_TRUE;
8891:   }
8892:   *flg = A->structurally_symmetric;
8893:   return(0);
8894: }

8896: /*@
8897:    MatStashGetInfo - Gets how many values are currently in the matrix stash, i.e. need
8898:        to be communicated to other processors during the MatAssemblyBegin/End() process

8900:     Not collective

8902:    Input Parameter:
8903: .   vec - the vector

8905:    Output Parameters:
8906: +   nstash   - the size of the stash
8907: .   reallocs - the number of additional mallocs incurred.
8908: .   bnstash   - the size of the block stash
8909: -   breallocs - the number of additional mallocs incurred.in the block stash

8911:    Level: advanced

8913: .seealso: MatAssemblyBegin(), MatAssemblyEnd(), Mat, MatStashSetInitialSize()

8915: @*/
8916: PetscErrorCode MatStashGetInfo(Mat mat,PetscInt *nstash,PetscInt *reallocs,PetscInt *bnstash,PetscInt *breallocs)
8917: {

8921:   MatStashGetInfo_Private(&mat->stash,nstash,reallocs);
8922:   MatStashGetInfo_Private(&mat->bstash,bnstash,breallocs);
8923:   return(0);
8924: }

8926: /*@C
8927:    MatCreateVecs - Get vector(s) compatible with the matrix, i.e. with the same
8928:      parallel layout

8930:    Collective on Mat

8932:    Input Parameter:
8933: .  mat - the matrix

8935:    Output Parameter:
8936: +   right - (optional) vector that the matrix can be multiplied against
8937: -   left - (optional) vector that the matrix vector product can be stored in

8939:    Notes:
8940:     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().

8942:   Notes:
8943:     These are new vectors which are not owned by the Mat, they should be destroyed in VecDestroy() when no longer needed

8945:   Level: advanced

8947: .seealso: MatCreate(), VecDestroy()
8948: @*/
8949: PetscErrorCode MatCreateVecs(Mat mat,Vec *right,Vec *left)
8950: {

8956:   if (mat->ops->getvecs) {
8957:     (*mat->ops->getvecs)(mat,right,left);
8958:   } else {
8959:     PetscInt rbs,cbs;
8960:     MatGetBlockSizes(mat,&rbs,&cbs);
8961:     if (right) {
8962:       if (mat->cmap->n < 0) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"PetscLayout for columns not yet setup");
8963:       VecCreate(PetscObjectComm((PetscObject)mat),right);
8964:       VecSetSizes(*right,mat->cmap->n,PETSC_DETERMINE);
8965:       VecSetBlockSize(*right,cbs);
8966:       VecSetType(*right,mat->defaultvectype);
8967:       PetscLayoutReference(mat->cmap,&(*right)->map);
8968:     }
8969:     if (left) {
8970:       if (mat->rmap->n < 0) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"PetscLayout for rows not yet setup");
8971:       VecCreate(PetscObjectComm((PetscObject)mat),left);
8972:       VecSetSizes(*left,mat->rmap->n,PETSC_DETERMINE);
8973:       VecSetBlockSize(*left,rbs);
8974:       VecSetType(*left,mat->defaultvectype);
8975:       PetscLayoutReference(mat->rmap,&(*left)->map);
8976:     }
8977:   }
8978:   return(0);
8979: }

8981: /*@C
8982:    MatFactorInfoInitialize - Initializes a MatFactorInfo data structure
8983:      with default values.

8985:    Not Collective

8987:    Input Parameters:
8988: .    info - the MatFactorInfo data structure


8991:    Notes:
8992:     The solvers are generally used through the KSP and PC objects, for example
8993:           PCLU, PCILU, PCCHOLESKY, PCICC

8995:    Level: developer

8997: .seealso: MatFactorInfo

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

9002: @*/

9004: PetscErrorCode MatFactorInfoInitialize(MatFactorInfo *info)
9005: {

9009:   PetscMemzero(info,sizeof(MatFactorInfo));
9010:   return(0);
9011: }

9013: /*@
9014:    MatFactorSetSchurIS - Set indices corresponding to the Schur complement you wish to have computed

9016:    Collective on Mat

9018:    Input Parameters:
9019: +  mat - the factored matrix
9020: -  is - the index set defining the Schur indices (0-based)

9022:    Notes:
9023:     Call MatFactorSolveSchurComplement() or MatFactorSolveSchurComplementTranspose() after this call to solve a Schur complement system.

9025:    You can call MatFactorGetSchurComplement() or MatFactorCreateSchurComplement() after this call.

9027:    Level: developer

9029:    Concepts:

9031: .seealso: MatGetFactor(), MatFactorGetSchurComplement(), MatFactorRestoreSchurComplement(), MatFactorCreateSchurComplement(), MatFactorSolveSchurComplement(),
9032:           MatFactorSolveSchurComplementTranspose(), MatFactorSolveSchurComplement()

9034: @*/
9035: PetscErrorCode MatFactorSetSchurIS(Mat mat,IS is)
9036: {
9037:   PetscErrorCode ierr,(*f)(Mat,IS);

9045:   if (!mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Only for factored matrix");
9046:   PetscObjectQueryFunction((PetscObject)mat,"MatFactorSetSchurIS_C",&f);
9047:   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");
9048:   if (mat->schur) {
9049:     MatDestroy(&mat->schur);
9050:   }
9051:   (*f)(mat,is);
9052:   if (!mat->schur) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_PLIB,"Schur complement has not been created");
9053:   MatFactorSetUpInPlaceSchur_Private(mat);
9054:   return(0);
9055: }

9057: /*@
9058:   MatFactorCreateSchurComplement - Create a Schur complement matrix object using Schur data computed during the factorization step

9060:    Logically Collective on Mat

9062:    Input Parameters:
9063: +  F - the factored matrix obtained by calling MatGetFactor() from PETSc-MUMPS interface
9064: .  S - location where to return the Schur complement, can be NULL
9065: -  status - the status of the Schur complement matrix, can be NULL

9067:    Notes:
9068:    You must call MatFactorSetSchurIS() before calling this routine.

9070:    The routine provides a copy of the Schur matrix stored within the solver data structures.
9071:    The caller must destroy the object when it is no longer needed.
9072:    If MatFactorInvertSchurComplement() has been called, the routine gets back the inverse.

9074:    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)

9076:    Developer Notes:
9077:     The reason this routine exists is because the representation of the Schur complement within the factor matrix may be different than a standard PETSc
9078:    matrix representation and we normally do not want to use the time or memory to make a copy as a regular PETSc matrix.

9080:    See MatCreateSchurComplement() or MatGetSchurComplement() for ways to create virtual or approximate Schur complements.

9082:    Level: advanced

9084:    References:

9086: .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorGetSchurComplement(), MatFactorSchurStatus
9087: @*/
9088: PetscErrorCode MatFactorCreateSchurComplement(Mat F,Mat* S,MatFactorSchurStatus* status)
9089: {

9096:   if (S) {
9097:     PetscErrorCode (*f)(Mat,Mat*);

9099:     PetscObjectQueryFunction((PetscObject)F,"MatFactorCreateSchurComplement_C",&f);
9100:     if (f) {
9101:       (*f)(F,S);
9102:     } else {
9103:       MatDuplicate(F->schur,MAT_COPY_VALUES,S);
9104:     }
9105:   }
9106:   if (status) *status = F->schur_status;
9107:   return(0);
9108: }

9110: /*@
9111:   MatFactorGetSchurComplement - Gets access to a Schur complement matrix using the current Schur data within a factored matrix

9113:    Logically Collective on Mat

9115:    Input Parameters:
9116: +  F - the factored matrix obtained by calling MatGetFactor()
9117: .  *S - location where to return the Schur complement, can be NULL
9118: -  status - the status of the Schur complement matrix, can be NULL

9120:    Notes:
9121:    You must call MatFactorSetSchurIS() before calling this routine.

9123:    Schur complement mode is currently implemented for sequential matrices.
9124:    The routine returns a the Schur Complement stored within the data strutures of the solver.
9125:    If MatFactorInvertSchurComplement() has previously been called, the returned matrix is actually the inverse of the Schur complement.
9126:    The returned matrix should not be destroyed; the caller should call MatFactorRestoreSchurComplement() when the object is no longer needed.

9128:    Use MatFactorCreateSchurComplement() to create a copy of the Schur complement matrix that is within a factored matrix

9130:    See MatCreateSchurComplement() or MatGetSchurComplement() for ways to create virtual or approximate Schur complements.

9132:    Level: advanced

9134:    References:

9136: .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorRestoreSchurComplement(), MatFactorCreateSchurComplement(), MatFactorSchurStatus
9137: @*/
9138: PetscErrorCode MatFactorGetSchurComplement(Mat F,Mat* S,MatFactorSchurStatus* status)
9139: {
9144:   if (S) *S = F->schur;
9145:   if (status) *status = F->schur_status;
9146:   return(0);
9147: }

9149: /*@
9150:   MatFactorRestoreSchurComplement - Restore the Schur complement matrix object obtained from a call to MatFactorGetSchurComplement

9152:    Logically Collective on Mat

9154:    Input Parameters:
9155: +  F - the factored matrix obtained by calling MatGetFactor()
9156: .  *S - location where the Schur complement is stored
9157: -  status - the status of the Schur complement matrix (see MatFactorSchurStatus)

9159:    Notes:

9161:    Level: advanced

9163:    References:

9165: .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorRestoreSchurComplement(), MatFactorCreateSchurComplement(), MatFactorSchurStatus
9166: @*/
9167: PetscErrorCode MatFactorRestoreSchurComplement(Mat F,Mat* S,MatFactorSchurStatus status)
9168: {

9173:   if (S) {
9175:     *S = NULL;
9176:   }
9177:   F->schur_status = status;
9178:   MatFactorUpdateSchurStatus_Private(F);
9179:   return(0);
9180: }

9182: /*@
9183:   MatFactorSolveSchurComplementTranspose - Solve the transpose of the Schur complement system computed during the factorization step

9185:    Logically Collective on Mat

9187:    Input Parameters:
9188: +  F - the factored matrix obtained by calling MatGetFactor()
9189: .  rhs - location where the right hand side of the Schur complement system is stored
9190: -  sol - location where the solution of the Schur complement system has to be returned

9192:    Notes:
9193:    The sizes of the vectors should match the size of the Schur complement

9195:    Must be called after MatFactorSetSchurIS()

9197:    Level: advanced

9199:    References:

9201: .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorSolveSchurComplement()
9202: @*/
9203: PetscErrorCode MatFactorSolveSchurComplementTranspose(Mat F, Vec rhs, Vec sol)
9204: {

9216:   MatFactorFactorizeSchurComplement(F);
9217:   switch (F->schur_status) {
9218:   case MAT_FACTOR_SCHUR_FACTORED:
9219:     MatSolveTranspose(F->schur,rhs,sol);
9220:     break;
9221:   case MAT_FACTOR_SCHUR_INVERTED:
9222:     MatMultTranspose(F->schur,rhs,sol);
9223:     break;
9224:   default:
9225:     SETERRQ1(PetscObjectComm((PetscObject)F),PETSC_ERR_SUP,"Unhandled MatFactorSchurStatus %D",F->schur_status);
9226:     break;
9227:   }
9228:   return(0);
9229: }

9231: /*@
9232:   MatFactorSolveSchurComplement - Solve the Schur complement system computed during the factorization step

9234:    Logically Collective on Mat

9236:    Input Parameters:
9237: +  F - the factored matrix obtained by calling MatGetFactor()
9238: .  rhs - location where the right hand side of the Schur complement system is stored
9239: -  sol - location where the solution of the Schur complement system has to be returned

9241:    Notes:
9242:    The sizes of the vectors should match the size of the Schur complement

9244:    Must be called after MatFactorSetSchurIS()

9246:    Level: advanced

9248:    References:

9250: .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorSolveSchurComplementTranspose()
9251: @*/
9252: PetscErrorCode MatFactorSolveSchurComplement(Mat F, Vec rhs, Vec sol)
9253: {

9265:   MatFactorFactorizeSchurComplement(F);
9266:   switch (F->schur_status) {
9267:   case MAT_FACTOR_SCHUR_FACTORED:
9268:     MatSolve(F->schur,rhs,sol);
9269:     break;
9270:   case MAT_FACTOR_SCHUR_INVERTED:
9271:     MatMult(F->schur,rhs,sol);
9272:     break;
9273:   default:
9274:     SETERRQ1(PetscObjectComm((PetscObject)F),PETSC_ERR_SUP,"Unhandled MatFactorSchurStatus %D",F->schur_status);
9275:     break;
9276:   }
9277:   return(0);
9278: }

9280: /*@
9281:   MatFactorInvertSchurComplement - Invert the Schur complement matrix computed during the factorization step

9283:    Logically Collective on Mat

9285:    Input Parameters:
9286: +  F - the factored matrix obtained by calling MatGetFactor()

9288:    Notes:
9289:     Must be called after MatFactorSetSchurIS().

9291:    Call MatFactorGetSchurComplement() or  MatFactorCreateSchurComplement() AFTER this call to actually compute the inverse and get access to it.

9293:    Level: advanced

9295:    References:

9297: .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorGetSchurComplement(), MatFactorCreateSchurComplement()
9298: @*/
9299: PetscErrorCode MatFactorInvertSchurComplement(Mat F)
9300: {

9306:   if (F->schur_status == MAT_FACTOR_SCHUR_INVERTED) return(0);
9307:   MatFactorFactorizeSchurComplement(F);
9308:   MatFactorInvertSchurComplement_Private(F);
9309:   F->schur_status = MAT_FACTOR_SCHUR_INVERTED;
9310:   return(0);
9311: }

9313: /*@
9314:   MatFactorFactorizeSchurComplement - Factorize the Schur complement matrix computed during the factorization step

9316:    Logically Collective on Mat

9318:    Input Parameters:
9319: +  F - the factored matrix obtained by calling MatGetFactor()

9321:    Notes:
9322:     Must be called after MatFactorSetSchurIS().

9324:    Level: advanced

9326:    References:

9328: .seealso: MatGetFactor(), MatFactorSetSchurIS(), MatFactorInvertSchurComplement()
9329: @*/
9330: PetscErrorCode MatFactorFactorizeSchurComplement(Mat F)
9331: {

9337:   if (F->schur_status == MAT_FACTOR_SCHUR_INVERTED || F->schur_status == MAT_FACTOR_SCHUR_FACTORED) return(0);
9338:   MatFactorFactorizeSchurComplement_Private(F);
9339:   F->schur_status = MAT_FACTOR_SCHUR_FACTORED;
9340:   return(0);
9341: }

9343: /*@
9344:    MatPtAP - Creates the matrix product C = P^T * A * P

9346:    Neighbor-wise Collective on Mat

9348:    Input Parameters:
9349: +  A - the matrix
9350: .  P - the projection matrix
9351: .  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
9352: -  fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(P)), use PETSC_DEFAULT if you do not have a good estimate
9353:           if the result is a dense matrix this is irrelevent

9355:    Output Parameters:
9356: .  C - the product matrix

9358:    Notes:
9359:    C will be created and must be destroyed by the user with MatDestroy().

9361:    This routine is currently only implemented for pairs of sequential dense matrices, AIJ matrices and classes
9362:    which inherit from AIJ.

9364:    Level: intermediate

9366: .seealso: MatPtAPSymbolic(), MatPtAPNumeric(), MatMatMult(), MatRARt()
9367: @*/
9368: PetscErrorCode MatPtAP(Mat A,Mat P,MatReuse scall,PetscReal fill,Mat *C)
9369: {
9371:   PetscErrorCode (*fA)(Mat,Mat,MatReuse,PetscReal,Mat*);
9372:   PetscErrorCode (*fP)(Mat,Mat,MatReuse,PetscReal,Mat*);
9373:   PetscErrorCode (*ptap)(Mat,Mat,MatReuse,PetscReal,Mat*)=NULL;
9374:   PetscBool      sametype;

9379:   MatCheckPreallocated(A,1);
9380:   if (scall == MAT_INPLACE_MATRIX) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Inplace product not supported");
9381:   if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
9382:   if (A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
9385:   MatCheckPreallocated(P,2);
9386:   if (!P->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
9387:   if (P->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");

9389:   if (A->rmap->N != A->cmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Matrix A must be square, %D != %D",A->rmap->N,A->cmap->N);
9390:   if (P->rmap->N != A->cmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",P->rmap->N,A->cmap->N);
9391:   if (fill == PETSC_DEFAULT || fill == PETSC_DECIDE) fill = 2.0;
9392:   if (fill < 1.0) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Expected fill=%g must be >= 1.0",(double)fill);

9394:   if (scall == MAT_REUSE_MATRIX) {

9398:     if (!(*C)->ops->ptapnumeric) SETERRQ(PetscObjectComm((PetscObject)C),PETSC_ERR_ARG_WRONGSTATE,"MatPtAPNumeric implementation is missing. You cannot use MAT_REUSE_MATRIX");
9399:     PetscLogEventBegin(MAT_PtAP,A,P,0,0);
9400:     PetscLogEventBegin(MAT_PtAPNumeric,A,P,0,0);
9401:     (*(*C)->ops->ptapnumeric)(A,P,*C);
9402:     PetscLogEventEnd(MAT_PtAPNumeric,A,P,0,0);
9403:     PetscLogEventEnd(MAT_PtAP,A,P,0,0);
9404:     return(0);
9405:   }

9407:   if (fill == PETSC_DEFAULT || fill == PETSC_DECIDE) fill = 2.0;
9408:   if (fill < 1.0) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Expected fill=%g must be >= 1.0",(double)fill);

9410:   fA = A->ops->ptap;
9411:   fP = P->ops->ptap;
9412:   PetscStrcmp(((PetscObject)A)->type_name,((PetscObject)P)->type_name,&sametype);
9413:   if (fP == fA && sametype) {
9414:     if (!fA) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"MatPtAP not supported for A of type %s",((PetscObject)A)->type_name);
9415:     ptap = fA;
9416:   } else {
9417:     /* dispatch based on the type of A and P from their PetscObject's PetscFunctionLists. */
9418:     char ptapname[256];
9419:     PetscStrncpy(ptapname,"MatPtAP_",sizeof(ptapname));
9420:     PetscStrlcat(ptapname,((PetscObject)A)->type_name,sizeof(ptapname));
9421:     PetscStrlcat(ptapname,"_",sizeof(ptapname));
9422:     PetscStrlcat(ptapname,((PetscObject)P)->type_name,sizeof(ptapname));
9423:     PetscStrlcat(ptapname,"_C",sizeof(ptapname)); /* e.g., ptapname = "MatPtAP_seqdense_seqaij_C" */
9424:     PetscObjectQueryFunction((PetscObject)P,ptapname,&ptap);
9425:     if (!ptap) SETERRQ3(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_INCOMP,"MatPtAP requires A, %s, to be compatible with P, %s (Misses composed function %s)",((PetscObject)A)->type_name,((PetscObject)P)->type_name,ptapname);
9426:   }

9428:   PetscLogEventBegin(MAT_PtAP,A,P,0,0);
9429:   (*ptap)(A,P,scall,fill,C);
9430:   PetscLogEventEnd(MAT_PtAP,A,P,0,0);
9431:   if (A->symmetric_set && A->symmetric) {
9432:     MatSetOption(*C,MAT_SYMMETRIC,PETSC_TRUE);
9433:   }
9434:   return(0);
9435: }

9437: /*@
9438:    MatPtAPNumeric - Computes the matrix product C = P^T * A * P

9440:    Neighbor-wise Collective on Mat

9442:    Input Parameters:
9443: +  A - the matrix
9444: -  P - the projection matrix

9446:    Output Parameters:
9447: .  C - the product matrix

9449:    Notes:
9450:    C must have been created by calling MatPtAPSymbolic and must be destroyed by
9451:    the user using MatDeatroy().

9453:    This routine is currently only implemented for pairs of AIJ matrices and classes
9454:    which inherit from AIJ.  C will be of type MATAIJ.

9456:    Level: intermediate

9458: .seealso: MatPtAP(), MatPtAPSymbolic(), MatMatMultNumeric()
9459: @*/
9460: PetscErrorCode MatPtAPNumeric(Mat A,Mat P,Mat C)
9461: {

9467:   if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
9468:   if (A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
9471:   MatCheckPreallocated(P,2);
9472:   if (!P->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
9473:   if (P->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
9476:   MatCheckPreallocated(C,3);
9477:   if (C->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
9478:   if (P->cmap->N!=C->rmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",P->cmap->N,C->rmap->N);
9479:   if (P->rmap->N!=A->cmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",P->rmap->N,A->cmap->N);
9480:   if (A->rmap->N!=A->cmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Matrix 'A' must be square, %D != %D",A->rmap->N,A->cmap->N);
9481:   if (P->cmap->N!=C->cmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",P->cmap->N,C->cmap->N);
9482:   MatCheckPreallocated(A,1);

9484:   if (!C->ops->ptapnumeric) SETERRQ(PetscObjectComm((PetscObject)C),PETSC_ERR_ARG_WRONGSTATE,"MatPtAPNumeric implementation is missing. You should call MatPtAPSymbolic first");
9485:   PetscLogEventBegin(MAT_PtAPNumeric,A,P,0,0);
9486:   (*C->ops->ptapnumeric)(A,P,C);
9487:   PetscLogEventEnd(MAT_PtAPNumeric,A,P,0,0);
9488:   return(0);
9489: }

9491: /*@
9492:    MatPtAPSymbolic - Creates the (i,j) structure of the matrix product C = P^T * A * P

9494:    Neighbor-wise Collective on Mat

9496:    Input Parameters:
9497: +  A - the matrix
9498: -  P - the projection matrix

9500:    Output Parameters:
9501: .  C - the (i,j) structure of the product matrix

9503:    Notes:
9504:    C will be created and must be destroyed by the user with MatDestroy().

9506:    This routine is currently only implemented for pairs of SeqAIJ matrices and classes
9507:    which inherit from SeqAIJ.  C will be of type MATSEQAIJ.  The product is computed using
9508:    this (i,j) structure by calling MatPtAPNumeric().

9510:    Level: intermediate

9512: .seealso: MatPtAP(), MatPtAPNumeric(), MatMatMultSymbolic()
9513: @*/
9514: PetscErrorCode MatPtAPSymbolic(Mat A,Mat P,PetscReal fill,Mat *C)
9515: {

9521:   if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
9522:   if (A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
9523:   if (fill <1.0) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Expected fill=%g must be >= 1.0",(double)fill);
9526:   MatCheckPreallocated(P,2);
9527:   if (!P->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
9528:   if (P->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");

9531:   if (P->rmap->N!=A->cmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",P->rmap->N,A->cmap->N);
9532:   if (A->rmap->N!=A->cmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Matrix 'A' must be square, %D != %D",A->rmap->N,A->cmap->N);
9533:   MatCheckPreallocated(A,1);

9535:   if (!A->ops->ptapsymbolic) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"MatType %s",((PetscObject)A)->type_name);
9536:   PetscLogEventBegin(MAT_PtAPSymbolic,A,P,0,0);
9537:   (*A->ops->ptapsymbolic)(A,P,fill,C);
9538:   PetscLogEventEnd(MAT_PtAPSymbolic,A,P,0,0);

9540:   /* MatSetBlockSize(*C,A->rmap->bs); NO! this is not always true -ma */
9541:   return(0);
9542: }

9544: /*@
9545:    MatRARt - Creates the matrix product C = R * A * R^T

9547:    Neighbor-wise Collective on Mat

9549:    Input Parameters:
9550: +  A - the matrix
9551: .  R - the projection matrix
9552: .  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
9553: -  fill - expected fill as ratio of nnz(C)/nnz(A), use PETSC_DEFAULT if you do not have a good estimate
9554:           if the result is a dense matrix this is irrelevent

9556:    Output Parameters:
9557: .  C - the product matrix

9559:    Notes:
9560:    C will be created and must be destroyed by the user with MatDestroy().

9562:    This routine is currently only implemented for pairs of AIJ matrices and classes
9563:    which inherit from AIJ. Due to PETSc sparse matrix block row distribution among processes,
9564:    parallel MatRARt is implemented via explicit transpose of R, which could be very expensive.
9565:    We recommend using MatPtAP().

9567:    Level: intermediate

9569: .seealso: MatRARtSymbolic(), MatRARtNumeric(), MatMatMult(), MatPtAP()
9570: @*/
9571: PetscErrorCode MatRARt(Mat A,Mat R,MatReuse scall,PetscReal fill,Mat *C)
9572: {

9578:   if (scall == MAT_INPLACE_MATRIX) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Inplace product not supported");
9579:   if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
9580:   if (A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
9583:   MatCheckPreallocated(R,2);
9584:   if (!R->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
9585:   if (R->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
9587:   if (R->cmap->N!=A->rmap->N) SETERRQ2(PetscObjectComm((PetscObject)R),PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",R->cmap->N,A->rmap->N);

9589:   if (fill == PETSC_DEFAULT || fill == PETSC_DECIDE) fill = 2.0;
9590:   if (fill < 1.0) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Expected fill=%g must be >= 1.0",(double)fill);
9591:   MatCheckPreallocated(A,1);

9593:   if (!A->ops->rart) {
9594:     Mat Rt;
9595:     MatTranspose(R,MAT_INITIAL_MATRIX,&Rt);
9596:     MatMatMatMult(R,A,Rt,scall,fill,C);
9597:     MatDestroy(&Rt);
9598:     return(0);
9599:   }
9600:   PetscLogEventBegin(MAT_RARt,A,R,0,0);
9601:   (*A->ops->rart)(A,R,scall,fill,C);
9602:   PetscLogEventEnd(MAT_RARt,A,R,0,0);
9603:   return(0);
9604: }

9606: /*@
9607:    MatRARtNumeric - Computes the matrix product C = R * A * R^T

9609:    Neighbor-wise Collective on Mat

9611:    Input Parameters:
9612: +  A - the matrix
9613: -  R - the projection matrix

9615:    Output Parameters:
9616: .  C - the product matrix

9618:    Notes:
9619:    C must have been created by calling MatRARtSymbolic and must be destroyed by
9620:    the user using MatDestroy().

9622:    This routine is currently only implemented for pairs of AIJ matrices and classes
9623:    which inherit from AIJ.  C will be of type MATAIJ.

9625:    Level: intermediate

9627: .seealso: MatRARt(), MatRARtSymbolic(), MatMatMultNumeric()
9628: @*/
9629: PetscErrorCode MatRARtNumeric(Mat A,Mat R,Mat C)
9630: {

9636:   if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
9637:   if (A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
9640:   MatCheckPreallocated(R,2);
9641:   if (!R->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
9642:   if (R->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
9645:   MatCheckPreallocated(C,3);
9646:   if (C->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
9647:   if (R->rmap->N!=C->rmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",R->rmap->N,C->rmap->N);
9648:   if (R->cmap->N!=A->rmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",R->cmap->N,A->rmap->N);
9649:   if (A->rmap->N!=A->cmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Matrix 'A' must be square, %D != %D",A->rmap->N,A->cmap->N);
9650:   if (R->rmap->N!=C->cmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",R->rmap->N,C->cmap->N);
9651:   MatCheckPreallocated(A,1);

9653:   PetscLogEventBegin(MAT_RARtNumeric,A,R,0,0);
9654:   (*A->ops->rartnumeric)(A,R,C);
9655:   PetscLogEventEnd(MAT_RARtNumeric,A,R,0,0);
9656:   return(0);
9657: }

9659: /*@
9660:    MatRARtSymbolic - Creates the (i,j) structure of the matrix product C = R * A * R^T

9662:    Neighbor-wise Collective on Mat

9664:    Input Parameters:
9665: +  A - the matrix
9666: -  R - the projection matrix

9668:    Output Parameters:
9669: .  C - the (i,j) structure of the product matrix

9671:    Notes:
9672:    C will be created and must be destroyed by the user with MatDestroy().

9674:    This routine is currently only implemented for pairs of SeqAIJ matrices and classes
9675:    which inherit from SeqAIJ.  C will be of type MATSEQAIJ.  The product is computed using
9676:    this (i,j) structure by calling MatRARtNumeric().

9678:    Level: intermediate

9680: .seealso: MatRARt(), MatRARtNumeric(), MatMatMultSymbolic()
9681: @*/
9682: PetscErrorCode MatRARtSymbolic(Mat A,Mat R,PetscReal fill,Mat *C)
9683: {

9689:   if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
9690:   if (A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
9691:   if (fill <1.0) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Expected fill=%g must be >= 1.0",(double)fill);
9694:   MatCheckPreallocated(R,2);
9695:   if (!R->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
9696:   if (R->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");

9699:   if (R->cmap->N!=A->rmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",R->cmap->N,A->rmap->N);
9700:   if (A->rmap->N!=A->cmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Matrix 'A' must be square, %D != %D",A->rmap->N,A->cmap->N);
9701:   MatCheckPreallocated(A,1);
9702:   PetscLogEventBegin(MAT_RARtSymbolic,A,R,0,0);
9703:   (*A->ops->rartsymbolic)(A,R,fill,C);
9704:   PetscLogEventEnd(MAT_RARtSymbolic,A,R,0,0);

9706:   MatSetBlockSizes(*C,PetscAbs(R->rmap->bs),PetscAbs(R->rmap->bs));
9707:   return(0);
9708: }

9710: /*@
9711:    MatMatMult - Performs Matrix-Matrix Multiplication C=A*B.

9713:    Neighbor-wise Collective on Mat

9715:    Input Parameters:
9716: +  A - the left matrix
9717: .  B - the right matrix
9718: .  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
9719: -  fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if you do not have a good estimate
9720:           if the result is a dense matrix this is irrelevent

9722:    Output Parameters:
9723: .  C - the product matrix

9725:    Notes:
9726:    Unless scall is MAT_REUSE_MATRIX C will be created.

9728:    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
9729:    call to this function with either MAT_INITIAL_MATRIX or MatMatMultSymbolic()

9731:    To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value
9732:    actually needed.

9734:    If you have many matrices with the same non-zero structure to multiply, you
9735:    should either
9736: $   1) use MAT_REUSE_MATRIX in all calls but the first or
9737: $   2) call MatMatMultSymbolic() once and then MatMatMultNumeric() for each product needed
9738:    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
9739:    with MAT_REUSE_MATRIX, rather than first having MatMatMult() create it for you. You can NEVER do this if the matrix C is sparse.

9741:    Level: intermediate

9743: .seealso: MatMatMultSymbolic(), MatMatMultNumeric(), MatTransposeMatMult(),  MatMatTransposeMult(), MatPtAP()
9744: @*/
9745: PetscErrorCode MatMatMult(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
9746: {
9748:   PetscErrorCode (*fA)(Mat,Mat,MatReuse,PetscReal,Mat*);
9749:   PetscErrorCode (*fB)(Mat,Mat,MatReuse,PetscReal,Mat*);
9750:   PetscErrorCode (*mult)(Mat,Mat,MatReuse,PetscReal,Mat*)=NULL;

9755:   MatCheckPreallocated(A,1);
9756:   if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
9757:   if (A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
9760:   MatCheckPreallocated(B,2);
9761:   if (!B->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
9762:   if (B->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
9764:   if (scall == MAT_INPLACE_MATRIX) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Inplace product not supported");
9765:   if (B->rmap->N!=A->cmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",B->rmap->N,A->cmap->N);
9766:   if (scall == MAT_REUSE_MATRIX) {
9769:     PetscLogEventBegin(MAT_MatMult,A,B,0,0);
9770:     PetscLogEventBegin(MAT_MatMultNumeric,A,B,0,0);
9771:     (*(*C)->ops->matmultnumeric)(A,B,*C);
9772:     PetscLogEventEnd(MAT_MatMultNumeric,A,B,0,0);
9773:     PetscLogEventEnd(MAT_MatMult,A,B,0,0);
9774:     return(0);
9775:   }
9776:   if (fill == PETSC_DEFAULT || fill == PETSC_DECIDE) fill = 2.0;
9777:   if (fill < 1.0) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Expected fill=%g must be >= 1.0",(double)fill);

9779:   fA = A->ops->matmult;
9780:   fB = B->ops->matmult;
9781:   if (fB == fA) {
9782:     if (!fB) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"MatMatMult not supported for B of type %s",((PetscObject)B)->type_name);
9783:     mult = fB;
9784:   } else {
9785:     /* dispatch based on the type of A and B from their PetscObject's PetscFunctionLists. */
9786:     char multname[256];
9787:     PetscStrncpy(multname,"MatMatMult_",sizeof(multname));
9788:     PetscStrlcat(multname,((PetscObject)A)->type_name,sizeof(multname));
9789:     PetscStrlcat(multname,"_",sizeof(multname));
9790:     PetscStrlcat(multname,((PetscObject)B)->type_name,sizeof(multname));
9791:     PetscStrlcat(multname,"_C",sizeof(multname)); /* e.g., multname = "MatMatMult_seqdense_seqaij_C" */
9792:     PetscObjectQueryFunction((PetscObject)B,multname,&mult);
9793:     if (!mult) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_INCOMP,"MatMatMult requires A, %s, to be compatible with B, %s",((PetscObject)A)->type_name,((PetscObject)B)->type_name);
9794:   }
9795:   PetscLogEventBegin(MAT_MatMult,A,B,0,0);
9796:   (*mult)(A,B,scall,fill,C);
9797:   PetscLogEventEnd(MAT_MatMult,A,B,0,0);
9798:   return(0);
9799: }

9801: /*@
9802:    MatMatMultSymbolic - Performs construction, preallocation, and computes the ij structure
9803:    of the matrix-matrix product C=A*B.  Call this routine before calling MatMatMultNumeric().

9805:    Neighbor-wise Collective on Mat

9807:    Input Parameters:
9808: +  A - the left matrix
9809: .  B - the right matrix
9810: -  fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if you do not have a good estimate,
9811:       if C is a dense matrix this is irrelevent

9813:    Output Parameters:
9814: .  C - the product matrix

9816:    Notes:
9817:    Unless scall is MAT_REUSE_MATRIX C will be created.

9819:    To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value
9820:    actually needed.

9822:    This routine is currently implemented for
9823:     - pairs of AIJ matrices and classes which inherit from AIJ, C will be of type AIJ
9824:     - pairs of AIJ (A) and Dense (B) matrix, C will be of type Dense.
9825:     - pairs of Dense (A) and AIJ (B) matrix, C will be of type Dense.

9827:    Level: intermediate

9829:    Developers Note: There are ways to estimate the number of nonzeros in the resulting product, see for example, http://arxiv.org/abs/1006.4173
9830:      We should incorporate them into PETSc.

9832: .seealso: MatMatMult(), MatMatMultNumeric()
9833: @*/
9834: PetscErrorCode MatMatMultSymbolic(Mat A,Mat B,PetscReal fill,Mat *C)
9835: {
9837:   PetscErrorCode (*Asymbolic)(Mat,Mat,PetscReal,Mat*);
9838:   PetscErrorCode (*Bsymbolic)(Mat,Mat,PetscReal,Mat*);
9839:   PetscErrorCode (*symbolic)(Mat,Mat,PetscReal,Mat*)=NULL;

9844:   if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
9845:   if (A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");

9849:   MatCheckPreallocated(B,2);
9850:   if (!B->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
9851:   if (B->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");

9854:   if (B->rmap->N!=A->cmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",B->rmap->N,A->cmap->N);
9855:   if (fill == PETSC_DEFAULT) fill = 2.0;
9856:   if (fill < 1.0) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Expected fill=%g must be > 1.0",(double)fill);
9857:   MatCheckPreallocated(A,1);

9859:   Asymbolic = A->ops->matmultsymbolic;
9860:   Bsymbolic = B->ops->matmultsymbolic;
9861:   if (Asymbolic == Bsymbolic) {
9862:     if (!Bsymbolic) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"C=A*B not implemented for B of type %s",((PetscObject)B)->type_name);
9863:     symbolic = Bsymbolic;
9864:   } else { /* dispatch based on the type of A and B */
9865:     char symbolicname[256];
9866:     PetscStrncpy(symbolicname,"MatMatMultSymbolic_",sizeof(symbolicname));
9867:     PetscStrlcat(symbolicname,((PetscObject)A)->type_name,sizeof(symbolicname));
9868:     PetscStrlcat(symbolicname,"_",sizeof(symbolicname));
9869:     PetscStrlcat(symbolicname,((PetscObject)B)->type_name,sizeof(symbolicname));
9870:     PetscStrlcat(symbolicname,"_C",sizeof(symbolicname));
9871:     PetscObjectQueryFunction((PetscObject)B,symbolicname,&symbolic);
9872:     if (!symbolic) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_INCOMP,"MatMatMultSymbolic requires A, %s, to be compatible with B, %s",((PetscObject)A)->type_name,((PetscObject)B)->type_name);
9873:   }
9874:   PetscLogEventBegin(MAT_MatMultSymbolic,A,B,0,0);
9875:   (*symbolic)(A,B,fill,C);
9876:   PetscLogEventEnd(MAT_MatMultSymbolic,A,B,0,0);
9877:   return(0);
9878: }

9880: /*@
9881:    MatMatMultNumeric - Performs the numeric matrix-matrix product.
9882:    Call this routine after first calling MatMatMultSymbolic().

9884:    Neighbor-wise Collective on Mat

9886:    Input Parameters:
9887: +  A - the left matrix
9888: -  B - the right matrix

9890:    Output Parameters:
9891: .  C - the product matrix, which was created by from MatMatMultSymbolic() or a call to MatMatMult().

9893:    Notes:
9894:    C must have been created with MatMatMultSymbolic().

9896:    This routine is currently implemented for
9897:     - pairs of AIJ matrices and classes which inherit from AIJ, C will be of type MATAIJ.
9898:     - pairs of AIJ (A) and Dense (B) matrix, C will be of type Dense.
9899:     - pairs of Dense (A) and AIJ (B) matrix, C will be of type Dense.

9901:    Level: intermediate

9903: .seealso: MatMatMult(), MatMatMultSymbolic()
9904: @*/
9905: PetscErrorCode MatMatMultNumeric(Mat A,Mat B,Mat C)
9906: {

9910:   MatMatMult(A,B,MAT_REUSE_MATRIX,0.0,&C);
9911:   return(0);
9912: }

9914: /*@
9915:    MatMatTransposeMult - Performs Matrix-Matrix Multiplication C=A*B^T.

9917:    Neighbor-wise Collective on Mat

9919:    Input Parameters:
9920: +  A - the left matrix
9921: .  B - the right matrix
9922: .  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
9923: -  fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if not known

9925:    Output Parameters:
9926: .  C - the product matrix

9928:    Notes:
9929:    C will be created if MAT_INITIAL_MATRIX and must be destroyed by the user with MatDestroy().

9931:    MAT_REUSE_MATRIX can only be used if the matrices A and B have the same nonzero pattern as in the previous call

9933:   To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value
9934:    actually needed.

9936:    This routine is currently only implemented for pairs of SeqAIJ matrices, for the SeqDense class,
9937:    and for pairs of MPIDense matrices.

9939:    Options Database Keys:
9940: +  -matmattransmult_mpidense_mpidense_via {allgatherv,cyclic} - Choose between algorthims for MPIDense matrices: the
9941:                                                                 first redundantly copies the transposed B matrix on each process and requiers O(log P) communication complexity;
9942:                                                                 the second never stores more than one portion of the B matrix at a time by requires O(P) communication complexity.

9944:    Level: intermediate

9946: .seealso: MatMatTransposeMultSymbolic(), MatMatTransposeMultNumeric(), MatMatMult(), MatTransposeMatMult() MatPtAP()
9947: @*/
9948: PetscErrorCode MatMatTransposeMult(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
9949: {
9951:   PetscErrorCode (*fA)(Mat,Mat,MatReuse,PetscReal,Mat*);
9952:   PetscErrorCode (*fB)(Mat,Mat,MatReuse,PetscReal,Mat*);

9957:   if (scall == MAT_INPLACE_MATRIX) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Inplace product not supported");
9958:   if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
9959:   if (A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
9962:   MatCheckPreallocated(B,2);
9963:   if (!B->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
9964:   if (B->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
9966:   if (B->cmap->N!=A->cmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, AN %D != BN %D",A->cmap->N,B->cmap->N);
9967:   if (fill == PETSC_DEFAULT || fill == PETSC_DECIDE) fill = 2.0;
9968:   if (fill < 1.0) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Expected fill=%g must be > 1.0",(double)fill);
9969:   MatCheckPreallocated(A,1);

9971:   fA = A->ops->mattransposemult;
9972:   if (!fA) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"MatMatTransposeMult not supported for A of type %s",((PetscObject)A)->type_name);
9973:   fB = B->ops->mattransposemult;
9974:   if (!fB) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"MatMatTransposeMult not supported for B of type %s",((PetscObject)B)->type_name);
9975:   if (fB!=fA) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_INCOMP,"MatMatTransposeMult requires A, %s, to be compatible with B, %s",((PetscObject)A)->type_name,((PetscObject)B)->type_name);

9977:   PetscLogEventBegin(MAT_MatTransposeMult,A,B,0,0);
9978:   if (scall == MAT_INITIAL_MATRIX) {
9979:     PetscLogEventBegin(MAT_MatTransposeMultSymbolic,A,B,0,0);
9980:     (*A->ops->mattransposemultsymbolic)(A,B,fill,C);
9981:     PetscLogEventEnd(MAT_MatTransposeMultSymbolic,A,B,0,0);
9982:   }
9983:   PetscLogEventBegin(MAT_MatTransposeMultNumeric,A,B,0,0);
9984:   (*A->ops->mattransposemultnumeric)(A,B,*C);
9985:   PetscLogEventEnd(MAT_MatTransposeMultNumeric,A,B,0,0);
9986:   PetscLogEventEnd(MAT_MatTransposeMult,A,B,0,0);
9987:   return(0);
9988: }

9990: /*@
9991:    MatTransposeMatMult - Performs Matrix-Matrix Multiplication C=A^T*B.

9993:    Neighbor-wise Collective on Mat

9995:    Input Parameters:
9996: +  A - the left matrix
9997: .  B - the right matrix
9998: .  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
9999: -  fill - expected fill as ratio of nnz(C)/(nnz(A) + nnz(B)), use PETSC_DEFAULT if not known

10001:    Output Parameters:
10002: .  C - the product matrix

10004:    Notes:
10005:    C will be created if MAT_INITIAL_MATRIX and must be destroyed by the user with MatDestroy().

10007:    MAT_REUSE_MATRIX can only be used if the matrices A and B have the same nonzero pattern as in the previous call

10009:   To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value
10010:    actually needed.

10012:    This routine is currently implemented for pairs of AIJ matrices and pairs of SeqDense matrices and classes
10013:    which inherit from SeqAIJ.  C will be of same type as the input matrices.

10015:    Level: intermediate

10017: .seealso: MatTransposeMatMultSymbolic(), MatTransposeMatMultNumeric(), MatMatMult(), MatMatTransposeMult(), MatPtAP()
10018: @*/
10019: PetscErrorCode MatTransposeMatMult(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
10020: {
10022:   PetscErrorCode (*fA)(Mat,Mat,MatReuse,PetscReal,Mat*);
10023:   PetscErrorCode (*fB)(Mat,Mat,MatReuse,PetscReal,Mat*);
10024:   PetscErrorCode (*transposematmult)(Mat,Mat,MatReuse,PetscReal,Mat*) = NULL;

10029:   if (scall == MAT_INPLACE_MATRIX) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Inplace product not supported");
10030:   if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
10031:   if (A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
10034:   MatCheckPreallocated(B,2);
10035:   if (!B->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
10036:   if (B->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
10038:   if (B->rmap->N!=A->rmap->N) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",B->rmap->N,A->rmap->N);
10039:   if (fill == PETSC_DEFAULT || fill == PETSC_DECIDE) fill = 2.0;
10040:   if (fill < 1.0) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Expected fill=%g must be > 1.0",(double)fill);
10041:   MatCheckPreallocated(A,1);

10043:   fA = A->ops->transposematmult;
10044:   fB = B->ops->transposematmult;
10045:   if (fB==fA) {
10046:     if (!fA) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"MatTransposeMatMult not supported for A of type %s",((PetscObject)A)->type_name);
10047:     transposematmult = fA;
10048:   } else {
10049:     /* dispatch based on the type of A and B from their PetscObject's PetscFunctionLists. */
10050:     char multname[256];
10051:     PetscStrncpy(multname,"MatTransposeMatMult_",sizeof(multname));
10052:     PetscStrlcat(multname,((PetscObject)A)->type_name,sizeof(multname));
10053:     PetscStrlcat(multname,"_",sizeof(multname));
10054:     PetscStrlcat(multname,((PetscObject)B)->type_name,sizeof(multname));
10055:     PetscStrlcat(multname,"_C",sizeof(multname)); /* e.g., multname = "MatMatMult_seqdense_seqaij_C" */
10056:     PetscObjectQueryFunction((PetscObject)B,multname,&transposematmult);
10057:     if (!transposematmult) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_INCOMP,"MatTransposeMatMult requires A, %s, to be compatible with B, %s",((PetscObject)A)->type_name,((PetscObject)B)->type_name);
10058:   }
10059:   PetscLogEventBegin(MAT_TransposeMatMult,A,B,0,0);
10060:   (*transposematmult)(A,B,scall,fill,C);
10061:   PetscLogEventEnd(MAT_TransposeMatMult,A,B,0,0);
10062:   return(0);
10063: }

10065: /*@
10066:    MatMatMatMult - Performs Matrix-Matrix-Matrix Multiplication D=A*B*C.

10068:    Neighbor-wise Collective on Mat

10070:    Input Parameters:
10071: +  A - the left matrix
10072: .  B - the middle matrix
10073: .  C - the right matrix
10074: .  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
10075: -  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
10076:           if the result is a dense matrix this is irrelevent

10078:    Output Parameters:
10079: .  D - the product matrix

10081:    Notes:
10082:    Unless scall is MAT_REUSE_MATRIX D will be created.

10084:    MAT_REUSE_MATRIX can only be used if the matrices A, B and C have the same nonzero pattern as in the previous call

10086:    To determine the correct fill value, run with -info and search for the string "Fill ratio" to see the value
10087:    actually needed.

10089:    If you have many matrices with the same non-zero structure to multiply, you
10090:    should use MAT_REUSE_MATRIX in all calls but the first or

10092:    Level: intermediate

10094: .seealso: MatMatMult, MatPtAP()
10095: @*/
10096: PetscErrorCode MatMatMatMult(Mat A,Mat B,Mat C,MatReuse scall,PetscReal fill,Mat *D)
10097: {
10099:   PetscErrorCode (*fA)(Mat,Mat,Mat,MatReuse,PetscReal,Mat*);
10100:   PetscErrorCode (*fB)(Mat,Mat,Mat,MatReuse,PetscReal,Mat*);
10101:   PetscErrorCode (*fC)(Mat,Mat,Mat,MatReuse,PetscReal,Mat*);
10102:   PetscErrorCode (*mult)(Mat,Mat,Mat,MatReuse,PetscReal,Mat*)=NULL;

10107:   MatCheckPreallocated(A,1);
10108:   if (scall == MAT_INPLACE_MATRIX) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Inplace product not supported");
10109:   if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
10110:   if (A->factortype) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
10113:   MatCheckPreallocated(B,2);
10114:   if (!B->assembled) SETERRQ(PetscObjectComm((PetscObject)B),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
10115:   if (B->factortype) SETERRQ(PetscObjectComm((PetscObject)B),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
10118:   MatCheckPreallocated(C,3);
10119:   if (!C->assembled) SETERRQ(PetscObjectComm((PetscObject)C),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
10120:   if (C->factortype) SETERRQ(PetscObjectComm((PetscObject)C),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
10121:   if (B->rmap->N!=A->cmap->N) SETERRQ2(PetscObjectComm((PetscObject)B),PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",B->rmap->N,A->cmap->N);
10122:   if (C->rmap->N!=B->cmap->N) SETERRQ2(PetscObjectComm((PetscObject)C),PETSC_ERR_ARG_SIZ,"Matrix dimensions are incompatible, %D != %D",C->rmap->N,B->cmap->N);
10123:   if (scall == MAT_REUSE_MATRIX) {
10126:     PetscLogEventBegin(MAT_MatMatMult,A,B,0,0);
10127:     (*(*D)->ops->matmatmult)(A,B,C,scall,fill,D);
10128:     PetscLogEventEnd(MAT_MatMatMult,A,B,0,0);
10129:     return(0);
10130:   }
10131:   if (fill == PETSC_DEFAULT || fill == PETSC_DECIDE) fill = 2.0;
10132:   if (fill < 1.0) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Expected fill=%g must be >= 1.0",(double)fill);

10134:   fA = A->ops->matmatmult;
10135:   fB = B->ops->matmatmult;
10136:   fC = C->ops->matmatmult;
10137:   if (fA == fB && fA == fC) {
10138:     if (!fA) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"MatMatMatMult not supported for A of type %s",((PetscObject)A)->type_name);
10139:     mult = fA;
10140:   } else {
10141:     /* dispatch based on the type of A, B and C from their PetscObject's PetscFunctionLists. */
10142:     char multname[256];
10143:     PetscStrncpy(multname,"MatMatMatMult_",sizeof(multname));
10144:     PetscStrlcat(multname,((PetscObject)A)->type_name,sizeof(multname));
10145:     PetscStrlcat(multname,"_",sizeof(multname));
10146:     PetscStrlcat(multname,((PetscObject)B)->type_name,sizeof(multname));
10147:     PetscStrlcat(multname,"_",sizeof(multname));
10148:     PetscStrlcat(multname,((PetscObject)C)->type_name,sizeof(multname));
10149:     PetscStrlcat(multname,"_C",sizeof(multname));
10150:     PetscObjectQueryFunction((PetscObject)B,multname,&mult);
10151:     if (!mult) SETERRQ3(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_INCOMP,"MatMatMatMult requires A, %s, to be compatible with B, %s, C, %s",((PetscObject)A)->type_name,((PetscObject)B)->type_name,((PetscObject)C)->type_name);
10152:   }
10153:   PetscLogEventBegin(MAT_MatMatMult,A,B,0,0);
10154:   (*mult)(A,B,C,scall,fill,D);
10155:   PetscLogEventEnd(MAT_MatMatMult,A,B,0,0);
10156:   return(0);
10157: }

10159: /*@
10160:    MatCreateRedundantMatrix - Create redundant matrices and put them into processors of subcommunicators.

10162:    Collective on Mat

10164:    Input Parameters:
10165: +  mat - the matrix
10166: .  nsubcomm - the number of subcommunicators (= number of redundant parallel or sequential matrices)
10167: .  subcomm - MPI communicator split from the communicator where mat resides in (or MPI_COMM_NULL if nsubcomm is used)
10168: -  reuse - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX

10170:    Output Parameter:
10171: .  matredundant - redundant matrix

10173:    Notes:
10174:    MAT_REUSE_MATRIX can only be used when the nonzero structure of the
10175:    original matrix has not changed from that last call to MatCreateRedundantMatrix().

10177:    This routine creates the duplicated matrices in subcommunicators; you should NOT create them before
10178:    calling it.

10180:    Level: advanced

10182:    Concepts: subcommunicator
10183:    Concepts: duplicate matrix

10185: .seealso: MatDestroy()
10186: @*/
10187: PetscErrorCode MatCreateRedundantMatrix(Mat mat,PetscInt nsubcomm,MPI_Comm subcomm,MatReuse reuse,Mat *matredundant)
10188: {
10190:   MPI_Comm       comm;
10191:   PetscMPIInt    size;
10192:   PetscInt       mloc_sub,nloc_sub,rstart,rend,M=mat->rmap->N,N=mat->cmap->N,bs=mat->rmap->bs;
10193:   Mat_Redundant  *redund=NULL;
10194:   PetscSubcomm   psubcomm=NULL;
10195:   MPI_Comm       subcomm_in=subcomm;
10196:   Mat            *matseq;
10197:   IS             isrow,iscol;
10198:   PetscBool      newsubcomm=PETSC_FALSE;

10202:   if (nsubcomm && reuse == MAT_REUSE_MATRIX) {
10205:   }

10207:   MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);
10208:   if (size == 1 || nsubcomm == 1) {
10209:     if (reuse == MAT_INITIAL_MATRIX) {
10210:       MatDuplicate(mat,MAT_COPY_VALUES,matredundant);
10211:     } else {
10212:       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");
10213:       MatCopy(mat,*matredundant,SAME_NONZERO_PATTERN);
10214:     }
10215:     return(0);
10216:   }

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

10222:   PetscLogEventBegin(MAT_RedundantMat,mat,0,0,0);
10223:   if (subcomm_in == MPI_COMM_NULL && reuse == MAT_INITIAL_MATRIX) { /* get subcomm if user does not provide subcomm */
10224:     /* create psubcomm, then get subcomm */
10225:     PetscObjectGetComm((PetscObject)mat,&comm);
10226:     MPI_Comm_size(comm,&size);
10227:     if (nsubcomm < 1 || nsubcomm > size) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"nsubcomm must between 1 and %D",size);

10229:     PetscSubcommCreate(comm,&psubcomm);
10230:     PetscSubcommSetNumber(psubcomm,nsubcomm);
10231:     PetscSubcommSetType(psubcomm,PETSC_SUBCOMM_CONTIGUOUS);
10232:     PetscSubcommSetFromOptions(psubcomm);
10233:     PetscCommDuplicate(PetscSubcommChild(psubcomm),&subcomm,NULL);
10234:     newsubcomm = PETSC_TRUE;
10235:     PetscSubcommDestroy(&psubcomm);
10236:   }

10238:   /* get isrow, iscol and a local sequential matrix matseq[0] */
10239:   if (reuse == MAT_INITIAL_MATRIX) {
10240:     mloc_sub = PETSC_DECIDE;
10241:     nloc_sub = PETSC_DECIDE;
10242:     if (bs < 1) {
10243:       PetscSplitOwnership(subcomm,&mloc_sub,&M);
10244:       PetscSplitOwnership(subcomm,&nloc_sub,&N);
10245:     } else {
10246:       PetscSplitOwnershipBlock(subcomm,bs,&mloc_sub,&M);
10247:       PetscSplitOwnershipBlock(subcomm,bs,&nloc_sub,&N);
10248:     }
10249:     MPI_Scan(&mloc_sub,&rend,1,MPIU_INT,MPI_SUM,subcomm);
10250:     rstart = rend - mloc_sub;
10251:     ISCreateStride(PETSC_COMM_SELF,mloc_sub,rstart,1,&isrow);
10252:     ISCreateStride(PETSC_COMM_SELF,N,0,1,&iscol);
10253:   } else { /* reuse == MAT_REUSE_MATRIX */
10254:     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");
10255:     /* retrieve subcomm */
10256:     PetscObjectGetComm((PetscObject)(*matredundant),&subcomm);
10257:     redund = (*matredundant)->redundant;
10258:     isrow  = redund->isrow;
10259:     iscol  = redund->iscol;
10260:     matseq = redund->matseq;
10261:   }
10262:   MatCreateSubMatrices(mat,1,&isrow,&iscol,reuse,&matseq);

10264:   /* get matredundant over subcomm */
10265:   if (reuse == MAT_INITIAL_MATRIX) {
10266:     MatCreateMPIMatConcatenateSeqMat(subcomm,matseq[0],nloc_sub,reuse,matredundant);

10268:     /* create a supporting struct and attach it to C for reuse */
10269:     PetscNewLog(*matredundant,&redund);
10270:     (*matredundant)->redundant = redund;
10271:     redund->isrow              = isrow;
10272:     redund->iscol              = iscol;
10273:     redund->matseq             = matseq;
10274:     if (newsubcomm) {
10275:       redund->subcomm          = subcomm;
10276:     } else {
10277:       redund->subcomm          = MPI_COMM_NULL;
10278:     }
10279:   } else {
10280:     MatCreateMPIMatConcatenateSeqMat(subcomm,matseq[0],PETSC_DECIDE,reuse,matredundant);
10281:   }
10282:   PetscLogEventEnd(MAT_RedundantMat,mat,0,0,0);
10283:   return(0);
10284: }

10286: /*@C
10287:    MatGetMultiProcBlock - Create multiple [bjacobi] 'parallel submatrices' from
10288:    a given 'mat' object. Each submatrix can span multiple procs.

10290:    Collective on Mat

10292:    Input Parameters:
10293: +  mat - the matrix
10294: .  subcomm - the subcommunicator obtained by com_split(comm)
10295: -  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX

10297:    Output Parameter:
10298: .  subMat - 'parallel submatrices each spans a given subcomm

10300:   Notes:
10301:   The submatrix partition across processors is dictated by 'subComm' a
10302:   communicator obtained by com_split(comm). The comm_split
10303:   is not restriced to be grouped with consecutive original ranks.

10305:   Due the comm_split() usage, the parallel layout of the submatrices
10306:   map directly to the layout of the original matrix [wrt the local
10307:   row,col partitioning]. So the original 'DiagonalMat' naturally maps
10308:   into the 'DiagonalMat' of the subMat, hence it is used directly from
10309:   the subMat. However the offDiagMat looses some columns - and this is
10310:   reconstructed with MatSetValues()

10312:   Level: advanced

10314:   Concepts: subcommunicator
10315:   Concepts: submatrices

10317: .seealso: MatCreateSubMatrices()
10318: @*/
10319: PetscErrorCode   MatGetMultiProcBlock(Mat mat, MPI_Comm subComm, MatReuse scall,Mat *subMat)
10320: {
10322:   PetscMPIInt    commsize,subCommSize;

10325:   MPI_Comm_size(PetscObjectComm((PetscObject)mat),&commsize);
10326:   MPI_Comm_size(subComm,&subCommSize);
10327:   if (subCommSize > commsize) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"CommSize %D < SubCommZize %D",commsize,subCommSize);

10329:   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");
10330:   PetscLogEventBegin(MAT_GetMultiProcBlock,mat,0,0,0);
10331:   (*mat->ops->getmultiprocblock)(mat,subComm,scall,subMat);
10332:   PetscLogEventEnd(MAT_GetMultiProcBlock,mat,0,0,0);
10333:   return(0);
10334: }

10336: /*@
10337:    MatGetLocalSubMatrix - Gets a reference to a submatrix specified in local numbering

10339:    Not Collective

10341:    Input Arguments:
10342:    mat - matrix to extract local submatrix from
10343:    isrow - local row indices for submatrix
10344:    iscol - local column indices for submatrix

10346:    Output Arguments:
10347:    submat - the submatrix

10349:    Level: intermediate

10351:    Notes:
10352:    The submat should be returned with MatRestoreLocalSubMatrix().

10354:    Depending on the format of mat, the returned submat may not implement MatMult().  Its communicator may be
10355:    the same as mat, it may be PETSC_COMM_SELF, or some other subcomm of mat's.

10357:    The submat always implements MatSetValuesLocal().  If isrow and iscol have the same block size, then
10358:    MatSetValuesBlockedLocal() will also be implemented.

10360:    The mat must have had a ISLocalToGlobalMapping provided to it with MatSetLocalToGlobalMapping(). Note that
10361:    matrices obtained with DMCreateMatrix() generally already have the local to global mapping provided.

10363: .seealso: MatRestoreLocalSubMatrix(), MatCreateLocalRef(), MatSetLocalToGlobalMapping()
10364: @*/
10365: PetscErrorCode MatGetLocalSubMatrix(Mat mat,IS isrow,IS iscol,Mat *submat)
10366: {

10375:   if (!mat->rmap->mapping) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Matrix must have local to global mapping provided before this call");

10377:   if (mat->ops->getlocalsubmatrix) {
10378:     (*mat->ops->getlocalsubmatrix)(mat,isrow,iscol,submat);
10379:   } else {
10380:     MatCreateLocalRef(mat,isrow,iscol,submat);
10381:   }
10382:   return(0);
10383: }

10385: /*@
10386:    MatRestoreLocalSubMatrix - Restores a reference to a submatrix specified in local numbering

10388:    Not Collective

10390:    Input Arguments:
10391:    mat - matrix to extract local submatrix from
10392:    isrow - local row indices for submatrix
10393:    iscol - local column indices for submatrix
10394:    submat - the submatrix

10396:    Level: intermediate

10398: .seealso: MatGetLocalSubMatrix()
10399: @*/
10400: PetscErrorCode MatRestoreLocalSubMatrix(Mat mat,IS isrow,IS iscol,Mat *submat)
10401: {

10410:   if (*submat) {
10412:   }

10414:   if (mat->ops->restorelocalsubmatrix) {
10415:     (*mat->ops->restorelocalsubmatrix)(mat,isrow,iscol,submat);
10416:   } else {
10417:     MatDestroy(submat);
10418:   }
10419:   *submat = NULL;
10420:   return(0);
10421: }

10423: /* --------------------------------------------------------*/
10424: /*@
10425:    MatFindZeroDiagonals - Finds all the rows of a matrix that have zero or no diagonal entry in the matrix

10427:    Collective on Mat

10429:    Input Parameter:
10430: .  mat - the matrix

10432:    Output Parameter:
10433: .  is - if any rows have zero diagonals this contains the list of them

10435:    Level: developer

10437:    Concepts: matrix-vector product

10439: .seealso: MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd()
10440: @*/
10441: PetscErrorCode MatFindZeroDiagonals(Mat mat,IS *is)
10442: {

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

10451:   if (!mat->ops->findzerodiagonals) {
10452:     Vec                diag;
10453:     const PetscScalar *a;
10454:     PetscInt          *rows;
10455:     PetscInt           rStart, rEnd, r, nrow = 0;

10457:     MatCreateVecs(mat, &diag, NULL);
10458:     MatGetDiagonal(mat, diag);
10459:     MatGetOwnershipRange(mat, &rStart, &rEnd);
10460:     VecGetArrayRead(diag, &a);
10461:     for (r = 0; r < rEnd-rStart; ++r) if (a[r] == 0.0) ++nrow;
10462:     PetscMalloc1(nrow, &rows);
10463:     nrow = 0;
10464:     for (r = 0; r < rEnd-rStart; ++r) if (a[r] == 0.0) rows[nrow++] = r+rStart;
10465:     VecRestoreArrayRead(diag, &a);
10466:     VecDestroy(&diag);
10467:     ISCreateGeneral(PetscObjectComm((PetscObject) mat), nrow, rows, PETSC_OWN_POINTER, is);
10468:   } else {
10469:     (*mat->ops->findzerodiagonals)(mat, is);
10470:   }
10471:   return(0);
10472: }

10474: /*@
10475:    MatFindOffBlockDiagonalEntries - Finds all the rows of a matrix that have entries outside of the main diagonal block (defined by the matrix block size)

10477:    Collective on Mat

10479:    Input Parameter:
10480: .  mat - the matrix

10482:    Output Parameter:
10483: .  is - contains the list of rows with off block diagonal entries

10485:    Level: developer

10487:    Concepts: matrix-vector product

10489: .seealso: MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd()
10490: @*/
10491: PetscErrorCode MatFindOffBlockDiagonalEntries(Mat mat,IS *is)
10492: {

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

10501:   if (!mat->ops->findoffblockdiagonalentries) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"This matrix type does not have a find off block diagonal entries defined");
10502:   (*mat->ops->findoffblockdiagonalentries)(mat,is);
10503:   return(0);
10504: }

10506: /*@C
10507:   MatInvertBlockDiagonal - Inverts the block diagonal entries.

10509:   Collective on Mat

10511:   Input Parameters:
10512: . mat - the matrix

10514:   Output Parameters:
10515: . values - the block inverses in column major order (FORTRAN-like)

10517:    Note:
10518:    This routine is not available from Fortran.

10520:   Level: advanced

10522: .seealso: MatInvertBockDiagonalMat
10523: @*/
10524: PetscErrorCode MatInvertBlockDiagonal(Mat mat,const PetscScalar **values)
10525: {

10530:   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
10531:   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
10532:   if (!mat->ops->invertblockdiagonal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not supported");
10533:   (*mat->ops->invertblockdiagonal)(mat,values);
10534:   return(0);
10535: }

10537: /*@C
10538:   MatInvertVariableBlockDiagonal - Inverts the block diagonal entries.

10540:   Collective on Mat

10542:   Input Parameters:
10543: + mat - the matrix
10544: . nblocks - the number of blocks
10545: - bsizes - the size of each block

10547:   Output Parameters:
10548: . values - the block inverses in column major order (FORTRAN-like)

10550:    Note:
10551:    This routine is not available from Fortran.

10553:   Level: advanced

10555: .seealso: MatInvertBockDiagonal()
10556: @*/
10557: PetscErrorCode MatInvertVariableBlockDiagonal(Mat mat,PetscInt nblocks,const PetscInt *bsizes,PetscScalar *values)
10558: {

10563:   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
10564:   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
10565:   if (!mat->ops->invertvariableblockdiagonal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not supported");
10566:   (*mat->ops->invertvariableblockdiagonal)(mat,nblocks,bsizes,values);
10567:   return(0);
10568: }

10570: /*@
10571:   MatInvertBlockDiagonalMat - set matrix C to be the inverted block diagonal of matrix A

10573:   Collective on Mat

10575:   Input Parameters:
10576: . A - the matrix

10578:   Output Parameters:
10579: . C - matrix with inverted block diagonal of A.  This matrix should be created and may have its type set.

10581:   Notes: the blocksize of the matrix is used to determine the blocks on the diagonal of C

10583:   Level: advanced

10585: .seealso: MatInvertBockDiagonal()
10586: @*/
10587: PetscErrorCode MatInvertBlockDiagonalMat(Mat A,Mat C)
10588: {
10589:   PetscErrorCode     ierr;
10590:   const PetscScalar *vals;
10591:   PetscInt          *dnnz;
10592:   PetscInt           M,N,m,n,rstart,rend,bs,i,j;

10595:   MatInvertBlockDiagonal(A,&vals);
10596:   MatGetBlockSize(A,&bs);
10597:   MatGetSize(A,&M,&N);
10598:   MatGetLocalSize(A,&m,&n);
10599:   MatSetSizes(C,m,n,M,N);
10600:   MatSetBlockSize(C,bs);
10601:   PetscMalloc1(m/bs,&dnnz);
10602:   for (j = 0; j < m/bs; j++) dnnz[j] = 1;
10603:   MatXAIJSetPreallocation(C,bs,dnnz,NULL,NULL,NULL);
10604:   PetscFree(dnnz);
10605:   MatGetOwnershipRange(C,&rstart,&rend);
10606:   MatSetOption(C,MAT_ROW_ORIENTED,PETSC_FALSE);
10607:   for (i = rstart/bs; i < rend/bs; i++) {
10608:     MatSetValuesBlocked(C,1,&i,1,&i,&vals[(i-rstart/bs)*bs*bs],INSERT_VALUES);
10609:   }
10610:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
10611:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
10612:   MatSetOption(C,MAT_ROW_ORIENTED,PETSC_TRUE);
10613:   return(0);
10614: }

10616: /*@C
10617:     MatTransposeColoringDestroy - Destroys a coloring context for matrix product C=A*B^T that was created
10618:     via MatTransposeColoringCreate().

10620:     Collective on MatTransposeColoring

10622:     Input Parameter:
10623: .   c - coloring context

10625:     Level: intermediate

10627: .seealso: MatTransposeColoringCreate()
10628: @*/
10629: PetscErrorCode MatTransposeColoringDestroy(MatTransposeColoring *c)
10630: {
10631:   PetscErrorCode       ierr;
10632:   MatTransposeColoring matcolor=*c;

10635:   if (!matcolor) return(0);
10636:   if (--((PetscObject)matcolor)->refct > 0) {matcolor = 0; return(0);}

10638:   PetscFree3(matcolor->ncolumns,matcolor->nrows,matcolor->colorforrow);
10639:   PetscFree(matcolor->rows);
10640:   PetscFree(matcolor->den2sp);
10641:   PetscFree(matcolor->colorforcol);
10642:   PetscFree(matcolor->columns);
10643:   if (matcolor->brows>0) {
10644:     PetscFree(matcolor->lstart);
10645:   }
10646:   PetscHeaderDestroy(c);
10647:   return(0);
10648: }

10650: /*@C
10651:     MatTransColoringApplySpToDen - Given a symbolic matrix product C=A*B^T for which
10652:     a MatTransposeColoring context has been created, computes a dense B^T by Apply
10653:     MatTransposeColoring to sparse B.

10655:     Collective on MatTransposeColoring

10657:     Input Parameters:
10658: +   B - sparse matrix B
10659: .   Btdense - symbolic dense matrix B^T
10660: -   coloring - coloring context created with MatTransposeColoringCreate()

10662:     Output Parameter:
10663: .   Btdense - dense matrix B^T

10665:     Level: advanced

10667:      Notes:
10668:     These are used internally for some implementations of MatRARt()

10670: .seealso: MatTransposeColoringCreate(), MatTransposeColoringDestroy(), MatTransColoringApplyDenToSp()

10672: .keywords: coloring
10673: @*/
10674: PetscErrorCode MatTransColoringApplySpToDen(MatTransposeColoring coloring,Mat B,Mat Btdense)
10675: {


10683:   if (!B->ops->transcoloringapplysptoden) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not supported for this matrix type %s",((PetscObject)B)->type_name);
10684:   (B->ops->transcoloringapplysptoden)(coloring,B,Btdense);
10685:   return(0);
10686: }

10688: /*@C
10689:     MatTransColoringApplyDenToSp - Given a symbolic matrix product Csp=A*B^T for which
10690:     a MatTransposeColoring context has been created and a dense matrix Cden=A*Btdense
10691:     in which Btdens is obtained from MatTransColoringApplySpToDen(), recover sparse matrix
10692:     Csp from Cden.

10694:     Collective on MatTransposeColoring

10696:     Input Parameters:
10697: +   coloring - coloring context created with MatTransposeColoringCreate()
10698: -   Cden - matrix product of a sparse matrix and a dense matrix Btdense

10700:     Output Parameter:
10701: .   Csp - sparse matrix

10703:     Level: advanced

10705:      Notes:
10706:     These are used internally for some implementations of MatRARt()

10708: .seealso: MatTransposeColoringCreate(), MatTransposeColoringDestroy(), MatTransColoringApplySpToDen()

10710: .keywords: coloring
10711: @*/
10712: PetscErrorCode MatTransColoringApplyDenToSp(MatTransposeColoring matcoloring,Mat Cden,Mat Csp)
10713: {


10721:   if (!Csp->ops->transcoloringapplydentosp) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not supported for this matrix type %s",((PetscObject)Csp)->type_name);
10722:   (Csp->ops->transcoloringapplydentosp)(matcoloring,Cden,Csp);
10723:   return(0);
10724: }

10726: /*@C
10727:    MatTransposeColoringCreate - Creates a matrix coloring context for matrix product C=A*B^T.

10729:    Collective on Mat

10731:    Input Parameters:
10732: +  mat - the matrix product C
10733: -  iscoloring - the coloring of the matrix; usually obtained with MatColoringCreate() or DMCreateColoring()

10735:     Output Parameter:
10736: .   color - the new coloring context

10738:     Level: intermediate

10740: .seealso: MatTransposeColoringDestroy(),  MatTransColoringApplySpToDen(),
10741:            MatTransColoringApplyDenToSp()
10742: @*/
10743: PetscErrorCode MatTransposeColoringCreate(Mat mat,ISColoring iscoloring,MatTransposeColoring *color)
10744: {
10745:   MatTransposeColoring c;
10746:   MPI_Comm             comm;
10747:   PetscErrorCode       ierr;

10750:   PetscLogEventBegin(MAT_TransposeColoringCreate,mat,0,0,0);
10751:   PetscObjectGetComm((PetscObject)mat,&comm);
10752:   PetscHeaderCreate(c,MAT_TRANSPOSECOLORING_CLASSID,"MatTransposeColoring","Matrix product C=A*B^T via coloring","Mat",comm,MatTransposeColoringDestroy,NULL);

10754:   c->ctype = iscoloring->ctype;
10755:   if (mat->ops->transposecoloringcreate) {
10756:     (*mat->ops->transposecoloringcreate)(mat,iscoloring,c);
10757:   } else SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Code not yet written for this matrix type");

10759:   *color = c;
10760:   PetscLogEventEnd(MAT_TransposeColoringCreate,mat,0,0,0);
10761:   return(0);
10762: }

10764: /*@
10765:       MatGetNonzeroState - Returns a 64 bit integer representing the current state of nonzeros in the matrix. If the
10766:         matrix has had no new nonzero locations added to the matrix since the previous call then the value will be the
10767:         same, otherwise it will be larger

10769:      Not Collective

10771:   Input Parameter:
10772: .    A  - the matrix

10774:   Output Parameter:
10775: .    state - the current state

10777:   Notes:
10778:     You can only compare states from two different calls to the SAME matrix, you cannot compare calls between
10779:          different matrices

10781:   Level: intermediate

10783: @*/
10784: PetscErrorCode MatGetNonzeroState(Mat mat,PetscObjectState *state)
10785: {
10788:   *state = mat->nonzerostate;
10789:   return(0);
10790: }

10792: /*@
10793:       MatCreateMPIMatConcatenateSeqMat - Creates a single large PETSc matrix by concatenating sequential
10794:                  matrices from each processor

10796:     Collective on MPI_Comm

10798:    Input Parameters:
10799: +    comm - the communicators the parallel matrix will live on
10800: .    seqmat - the input sequential matrices
10801: .    n - number of local columns (or PETSC_DECIDE)
10802: -    reuse - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX

10804:    Output Parameter:
10805: .    mpimat - the parallel matrix generated

10807:     Level: advanced

10809:    Notes:
10810:     The number of columns of the matrix in EACH processor MUST be the same.

10812: @*/
10813: PetscErrorCode MatCreateMPIMatConcatenateSeqMat(MPI_Comm comm,Mat seqmat,PetscInt n,MatReuse reuse,Mat *mpimat)
10814: {

10818:   if (!seqmat->ops->creatempimatconcatenateseqmat) SETERRQ1(PetscObjectComm((PetscObject)seqmat),PETSC_ERR_SUP,"Mat type %s",((PetscObject)seqmat)->type_name);
10819:   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");

10821:   PetscLogEventBegin(MAT_Merge,seqmat,0,0,0);
10822:   (*seqmat->ops->creatempimatconcatenateseqmat)(comm,seqmat,n,reuse,mpimat);
10823:   PetscLogEventEnd(MAT_Merge,seqmat,0,0,0);
10824:   return(0);
10825: }

10827: /*@
10828:      MatSubdomainsCreateCoalesce - Creates index subdomains by coalescing adjacent
10829:                  ranks' ownership ranges.

10831:     Collective on A

10833:    Input Parameters:
10834: +    A   - the matrix to create subdomains from
10835: -    N   - requested number of subdomains


10838:    Output Parameters:
10839: +    n   - number of subdomains resulting on this rank
10840: -    iss - IS list with indices of subdomains on this rank

10842:     Level: advanced

10844:     Notes:
10845:     number of subdomains must be smaller than the communicator size
10846: @*/
10847: PetscErrorCode MatSubdomainsCreateCoalesce(Mat A,PetscInt N,PetscInt *n,IS *iss[])
10848: {
10849:   MPI_Comm        comm,subcomm;
10850:   PetscMPIInt     size,rank,color;
10851:   PetscInt        rstart,rend,k;
10852:   PetscErrorCode  ierr;

10855:   PetscObjectGetComm((PetscObject)A,&comm);
10856:   MPI_Comm_size(comm,&size);
10857:   MPI_Comm_rank(comm,&rank);
10858:   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);
10859:   *n = 1;
10860:   k = ((PetscInt)size)/N + ((PetscInt)size%N>0); /* There are up to k ranks to a color */
10861:   color = rank/k;
10862:   MPI_Comm_split(comm,color,rank,&subcomm);
10863:   PetscMalloc1(1,iss);
10864:   MatGetOwnershipRange(A,&rstart,&rend);
10865:   ISCreateStride(subcomm,rend-rstart,rstart,1,iss[0]);
10866:   MPI_Comm_free(&subcomm);
10867:   return(0);
10868: }

10870: /*@
10871:    MatGalerkin - Constructs the coarse grid problem via Galerkin projection.

10873:    If the interpolation and restriction operators are the same, uses MatPtAP.
10874:    If they are not the same, use MatMatMatMult.

10876:    Once the coarse grid problem is constructed, correct for interpolation operators
10877:    that are not of full rank, which can legitimately happen in the case of non-nested
10878:    geometric multigrid.

10880:    Input Parameters:
10881: +  restrct - restriction operator
10882: .  dA - fine grid matrix
10883: .  interpolate - interpolation operator
10884: .  reuse - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
10885: -  fill - expected fill, use PETSC_DEFAULT if you do not have a good estimate

10887:    Output Parameters:
10888: .  A - the Galerkin coarse matrix

10890:    Options Database Key:
10891: .  -pc_mg_galerkin <both,pmat,mat,none>

10893:    Level: developer

10895: .keywords: MG, multigrid, Galerkin

10897: .seealso: MatPtAP(), MatMatMatMult()
10898: @*/
10899: PetscErrorCode  MatGalerkin(Mat restrct, Mat dA, Mat interpolate, MatReuse reuse, PetscReal fill, Mat *A)
10900: {
10902:   IS             zerorows;
10903:   Vec            diag;

10906:   if (reuse == MAT_INPLACE_MATRIX) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Inplace product not supported");
10907:   /* Construct the coarse grid matrix */
10908:   if (interpolate == restrct) {
10909:     MatPtAP(dA,interpolate,reuse,fill,A);
10910:   } else {
10911:     MatMatMatMult(restrct,dA,interpolate,reuse,fill,A);
10912:   }

10914:   /* If the interpolation matrix is not of full rank, A will have zero rows.
10915:      This can legitimately happen in the case of non-nested geometric multigrid.
10916:      In that event, we set the rows of the matrix to the rows of the identity,
10917:      ignoring the equations (as the RHS will also be zero). */

10919:   MatFindZeroRows(*A, &zerorows);

10921:   if (zerorows != NULL) { /* if there are any zero rows */
10922:     MatCreateVecs(*A, &diag, NULL);
10923:     MatGetDiagonal(*A, diag);
10924:     VecISSet(diag, zerorows, 1.0);
10925:     MatDiagonalSet(*A, diag, INSERT_VALUES);
10926:     VecDestroy(&diag);
10927:     ISDestroy(&zerorows);
10928:   }
10929:   return(0);
10930: }

10932: /*@C
10933:     MatSetOperation - Allows user to set a matrix operation for any matrix type

10935:    Logically Collective on Mat

10937:     Input Parameters:
10938: +   mat - the matrix
10939: .   op - the name of the operation
10940: -   f - the function that provides the operation

10942:    Level: developer

10944:     Usage:
10945: $      extern PetscErrorCode usermult(Mat,Vec,Vec);
10946: $      MatCreateXXX(comm,...&A);
10947: $      MatSetOperation(A,MATOP_MULT,(void(*)(void))usermult);

10949:     Notes:
10950:     See the file include/petscmat.h for a complete list of matrix
10951:     operations, which all have the form MATOP_<OPERATION>, where
10952:     <OPERATION> is the name (in all capital letters) of the
10953:     user interface routine (e.g., MatMult() -> MATOP_MULT).

10955:     All user-provided functions (except for MATOP_DESTROY) should have the same calling
10956:     sequence as the usual matrix interface routines, since they
10957:     are intended to be accessed via the usual matrix interface
10958:     routines, e.g.,
10959: $       MatMult(Mat,Vec,Vec) -> usermult(Mat,Vec,Vec)

10961:     In particular each function MUST return an error code of 0 on success and
10962:     nonzero on failure.

10964:     This routine is distinct from MatShellSetOperation() in that it can be called on any matrix type.

10966: .keywords: matrix, set, operation

10968: .seealso: MatGetOperation(), MatCreateShell(), MatShellSetContext(), MatShellSetOperation()
10969: @*/
10970: PetscErrorCode MatSetOperation(Mat mat,MatOperation op,void (*f)(void))
10971: {
10974:   if (op == MATOP_VIEW && !mat->ops->viewnative && f != (void (*)(void))(mat->ops->view)) {
10975:     mat->ops->viewnative = mat->ops->view;
10976:   }
10977:   (((void(**)(void))mat->ops)[op]) = f;
10978:   return(0);
10979: }

10981: /*@C
10982:     MatGetOperation - Gets a matrix operation for any matrix type.

10984:     Not Collective

10986:     Input Parameters:
10987: +   mat - the matrix
10988: -   op - the name of the operation

10990:     Output Parameter:
10991: .   f - the function that provides the operation

10993:     Level: developer

10995:     Usage:
10996: $      PetscErrorCode (*usermult)(Mat,Vec,Vec);
10997: $      MatGetOperation(A,MATOP_MULT,(void(**)(void))&usermult);

10999:     Notes:
11000:     See the file include/petscmat.h for a complete list of matrix
11001:     operations, which all have the form MATOP_<OPERATION>, where
11002:     <OPERATION> is the name (in all capital letters) of the
11003:     user interface routine (e.g., MatMult() -> MATOP_MULT).

11005:     This routine is distinct from MatShellGetOperation() in that it can be called on any matrix type.

11007: .keywords: matrix, get, operation

11009: .seealso: MatSetOperation(), MatCreateShell(), MatShellGetContext(), MatShellGetOperation()
11010: @*/
11011: PetscErrorCode MatGetOperation(Mat mat,MatOperation op,void(**f)(void))
11012: {
11015:   *f = (((void (**)(void))mat->ops)[op]);
11016:   return(0);
11017: }

11019: /*@
11020:     MatHasOperation - Determines whether the given matrix supports the particular
11021:     operation.

11023:    Not Collective

11025:    Input Parameters:
11026: +  mat - the matrix
11027: -  op - the operation, for example, MATOP_GET_DIAGONAL

11029:    Output Parameter:
11030: .  has - either PETSC_TRUE or PETSC_FALSE

11032:    Level: advanced

11034:    Notes:
11035:    See the file include/petscmat.h for a complete list of matrix
11036:    operations, which all have the form MATOP_<OPERATION>, where
11037:    <OPERATION> is the name (in all capital letters) of the
11038:    user-level routine.  E.g., MatNorm() -> MATOP_NORM.

11040: .keywords: matrix, has, operation

11042: .seealso: MatCreateShell()
11043: @*/
11044: PetscErrorCode MatHasOperation(Mat mat,MatOperation op,PetscBool *has)
11045: {

11052:   if (mat->ops->hasoperation) {
11053:     (*mat->ops->hasoperation)(mat,op,has);
11054:   } else {
11055:     if (((void**)mat->ops)[op]) *has =  PETSC_TRUE;
11056:     else {
11057:       *has = PETSC_FALSE;
11058:       if (op == MATOP_CREATE_SUBMATRIX) {
11059:         PetscMPIInt size;

11061:         MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);
11062:         if (size == 1) {
11063:           MatHasOperation(mat,MATOP_CREATE_SUBMATRICES,has);
11064:         }
11065:       }
11066:     }
11067:   }
11068:   return(0);
11069: }

11071: /*@
11072:     MatHasCongruentLayouts - Determines whether the rows and columns layouts
11073:     of the matrix are congruent

11075:    Collective on mat

11077:    Input Parameters:
11078: .  mat - the matrix

11080:    Output Parameter:
11081: .  cong - either PETSC_TRUE or PETSC_FALSE

11083:    Level: beginner

11085:    Notes:

11087: .keywords: matrix, has

11089: .seealso: MatCreate(), MatSetSizes()
11090: @*/
11091: PetscErrorCode MatHasCongruentLayouts(Mat mat,PetscBool *cong)
11092: {

11099:   if (!mat->rmap || !mat->cmap) {
11100:     *cong = mat->rmap == mat->cmap ? PETSC_TRUE : PETSC_FALSE;
11101:     return(0);
11102:   }
11103:   if (mat->congruentlayouts == PETSC_DECIDE) { /* first time we compare rows and cols layouts */
11104:     PetscLayoutCompare(mat->rmap,mat->cmap,cong);
11105:     if (*cong) mat->congruentlayouts = 1;
11106:     else       mat->congruentlayouts = 0;
11107:   } else *cong = mat->congruentlayouts ? PETSC_TRUE : PETSC_FALSE;
11108:   return(0);
11109: }

11111: /*@
11112:     MatFreeIntermediateDataStructures - Free intermediate data structures created for reuse,
11113:     e.g., matrx product of MatPtAP.

11115:    Collective on mat

11117:    Input Parameters:
11118: .  mat - the matrix

11120:    Output Parameter:
11121: .  mat - the matrix with intermediate data structures released

11123:    Level: advanced

11125:    Notes:

11127: .keywords: matrix

11129: .seealso: MatPtAP(), MatMatMult()
11130: @*/
11131: PetscErrorCode MatFreeIntermediateDataStructures(Mat mat)
11132: {

11138:   if (mat->ops->freeintermediatedatastructures) {
11139:     (*mat->ops->freeintermediatedatastructures)(mat);
11140:   }
11141:   return(0);
11142: }