Actual source code: matimpl.h

  1: #pragma once

  3: #include <petscmat.h>
  4: #include <petscmatcoarsen.h>
  5: #include <petsc/private/petscimpl.h>

  7: PETSC_EXTERN PetscBool      MatRegisterAllCalled;
  8: PETSC_EXTERN PetscBool      MatSeqAIJRegisterAllCalled;
  9: PETSC_EXTERN PetscBool      MatOrderingRegisterAllCalled;
 10: PETSC_EXTERN PetscBool      MatColoringRegisterAllCalled;
 11: PETSC_EXTERN PetscBool      MatPartitioningRegisterAllCalled;
 12: PETSC_EXTERN PetscBool      MatCoarsenRegisterAllCalled;
 13: PETSC_EXTERN PetscErrorCode MatRegisterAll(void);
 14: PETSC_EXTERN PetscErrorCode MatOrderingRegisterAll(void);
 15: PETSC_EXTERN PetscErrorCode MatColoringRegisterAll(void);
 16: PETSC_EXTERN PetscErrorCode MatPartitioningRegisterAll(void);
 17: PETSC_EXTERN PetscErrorCode MatCoarsenRegisterAll(void);
 18: PETSC_EXTERN PetscErrorCode MatSeqAIJRegisterAll(void);

 20: /* Gets the root type of the input matrix's type (e.g., MATAIJ for MATSEQAIJ) */
 21: PETSC_EXTERN PetscErrorCode MatGetRootType_Private(Mat, MatType *);

 23: /* Gets the MPI type corresponding to the input matrix's type (e.g., MATMPIAIJ for MATSEQAIJ) */
 24: PETSC_EXTERN PetscErrorCode MatGetMPIMatType_Private(Mat, MatType *);

 26: /*
 27:   This file defines the parts of the matrix data structure that are
 28:   shared by all matrix types.
 29: */

 31: /*
 32:     If you add entries here also add them to the MATOP enum
 33:     in include/petscmat.h and src/mat/f90-mod/petscmat.h
 34: */
 35: typedef struct _MatOps *MatOps;
 36: struct _MatOps {
 37:   /* 0*/
 38:   PetscErrorCode (*setvalues)(Mat, PetscInt, const PetscInt[], PetscInt, const PetscInt[], const PetscScalar[], InsertMode);
 39:   PetscErrorCode (*getrow)(Mat, PetscInt, PetscInt *, PetscInt *[], PetscScalar *[]);
 40:   PetscErrorCode (*restorerow)(Mat, PetscInt, PetscInt *, PetscInt *[], PetscScalar *[]);
 41:   PetscErrorCode (*mult)(Mat, Vec, Vec);
 42:   PetscErrorCode (*multadd)(Mat, Vec, Vec, Vec);
 43:   /* 5*/
 44:   PetscErrorCode (*multtranspose)(Mat, Vec, Vec);
 45:   PetscErrorCode (*multtransposeadd)(Mat, Vec, Vec, Vec);
 46:   PetscErrorCode (*solve)(Mat, Vec, Vec);
 47:   PetscErrorCode (*solveadd)(Mat, Vec, Vec, Vec);
 48:   PetscErrorCode (*solvetranspose)(Mat, Vec, Vec);
 49:   /*10*/
 50:   PetscErrorCode (*solvetransposeadd)(Mat, Vec, Vec, Vec);
 51:   PetscErrorCode (*lufactor)(Mat, IS, IS, const MatFactorInfo *);
 52:   PetscErrorCode (*choleskyfactor)(Mat, IS, const MatFactorInfo *);
 53:   PetscErrorCode (*sor)(Mat, Vec, PetscReal, MatSORType, PetscReal, PetscInt, PetscInt, Vec);
 54:   PetscErrorCode (*transpose)(Mat, MatReuse, Mat *);
 55:   /*15*/
 56:   PetscErrorCode (*getinfo)(Mat, MatInfoType, MatInfo *);
 57:   PetscErrorCode (*equal)(Mat, Mat, PetscBool *);
 58:   PetscErrorCode (*getdiagonal)(Mat, Vec);
 59:   PetscErrorCode (*diagonalscale)(Mat, Vec, Vec);
 60:   PetscErrorCode (*norm)(Mat, NormType, PetscReal *);
 61:   /*20*/
 62:   PetscErrorCode (*assemblybegin)(Mat, MatAssemblyType);
 63:   PetscErrorCode (*assemblyend)(Mat, MatAssemblyType);
 64:   PetscErrorCode (*setoption)(Mat, MatOption, PetscBool);
 65:   PetscErrorCode (*zeroentries)(Mat);
 66:   /*24*/
 67:   PetscErrorCode (*zerorows)(Mat, PetscInt, const PetscInt[], PetscScalar, Vec, Vec);
 68:   PetscErrorCode (*lufactorsymbolic)(Mat, Mat, IS, IS, const MatFactorInfo *);
 69:   PetscErrorCode (*lufactornumeric)(Mat, Mat, const MatFactorInfo *);
 70:   PetscErrorCode (*choleskyfactorsymbolic)(Mat, Mat, IS, const MatFactorInfo *);
 71:   PetscErrorCode (*choleskyfactornumeric)(Mat, Mat, const MatFactorInfo *);
 72:   /*29*/
 73:   PetscErrorCode (*setup)(Mat);
 74:   PetscErrorCode (*ilufactorsymbolic)(Mat, Mat, IS, IS, const MatFactorInfo *);
 75:   PetscErrorCode (*iccfactorsymbolic)(Mat, Mat, IS, const MatFactorInfo *);
 76:   PetscErrorCode (*getdiagonalblock)(Mat, Mat *);
 77:   PetscErrorCode (*setinf)(Mat);
 78:   /*34*/
 79:   PetscErrorCode (*duplicate)(Mat, MatDuplicateOption, Mat *);
 80:   PetscErrorCode (*forwardsolve)(Mat, Vec, Vec);
 81:   PetscErrorCode (*backwardsolve)(Mat, Vec, Vec);
 82:   PetscErrorCode (*ilufactor)(Mat, IS, IS, const MatFactorInfo *);
 83:   PetscErrorCode (*iccfactor)(Mat, IS, const MatFactorInfo *);
 84:   /*39*/
 85:   PetscErrorCode (*axpy)(Mat, PetscScalar, Mat, MatStructure);
 86:   PetscErrorCode (*createsubmatrices)(Mat, PetscInt, const IS[], const IS[], MatReuse, Mat *[]);
 87:   PetscErrorCode (*increaseoverlap)(Mat, PetscInt, IS[], PetscInt);
 88:   PetscErrorCode (*getvalues)(Mat, PetscInt, const PetscInt[], PetscInt, const PetscInt[], PetscScalar[]);
 89:   PetscErrorCode (*copy)(Mat, Mat, MatStructure);
 90:   /*44*/
 91:   PetscErrorCode (*getrowmax)(Mat, Vec, PetscInt[]);
 92:   PetscErrorCode (*scale)(Mat, PetscScalar);
 93:   PetscErrorCode (*shift)(Mat, PetscScalar);
 94:   PetscErrorCode (*diagonalset)(Mat, Vec, InsertMode);
 95:   PetscErrorCode (*zerorowscolumns)(Mat, PetscInt, const PetscInt[], PetscScalar, Vec, Vec);
 96:   /*49*/
 97:   PetscErrorCode (*setrandom)(Mat, PetscRandom);
 98:   PetscErrorCode (*getrowij)(Mat, PetscInt, PetscBool, PetscBool, PetscInt *, const PetscInt *[], const PetscInt *[], PetscBool *);
 99:   PetscErrorCode (*restorerowij)(Mat, PetscInt, PetscBool, PetscBool, PetscInt *, const PetscInt *[], const PetscInt *[], PetscBool *);
100:   PetscErrorCode (*getcolumnij)(Mat, PetscInt, PetscBool, PetscBool, PetscInt *, const PetscInt *[], const PetscInt *[], PetscBool *);
101:   PetscErrorCode (*restorecolumnij)(Mat, PetscInt, PetscBool, PetscBool, PetscInt *, const PetscInt *[], const PetscInt *[], PetscBool *);
102:   /*54*/
103:   PetscErrorCode (*fdcoloringcreate)(Mat, ISColoring, MatFDColoring);
104:   PetscErrorCode (*coloringpatch)(Mat, PetscInt, PetscInt, ISColoringValue[], ISColoring *);
105:   PetscErrorCode (*setunfactored)(Mat);
106:   PetscErrorCode (*permute)(Mat, IS, IS, Mat *);
107:   PetscErrorCode (*setvaluesblocked)(Mat, PetscInt, const PetscInt[], PetscInt, const PetscInt[], const PetscScalar[], InsertMode);
108:   /*59*/
109:   PetscErrorCode (*createsubmatrix)(Mat, IS, IS, MatReuse, Mat *);
110:   PetscErrorCode (*destroy)(Mat);
111:   PetscErrorCode (*view)(Mat, PetscViewer);
112:   PetscErrorCode (*convertfrom)(Mat, MatType, MatReuse, Mat *);
113:   PetscErrorCode (*placeholder_63)(void);
114:   /*64*/
115:   PetscErrorCode (*matmatmultsymbolic)(Mat, Mat, Mat, PetscReal, Mat);
116:   PetscErrorCode (*matmatmultnumeric)(Mat, Mat, Mat, Mat);
117:   PetscErrorCode (*setlocaltoglobalmapping)(Mat, ISLocalToGlobalMapping, ISLocalToGlobalMapping);
118:   PetscErrorCode (*setvalueslocal)(Mat, PetscInt, const PetscInt[], PetscInt, const PetscInt[], const PetscScalar[], InsertMode);
119:   PetscErrorCode (*zerorowslocal)(Mat, PetscInt, const PetscInt[], PetscScalar, Vec, Vec);
120:   /*69*/
121:   PetscErrorCode (*getrowmaxabs)(Mat, Vec, PetscInt[]);
122:   PetscErrorCode (*getrowminabs)(Mat, Vec, PetscInt[]);
123:   PetscErrorCode (*convert)(Mat, MatType, MatReuse, Mat *);
124:   PetscErrorCode (*hasoperation)(Mat, MatOperation, PetscBool *);
125:   PetscErrorCode (*placeholder_73)(void);
126:   /*74*/
127:   PetscErrorCode (*setvaluesadifor)(Mat, PetscInt, void *);
128:   PetscErrorCode (*fdcoloringapply)(Mat, MatFDColoring, Vec, void *);
129:   PetscErrorCode (*setfromoptions)(Mat, PetscOptionItems *);
130:   PetscErrorCode (*placeholder_77)(void);
131:   PetscErrorCode (*placeholder_78)(void);
132:   /*79*/
133:   PetscErrorCode (*findzerodiagonals)(Mat, IS *);
134:   PetscErrorCode (*mults)(Mat, Vecs, Vecs);
135:   PetscErrorCode (*solves)(Mat, Vecs, Vecs);
136:   PetscErrorCode (*getinertia)(Mat, PetscInt *, PetscInt *, PetscInt *);
137:   PetscErrorCode (*load)(Mat, PetscViewer);
138:   /*84*/
139:   PetscErrorCode (*issymmetric)(Mat, PetscReal, PetscBool *);
140:   PetscErrorCode (*ishermitian)(Mat, PetscReal, PetscBool *);
141:   PetscErrorCode (*isstructurallysymmetric)(Mat, PetscBool *);
142:   PetscErrorCode (*setvaluesblockedlocal)(Mat, PetscInt, const PetscInt[], PetscInt, const PetscInt[], const PetscScalar[], InsertMode);
143:   PetscErrorCode (*getvecs)(Mat, Vec *, Vec *);
144:   /*89*/
145:   PetscErrorCode (*placeholder_89)(void);
146:   PetscErrorCode (*matmultsymbolic)(Mat, Mat, PetscReal, Mat);
147:   PetscErrorCode (*matmultnumeric)(Mat, Mat, Mat);
148:   PetscErrorCode (*placeholder_92)(void);
149:   PetscErrorCode (*ptapsymbolic)(Mat, Mat, PetscReal, Mat); /* double dispatch wrapper routine */
150:   /*94*/
151:   PetscErrorCode (*ptapnumeric)(Mat, Mat, Mat); /* double dispatch wrapper routine */
152:   PetscErrorCode (*placeholder_95)(void);
153:   PetscErrorCode (*mattransposemultsymbolic)(Mat, Mat, PetscReal, Mat);
154:   PetscErrorCode (*mattransposemultnumeric)(Mat, Mat, Mat);
155:   PetscErrorCode (*bindtocpu)(Mat, PetscBool);
156:   /*99*/
157:   PetscErrorCode (*productsetfromoptions)(Mat);
158:   PetscErrorCode (*productsymbolic)(Mat);
159:   PetscErrorCode (*productnumeric)(Mat);
160:   PetscErrorCode (*conjugate)(Mat); /* complex conjugate */
161:   PetscErrorCode (*viewnative)(Mat, PetscViewer);
162:   /*104*/
163:   PetscErrorCode (*setvaluesrow)(Mat, PetscInt, const PetscScalar[]);
164:   PetscErrorCode (*realpart)(Mat);
165:   PetscErrorCode (*imaginarypart)(Mat);
166:   PetscErrorCode (*getrowuppertriangular)(Mat);
167:   PetscErrorCode (*restorerowuppertriangular)(Mat);
168:   /*109*/
169:   PetscErrorCode (*matsolve)(Mat, Mat, Mat);
170:   PetscErrorCode (*matsolvetranspose)(Mat, Mat, Mat);
171:   PetscErrorCode (*getrowmin)(Mat, Vec, PetscInt[]);
172:   PetscErrorCode (*getcolumnvector)(Mat, Vec, PetscInt);
173:   PetscErrorCode (*missingdiagonal)(Mat, PetscBool *, PetscInt *);
174:   /*114*/
175:   PetscErrorCode (*getseqnonzerostructure)(Mat, Mat *);
176:   PetscErrorCode (*create)(Mat);
177:   PetscErrorCode (*getghosts)(Mat, PetscInt *, const PetscInt *[]);
178:   PetscErrorCode (*getlocalsubmatrix)(Mat, IS, IS, Mat *);
179:   PetscErrorCode (*restorelocalsubmatrix)(Mat, IS, IS, Mat *);
180:   /*119*/
181:   PetscErrorCode (*multdiagonalblock)(Mat, Vec, Vec);
182:   PetscErrorCode (*hermitiantranspose)(Mat, MatReuse, Mat *);
183:   PetscErrorCode (*multhermitiantranspose)(Mat, Vec, Vec);
184:   PetscErrorCode (*multhermitiantransposeadd)(Mat, Vec, Vec, Vec);
185:   PetscErrorCode (*getmultiprocblock)(Mat, MPI_Comm, MatReuse, Mat *);
186:   /*124*/
187:   PetscErrorCode (*findnonzerorows)(Mat, IS *);
188:   PetscErrorCode (*getcolumnreductions)(Mat, PetscInt, PetscReal *);
189:   PetscErrorCode (*invertblockdiagonal)(Mat, const PetscScalar **);
190:   PetscErrorCode (*invertvariableblockdiagonal)(Mat, PetscInt, const PetscInt *, PetscScalar *);
191:   PetscErrorCode (*createsubmatricesmpi)(Mat, PetscInt, const IS[], const IS[], MatReuse, Mat **);
192:   /*129*/
193:   PetscErrorCode (*setvaluesbatch)(Mat, PetscInt, PetscInt, PetscInt *, const PetscScalar *);
194:   PetscErrorCode (*placeholder_130)(void);
195:   PetscErrorCode (*transposematmultsymbolic)(Mat, Mat, PetscReal, Mat);
196:   PetscErrorCode (*transposematmultnumeric)(Mat, Mat, Mat);
197:   PetscErrorCode (*transposecoloringcreate)(Mat, ISColoring, MatTransposeColoring);
198:   /*134*/
199:   PetscErrorCode (*transcoloringapplysptoden)(MatTransposeColoring, Mat, Mat);
200:   PetscErrorCode (*transcoloringapplydentosp)(MatTransposeColoring, Mat, Mat);
201:   PetscErrorCode (*placeholder_136)(void);
202:   PetscErrorCode (*rartsymbolic)(Mat, Mat, PetscReal, Mat); /* double dispatch wrapper routine */
203:   PetscErrorCode (*rartnumeric)(Mat, Mat, Mat);             /* double dispatch wrapper routine */
204:   /*139*/
205:   PetscErrorCode (*setblocksizes)(Mat, PetscInt, PetscInt);
206:   PetscErrorCode (*aypx)(Mat, PetscScalar, Mat, MatStructure);
207:   PetscErrorCode (*residual)(Mat, Vec, Vec, Vec);
208:   PetscErrorCode (*fdcoloringsetup)(Mat, ISColoring, MatFDColoring);
209:   PetscErrorCode (*findoffblockdiagonalentries)(Mat, IS *);
210:   PetscErrorCode (*creatempimatconcatenateseqmat)(MPI_Comm, Mat, PetscInt, MatReuse, Mat *);
211:   /*145*/
212:   PetscErrorCode (*destroysubmatrices)(PetscInt, Mat *[]);
213:   PetscErrorCode (*mattransposesolve)(Mat, Mat, Mat);
214:   PetscErrorCode (*getvalueslocal)(Mat, PetscInt, const PetscInt[], PetscInt, const PetscInt[], PetscScalar[]);
215:   PetscErrorCode (*creategraph)(Mat, PetscBool, PetscBool, PetscReal, PetscInt, PetscInt[], Mat *);
216:   PetscErrorCode (*dummy)(Mat);
217:   /*150*/
218:   PetscErrorCode (*transposesymbolic)(Mat, Mat *);
219:   PetscErrorCode (*eliminatezeros)(Mat, PetscBool);
220:   PetscErrorCode (*getrowsumabs)(Mat, Vec);
221: };
222: /*
223:     If you add MatOps entries above also add them to the MATOP enum
224:     in include/petscmat.h and src/mat/f90-mod/petscmat.h
225: */

227: #include <petscsys.h>

229: typedef struct _p_MatRootName *MatRootName;
230: struct _p_MatRootName {
231:   char       *rname, *sname, *mname;
232:   MatRootName next;
233: };

235: PETSC_EXTERN MatRootName MatRootNameList;

237: /*
238:    Utility private matrix routines used outside Mat
239: */
240: PETSC_SINGLE_LIBRARY_INTERN PetscErrorCode MatFindNonzeroRowsOrCols_Basic(Mat, PetscBool, PetscReal, IS *);

242: /*
243:    Utility private matrix routines
244: */
245: PETSC_INTERN PetscErrorCode MatConvert_Basic(Mat, MatType, MatReuse, Mat *);
246: PETSC_INTERN PetscErrorCode MatConvert_Shell(Mat, MatType, MatReuse, Mat *);
247: PETSC_INTERN PetscErrorCode MatConvertFrom_Shell(Mat, MatType, MatReuse, Mat *);
248: PETSC_INTERN PetscErrorCode MatCopy_Basic(Mat, Mat, MatStructure);
249: PETSC_INTERN PetscErrorCode MatDiagonalSet_Default(Mat, Vec, InsertMode);
250: #if defined(PETSC_HAVE_SCALAPACK)
251: PETSC_INTERN PetscErrorCode MatConvert_Dense_ScaLAPACK(Mat, MatType, MatReuse, Mat *);
252: #endif
253: PETSC_INTERN PetscErrorCode MatSetPreallocationCOO_Basic(Mat, PetscCount, PetscInt[], PetscInt[]);
254: PETSC_INTERN PetscErrorCode MatSetValuesCOO_Basic(Mat, const PetscScalar[], InsertMode);

256: /* This can be moved to the public header after implementing some missing MatProducts */
257: PETSC_INTERN PetscErrorCode MatCreateFromISLocalToGlobalMapping(ISLocalToGlobalMapping, Mat, PetscBool, PetscBool, MatType, Mat *);

259: /* these callbacks rely on the old matrix function pointers for
260:    matmat operations. They are unsafe, and should be removed.
261:    However, the amount of work needed to clean up all the
262:    implementations is not negligible */
263: PETSC_INTERN PetscErrorCode MatProductSymbolic_AB(Mat);
264: PETSC_INTERN PetscErrorCode MatProductNumeric_AB(Mat);
265: PETSC_INTERN PetscErrorCode MatProductSymbolic_AtB(Mat);
266: PETSC_INTERN PetscErrorCode MatProductNumeric_AtB(Mat);
267: PETSC_INTERN PetscErrorCode MatProductSymbolic_ABt(Mat);
268: PETSC_INTERN PetscErrorCode MatProductNumeric_ABt(Mat);
269: PETSC_INTERN PetscErrorCode MatProductNumeric_PtAP(Mat);
270: PETSC_INTERN PetscErrorCode MatProductNumeric_RARt(Mat);
271: PETSC_INTERN PetscErrorCode MatProductSymbolic_ABC(Mat);
272: PETSC_INTERN PetscErrorCode MatProductNumeric_ABC(Mat);

274: PETSC_INTERN PetscErrorCode MatProductCreate_Private(Mat, Mat, Mat, Mat);
275: /* this callback handles all the different triple products and
276:    does not rely on the function pointers; used by cuSPARSE/hipSPARSE and KOKKOS-KERNELS */
277: PETSC_INTERN PetscErrorCode MatProductSymbolic_ABC_Basic(Mat);

279: /* CreateGraph is common to AIJ seq and mpi */
280: PETSC_INTERN PetscErrorCode MatCreateGraph_Simple_AIJ(Mat, PetscBool, PetscBool, PetscReal, PetscInt, PetscInt[], Mat *);

282: #if defined(PETSC_CLANG_STATIC_ANALYZER)
283: template <typename Tm>
284: extern void MatCheckPreallocated(Tm, int);
285: template <typename Tm>
286: extern void MatCheckProduct(Tm, int);
287: #else /* PETSC_CLANG_STATIC_ANALYZER */
288:   #define MatCheckPreallocated(A, arg) \
289:     do { \
290:       if (!(A)->preallocated) PetscCall(MatSetUp(A)); \
291:     } while (0)

293:   #if defined(PETSC_USE_DEBUG)
294:     #define MatCheckProduct(A, arg) \
295:       do { \
296:         PetscCheck((A)->product, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Argument %d \"%s\" is not a matrix obtained from MatProductCreate()", (arg), #A); \
297:       } while (0)
298:   #else
299:     #define MatCheckProduct(A, arg) \
300:       do { \
301:       } while (0)
302:   #endif
303: #endif /* PETSC_CLANG_STATIC_ANALYZER */

305: /*
306:   The stash is used to temporarily store inserted matrix values that
307:   belong to another processor. During the assembly phase the stashed
308:   values are moved to the correct processor and
309: */

311: typedef struct _MatStashSpace *PetscMatStashSpace;

313: struct _MatStashSpace {
314:   PetscMatStashSpace next;
315:   PetscScalar       *space_head, *val;
316:   PetscInt          *idx, *idy;
317:   PetscInt           total_space_size;
318:   PetscInt           local_used;
319:   PetscInt           local_remaining;
320: };

322: PETSC_EXTERN PetscErrorCode PetscMatStashSpaceGet(PetscInt, PetscInt, PetscMatStashSpace *);
323: PETSC_EXTERN PetscErrorCode PetscMatStashSpaceContiguous(PetscInt, PetscMatStashSpace *, PetscScalar *, PetscInt *, PetscInt *);
324: PETSC_EXTERN PetscErrorCode PetscMatStashSpaceDestroy(PetscMatStashSpace *);

326: typedef struct {
327:   PetscInt count;
328: } MatStashHeader;

330: typedef struct {
331:   void    *buffer; /* Of type blocktype, dynamically constructed  */
332:   PetscInt count;
333:   char     pending;
334: } MatStashFrame;

336: typedef struct _MatStash MatStash;
337: struct _MatStash {
338:   PetscInt           nmax;              /* maximum stash size */
339:   PetscInt           umax;              /* user specified max-size */
340:   PetscInt           oldnmax;           /* the nmax value used previously */
341:   PetscInt           n;                 /* stash size */
342:   PetscInt           bs;                /* block size of the stash */
343:   PetscInt           reallocs;          /* preserve the no of mallocs invoked */
344:   PetscMatStashSpace space_head, space; /* linked list to hold stashed global row/column numbers and matrix values */

346:   PetscErrorCode (*ScatterBegin)(Mat, MatStash *, PetscInt *);
347:   PetscErrorCode (*ScatterGetMesg)(MatStash *, PetscMPIInt *, PetscInt **, PetscInt **, PetscScalar **, PetscInt *);
348:   PetscErrorCode (*ScatterEnd)(MatStash *);
349:   PetscErrorCode (*ScatterDestroy)(MatStash *);

351:   /* The following variables are used for communication */
352:   MPI_Comm      comm;
353:   PetscMPIInt   size, rank;
354:   PetscMPIInt   tag1, tag2;
355:   MPI_Request  *send_waits;     /* array of send requests */
356:   MPI_Request  *recv_waits;     /* array of receive requests */
357:   MPI_Status   *send_status;    /* array of send status */
358:   PetscInt      nsends, nrecvs; /* numbers of sends and receives */
359:   PetscScalar  *svalues;        /* sending data */
360:   PetscInt     *sindices;
361:   PetscScalar **rvalues;    /* receiving data (values) */
362:   PetscInt    **rindices;   /* receiving data (indices) */
363:   PetscInt      nprocessed; /* number of messages already processed */
364:   PetscMPIInt  *flg_v;      /* indicates what messages have arrived so far and from whom */
365:   PetscBool     reproduce;
366:   PetscInt      reproduce_count;

368:   /* The following variables are used for BTS communication */
369:   PetscBool       first_assembly_done; /* Is the first time matrix assembly done? */
370:   PetscBool       use_status;          /* Use MPI_Status to determine number of items in each message */
371:   PetscMPIInt     nsendranks;
372:   PetscMPIInt     nrecvranks;
373:   PetscMPIInt    *sendranks;
374:   PetscMPIInt    *recvranks;
375:   MatStashHeader *sendhdr, *recvhdr;
376:   MatStashFrame  *sendframes; /* pointers to the main messages */
377:   MatStashFrame  *recvframes;
378:   MatStashFrame  *recvframe_active;
379:   PetscInt        recvframe_i;     /* index of block within active frame */
380:   PetscMPIInt     recvframe_count; /* Count actually sent for current frame */
381:   PetscInt        recvcount;       /* Number of receives processed so far */
382:   PetscMPIInt    *some_indices;    /* From last call to MPI_Waitsome */
383:   MPI_Status     *some_statuses;   /* Statuses from last call to MPI_Waitsome */
384:   PetscMPIInt     some_count;      /* Number of requests completed in last call to MPI_Waitsome */
385:   PetscMPIInt     some_i;          /* Index of request currently being processed */
386:   MPI_Request    *sendreqs;
387:   MPI_Request    *recvreqs;
388:   PetscSegBuffer  segsendblocks;
389:   PetscSegBuffer  segrecvframe;
390:   PetscSegBuffer  segrecvblocks;
391:   MPI_Datatype    blocktype;
392:   size_t          blocktype_size;
393:   InsertMode     *insertmode; /* Pointer to check mat->insertmode and set upon message arrival in case no local values have been set. */
394: };

396: #if !defined(PETSC_HAVE_MPIUNI)
397: PETSC_INTERN PetscErrorCode MatStashScatterDestroy_BTS(MatStash *);
398: #endif
399: PETSC_INTERN PetscErrorCode MatStashCreate_Private(MPI_Comm, PetscInt, MatStash *);
400: PETSC_INTERN PetscErrorCode MatStashDestroy_Private(MatStash *);
401: PETSC_INTERN PetscErrorCode MatStashScatterEnd_Private(MatStash *);
402: PETSC_INTERN PetscErrorCode MatStashSetInitialSize_Private(MatStash *, PetscInt);
403: PETSC_INTERN PetscErrorCode MatStashGetInfo_Private(MatStash *, PetscInt *, PetscInt *);
404: PETSC_INTERN PetscErrorCode MatStashValuesRow_Private(MatStash *, PetscInt, PetscInt, const PetscInt[], const PetscScalar[], PetscBool);
405: PETSC_INTERN PetscErrorCode MatStashValuesCol_Private(MatStash *, PetscInt, PetscInt, const PetscInt[], const PetscScalar[], PetscInt, PetscBool);
406: PETSC_INTERN PetscErrorCode MatStashValuesRowBlocked_Private(MatStash *, PetscInt, PetscInt, const PetscInt[], const PetscScalar[], PetscInt, PetscInt, PetscInt);
407: PETSC_INTERN PetscErrorCode MatStashValuesColBlocked_Private(MatStash *, PetscInt, PetscInt, const PetscInt[], const PetscScalar[], PetscInt, PetscInt, PetscInt);
408: PETSC_INTERN PetscErrorCode MatStashScatterBegin_Private(Mat, MatStash *, PetscInt *);
409: PETSC_INTERN PetscErrorCode MatStashScatterGetMesg_Private(MatStash *, PetscMPIInt *, PetscInt **, PetscInt **, PetscScalar **, PetscInt *);
410: PETSC_INTERN PetscErrorCode MatGetInfo_External(Mat, MatInfoType, MatInfo *);

412: typedef struct {
413:   PetscInt  dim;
414:   PetscInt  dims[4];
415:   PetscInt  starts[4];
416:   PetscBool noc; /* this is a single component problem, hence user will not set MatStencil.c */
417: } MatStencilInfo;

419: /* Info about using compressed row format */
420: typedef struct {
421:   PetscBool use;    /* indicates compressed rows have been checked and will be used */
422:   PetscInt  nrows;  /* number of non-zero rows */
423:   PetscInt *i;      /* compressed row pointer  */
424:   PetscInt *rindex; /* compressed row index               */
425: } Mat_CompressedRow;
426: PETSC_EXTERN PetscErrorCode MatCheckCompressedRow(Mat, PetscInt, Mat_CompressedRow *, PetscInt *, PetscInt, PetscReal);

428: typedef struct { /* used by MatCreateRedundantMatrix() for reusing matredundant */
429:   PetscInt     nzlocal, nsends, nrecvs;
430:   PetscMPIInt *send_rank, *recv_rank;
431:   PetscInt    *sbuf_nz, *rbuf_nz, *sbuf_j, **rbuf_j;
432:   PetscScalar *sbuf_a, **rbuf_a;
433:   MPI_Comm     subcomm; /* when user does not provide a subcomm */
434:   IS           isrow, iscol;
435:   Mat         *matseq;
436: } Mat_Redundant;

438: typedef struct { /* used by MatProduct() */
439:   MatProductType type;
440:   char          *alg;
441:   Mat            A, B, C, Dwork;
442:   PetscBool      symbolic_used_the_fact_A_is_symmetric; /* Symbolic phase took advantage of the fact that A is symmetric, and optimized e.g. AtB as AB. Then, .. */
443:   PetscBool      symbolic_used_the_fact_B_is_symmetric; /* .. in the numeric phase, if a new A is not symmetric (but has the same sparsity as the old A therefore .. */
444:   PetscBool      symbolic_used_the_fact_C_is_symmetric; /* MatMatMult(A,B,MAT_REUSE_MATRIX,..&C) is still legitimate), we need to redo symbolic! */
445:   PetscReal      fill;
446:   PetscBool      api_user; /* used to distinguish command line options and to indicate the matrix values are ready to be consumed at symbolic phase if needed */
447:   PetscBool      setfromoptionscalled;

449:   /* Some products may display the information on the algorithm used */
450:   PetscErrorCode (*view)(Mat, PetscViewer);

452:   /* many products have intermediate data structures, each specific to Mat types and product type */
453:   PetscBool clear;                   /* whether or not to clear the data structures after MatProductNumeric has been called */
454:   void     *data;                    /* where to stash those structures */
455:   PetscErrorCode (*destroy)(void *); /* destroy routine */
456: } Mat_Product;

458: struct _p_Mat {
459:   PETSCHEADER(struct _MatOps);
460:   PetscLayout      rmap, cmap;
461:   void            *data;                                    /* implementation-specific data */
462:   MatFactorType    factortype;                              /* MAT_FACTOR_LU, ILU, CHOLESKY or ICC */
463:   PetscBool        trivialsymbolic;                         /* indicates the symbolic factorization doesn't actually do a symbolic factorization, it is delayed to the numeric factorization */
464:   PetscBool        canuseordering;                          /* factorization can use ordering provide to routine (most PETSc implementations) */
465:   MatOrderingType  preferredordering[MAT_FACTOR_NUM_TYPES]; /* what is the preferred (or default) ordering for the matrix solver type */
466:   PetscBool        assembled;                               /* is the matrix assembled? */
467:   PetscBool        was_assembled;                           /* new values inserted into assembled mat */
468:   PetscInt         num_ass;                                 /* number of times matrix has been assembled */
469:   PetscObjectState nonzerostate;                            /* each time new nonzeros locations are introduced into the matrix this is updated */
470:   PetscObjectState ass_nonzerostate;                        /* nonzero state at last assembly */
471:   MatInfo          info;                                    /* matrix information */
472:   InsertMode       insertmode;                              /* have values been inserted in matrix or added? */
473:   MatStash         stash, bstash;                           /* used for assembling off-proc mat emements */
474:   MatNullSpace     nullsp;                                  /* null space (operator is singular) */
475:   MatNullSpace     transnullsp;                             /* null space of transpose of operator */
476:   MatNullSpace     nearnullsp;                              /* near null space to be used by multigrid methods */
477:   PetscInt         congruentlayouts;                        /* are the rows and columns layouts congruent? */
478:   PetscBool        preallocated;
479:   MatStencilInfo   stencil; /* information for structured grid */
480:   PetscBool3       symmetric, hermitian, structurally_symmetric, spd;
481:   PetscBool        symmetry_eternal, structural_symmetry_eternal, spd_eternal;
482:   PetscBool        nooffprocentries, nooffproczerorows;
483:   PetscBool        assembly_subset; /* set by MAT_SUBSET_OFF_PROC_ENTRIES */
484:   PetscBool        submat_singleis; /* for efficient PCSetUp_ASM() */
485:   PetscBool        structure_only;
486:   PetscBool        sortedfull;      /* full, sorted rows are inserted */
487:   PetscBool        force_diagonals; /* set by MAT_FORCE_DIAGONAL_ENTRIES */
488: #if defined(PETSC_HAVE_DEVICE)
489:   PetscOffloadMask offloadmask; /* a mask which indicates where the valid matrix data is (GPU, CPU or both) */
490:   PetscBool        boundtocpu;
491:   PetscBool        bindingpropagates;
492: #endif
493:   char                *defaultrandtype;
494:   void                *spptr; /* pointer for special library like SuperLU */
495:   char                *solvertype;
496:   PetscBool            checksymmetryonassembly, checknullspaceonassembly;
497:   PetscReal            checksymmetrytol;
498:   Mat                  schur;                            /* Schur complement matrix */
499:   MatFactorSchurStatus schur_status;                     /* status of the Schur complement matrix */
500:   Mat_Redundant       *redundant;                        /* used by MatCreateRedundantMatrix() */
501:   PetscBool            erroriffailure;                   /* Generate an error if detected (for example a zero pivot) instead of returning */
502:   MatFactorError       factorerrortype;                  /* type of error in factorization */
503:   PetscReal            factorerror_zeropivot_value;      /* If numerical zero pivot was detected this is the computed value */
504:   PetscInt             factorerror_zeropivot_row;        /* Row where zero pivot was detected */
505:   PetscInt             nblocks, *bsizes;                 /* support for MatSetVariableBlockSizes() */
506:   PetscInt             p_cstart, p_rank, p_cend, n_rank; /* Information from parallel MatComputeVariableBlockEnvelope() */
507:   PetscBool            p_parallel;
508:   char                *defaultvectype;
509:   Mat_Product         *product;
510:   PetscBool            form_explicit_transpose; /* hint to generate an explicit mat tranpsose for operations like MatMultTranspose() */
511:   PetscBool            transupdated;            /* whether or not the explicitly generated transpose is up-to-date */
512:   char                *factorprefix;            /* the prefix to use with factored matrix that is created */
513:   PetscBool            hash_active;             /* indicates MatSetValues() is being handled by hashing */
514: };

516: PETSC_INTERN PetscErrorCode MatAXPY_Basic(Mat, PetscScalar, Mat, MatStructure);
517: PETSC_INTERN PetscErrorCode MatAXPY_BasicWithPreallocation(Mat, Mat, PetscScalar, Mat, MatStructure);
518: PETSC_INTERN PetscErrorCode MatAXPY_Basic_Preallocate(Mat, Mat, Mat *);
519: PETSC_INTERN PetscErrorCode MatAXPY_Dense_Nest(Mat, PetscScalar, Mat);

521: PETSC_INTERN PetscErrorCode MatSetUp_Default(Mat);

523: /*
524:     Utility for MatZeroRows
525: */
526: PETSC_INTERN PetscErrorCode MatZeroRowsMapLocal_Private(Mat, PetscInt, const PetscInt *, PetscInt *, PetscInt **);

528: /*
529:     Utility for MatView/MatLoad
530: */
531: PETSC_INTERN PetscErrorCode MatView_Binary_BlockSizes(Mat, PetscViewer);
532: PETSC_INTERN PetscErrorCode MatLoad_Binary_BlockSizes(Mat, PetscViewer);

534: /*
535:     Object for partitioning graphs
536: */

538: typedef struct _MatPartitioningOps *MatPartitioningOps;
539: struct _MatPartitioningOps {
540:   PetscErrorCode (*apply)(MatPartitioning, IS *);
541:   PetscErrorCode (*applynd)(MatPartitioning, IS *);
542:   PetscErrorCode (*setfromoptions)(MatPartitioning, PetscOptionItems *);
543:   PetscErrorCode (*destroy)(MatPartitioning);
544:   PetscErrorCode (*view)(MatPartitioning, PetscViewer);
545:   PetscErrorCode (*improve)(MatPartitioning, IS *);
546: };

548: struct _p_MatPartitioning {
549:   PETSCHEADER(struct _MatPartitioningOps);
550:   Mat        adj;
551:   PetscInt  *vertex_weights;
552:   PetscReal *part_weights;
553:   PetscInt   n;    /* number of partitions */
554:   PetscInt   ncon; /* number of vertex weights per vertex */
555:   void      *data;
556:   PetscInt   setupcalled;
557:   PetscBool  use_edge_weights; /* A flag indicates whether or not to use edge weights */
558: };

560: /* needed for parallel nested dissection by ParMetis and PTSCOTCH */
561: PETSC_INTERN PetscErrorCode MatPartitioningSizesToSep_Private(PetscInt, PetscInt[], PetscInt[], PetscInt[]);

563: /*
564:     Object for coarsen graphs
565: */
566: typedef struct _MatCoarsenOps *MatCoarsenOps;
567: struct _MatCoarsenOps {
568:   PetscErrorCode (*apply)(MatCoarsen);
569:   PetscErrorCode (*setfromoptions)(MatCoarsen, PetscOptionItems *);
570:   PetscErrorCode (*destroy)(MatCoarsen);
571:   PetscErrorCode (*view)(MatCoarsen, PetscViewer);
572: };

574: #define MAT_COARSEN_STRENGTH_INDEX_SIZE 3
575: struct _p_MatCoarsen {
576:   PETSCHEADER(struct _MatCoarsenOps);
577:   Mat   graph;
578:   void *subctx;
579:   /* */
580:   PetscBool         strict_aggs;
581:   IS                perm;
582:   PetscCoarsenData *agg_lists;
583:   PetscInt          max_it;    /* number of iterations in HEM */
584:   PetscReal         threshold; /* HEM can filter interim graphs */
585:   PetscInt          strength_index_size;
586:   PetscInt          strength_index[MAT_COARSEN_STRENGTH_INDEX_SIZE];
587: };

589: PETSC_EXTERN PetscErrorCode MatCoarsenMISKSetDistance(MatCoarsen, PetscInt);
590: PETSC_EXTERN PetscErrorCode MatCoarsenMISKGetDistance(MatCoarsen, PetscInt *);

592: /*
593:     Used in aijdevice.h
594: */
595: typedef struct {
596:   PetscInt    *i;
597:   PetscInt    *j;
598:   PetscScalar *a;
599:   PetscInt     n;
600:   PetscInt     ignorezeroentries;
601: } PetscCSRDataStructure;

603: /*
604:     MatFDColoring is used to compute Jacobian matrices efficiently
605:   via coloring. The data structure is explained below in an example.

607:    Color =   0    1     0    2   |   2      3       0
608:    ---------------------------------------------------
609:             00   01              |          05
610:             10   11              |   14     15               Processor  0
611:                        22    23  |          25
612:                        32    33  |
613:    ===================================================
614:                                  |   44     45     46
615:             50                   |          55               Processor 1
616:                                  |   64            66
617:    ---------------------------------------------------

619:     ncolors = 4;

621:     ncolumns      = {2,1,1,0}
622:     columns       = {{0,2},{1},{3},{}}
623:     nrows         = {4,2,3,3}
624:     rows          = {{0,1,2,3},{0,1},{1,2,3},{0,1,2}}
625:     vwscale       = {dx(0),dx(1),dx(2),dx(3)}               MPI Vec
626:     vscale        = {dx(0),dx(1),dx(2),dx(3),dx(4),dx(5)}   Seq Vec

628:     ncolumns      = {1,0,1,1}
629:     columns       = {{6},{},{4},{5}}
630:     nrows         = {3,0,2,2}
631:     rows          = {{0,1,2},{},{1,2},{1,2}}
632:     vwscale       = {dx(4),dx(5),dx(6)}              MPI Vec
633:     vscale        = {dx(0),dx(4),dx(5),dx(6)}        Seq Vec

635:     See the routine MatFDColoringApply() for how this data is used
636:     to compute the Jacobian.

638: */
639: typedef struct {
640:   PetscInt     row;
641:   PetscInt     col;
642:   PetscScalar *valaddr; /* address of value */
643: } MatEntry;

645: typedef struct {
646:   PetscInt     row;
647:   PetscScalar *valaddr; /* address of value */
648: } MatEntry2;

650: struct _p_MatFDColoring {
651:   PETSCHEADER(int);
652:   PetscInt     M, N, m;                           /* total rows, columns; local rows */
653:   PetscInt     rstart;                            /* first row owned by local processor */
654:   PetscInt     ncolors;                           /* number of colors */
655:   PetscInt    *ncolumns;                          /* number of local columns for a color */
656:   PetscInt   **columns;                           /* lists the local columns of each color (using global column numbering) */
657:   IS          *isa;                               /* these are the IS that contain the column values given in columns */
658:   PetscInt    *nrows;                             /* number of local rows for each color */
659:   MatEntry    *matentry;                          /* holds (row, column, address of value) for Jacobian matrix entry */
660:   MatEntry2   *matentry2;                         /* holds (row, address of value) for Jacobian matrix entry */
661:   PetscScalar *dy;                                /* store a block of F(x+dx)-F(x) when J is in BAIJ format */
662:   PetscReal    error_rel;                         /* square root of relative error in computing function */
663:   PetscReal    umin;                              /* minimum allowable u'dx value */
664:   Vec          w1, w2, w3;                        /* work vectors used in computing Jacobian */
665:   PetscBool    fset;                              /* indicates that the initial function value F(X) is set */
666:   PetscErrorCode (*f)(void);                      /* function that defines Jacobian */
667:   void          *fctx;                            /* optional user-defined context for use by the function f */
668:   Vec            vscale;                          /* holds FD scaling, i.e. 1/dx for each perturbed column */
669:   PetscInt       currentcolor;                    /* color for which function evaluation is being done now */
670:   const char    *htype;                           /* "wp" or "ds" */
671:   ISColoringType ctype;                           /* IS_COLORING_GLOBAL or IS_COLORING_LOCAL */
672:   PetscInt       brows, bcols;                    /* number of block rows or columns for speedup inserting the dense matrix into sparse Jacobian */
673:   PetscBool      setupcalled;                     /* true if setup has been called */
674:   PetscBool      viewed;                          /* true if the -mat_fd_coloring_view has been triggered already */
675:   void (*ftn_func_pointer)(void), *ftn_func_cntx; /* serve the same purpose as *fortran_func_pointers in PETSc objects */
676:   PetscObjectId matid;                            /* matrix this object was created with, must always be the same */
677: };

679: typedef struct _MatColoringOps *MatColoringOps;
680: struct _MatColoringOps {
681:   PetscErrorCode (*destroy)(MatColoring);
682:   PetscErrorCode (*setfromoptions)(MatColoring, PetscOptionItems *);
683:   PetscErrorCode (*view)(MatColoring, PetscViewer);
684:   PetscErrorCode (*apply)(MatColoring, ISColoring *);
685:   PetscErrorCode (*weights)(MatColoring, PetscReal **, PetscInt **);
686: };

688: struct _p_MatColoring {
689:   PETSCHEADER(struct _MatColoringOps);
690:   Mat                   mat;
691:   PetscInt              dist;         /* distance of the coloring */
692:   PetscInt              maxcolors;    /* the maximum number of colors returned, maxcolors=1 for MIS */
693:   void                 *data;         /* inner context */
694:   PetscBool             valid;        /* check to see if what is produced is a valid coloring */
695:   MatColoringWeightType weight_type;  /* type of weight computation to be performed */
696:   PetscReal            *user_weights; /* custom weights and permutation */
697:   PetscInt             *user_lperm;
698:   PetscBool             valid_iscoloring; /* check to see if matcoloring is produced a valid iscoloring */
699: };

701: struct _p_MatTransposeColoring {
702:   PETSCHEADER(int);
703:   PetscInt       M, N, m;      /* total rows, columns; local rows */
704:   PetscInt       rstart;       /* first row owned by local processor */
705:   PetscInt       ncolors;      /* number of colors */
706:   PetscInt      *ncolumns;     /* number of local columns for a color */
707:   PetscInt      *nrows;        /* number of local rows for each color */
708:   PetscInt       currentcolor; /* color for which function evaluation is being done now */
709:   ISColoringType ctype;        /* IS_COLORING_GLOBAL or IS_COLORING_LOCAL */

711:   PetscInt *colorforrow, *colorforcol; /* pointer to rows and columns */
712:   PetscInt *rows;                      /* lists the local rows for each color (using the local row numbering) */
713:   PetscInt *den2sp;                    /* maps (row,color) in the dense matrix to index of sparse matrix array a->a */
714:   PetscInt *columns;                   /* lists the local columns of each color (using global column numbering) */
715:   PetscInt  brows;                     /* number of rows for efficient implementation of MatTransColoringApplyDenToSp() */
716:   PetscInt *lstart;                    /* array used for loop over row blocks of Csparse */
717: };

719: /*
720:    Null space context for preconditioner/operators
721: */
722: struct _p_MatNullSpace {
723:   PETSCHEADER(int);
724:   PetscBool    has_cnst;
725:   PetscInt     n;
726:   Vec         *vecs;
727:   PetscScalar *alpha;                                  /* for projections */
728:   PetscErrorCode (*remove)(MatNullSpace, Vec, void *); /* for user provided removal function */
729:   void *rmctx;                                         /* context for remove() function */
730: };

732: /*
733:    Checking zero pivot for LU, ILU preconditioners.
734: */
735: typedef struct {
736:   PetscInt    nshift, nshift_max;
737:   PetscReal   shift_amount, shift_lo, shift_hi, shift_top, shift_fraction;
738:   PetscBool   newshift;
739:   PetscReal   rs; /* active row sum of abs(off-diagonals) */
740:   PetscScalar pv; /* pivot of the active row */
741: } FactorShiftCtx;

743: PETSC_EXTERN PetscErrorCode MatTransposeCheckNonzeroState_Private(Mat, Mat);

745: /*
746:  Used by MatTranspose() and potentially other functions to track the matrix used in the generation of another matrix
747: */
748: typedef struct {
749:   PetscObjectId    id;
750:   PetscObjectState state;
751:   PetscObjectState nonzerostate;
752: } MatParentState;

754: PETSC_EXTERN PetscErrorCode MatFactorDumpMatrix(Mat);
755: PETSC_INTERN PetscErrorCode MatSetBlockSizes_Default(Mat, PetscInt, PetscInt);

757: PETSC_SINGLE_LIBRARY_INTERN PetscErrorCode MatShift_Basic(Mat, PetscScalar);

759: static inline PetscErrorCode MatPivotCheck_nz(PETSC_UNUSED Mat mat, const MatFactorInfo *info, FactorShiftCtx *sctx, PETSC_UNUSED PetscInt row)
760: {
761:   PetscReal _rs   = sctx->rs;
762:   PetscReal _zero = info->zeropivot * _rs;

764:   PetscFunctionBegin;
765:   if (PetscAbsScalar(sctx->pv) <= _zero && !PetscIsNanScalar(sctx->pv)) {
766:     /* force |diag| > zeropivot*rs */
767:     if (!sctx->nshift) sctx->shift_amount = info->shiftamount;
768:     else sctx->shift_amount *= 2.0;
769:     sctx->newshift = PETSC_TRUE;
770:     (sctx->nshift)++;
771:   } else {
772:     sctx->newshift = PETSC_FALSE;
773:   }
774:   PetscFunctionReturn(PETSC_SUCCESS);
775: }

777: static inline PetscErrorCode MatPivotCheck_pd(PETSC_UNUSED Mat mat, const MatFactorInfo *info, FactorShiftCtx *sctx, PETSC_UNUSED PetscInt row)
778: {
779:   PetscReal _rs   = sctx->rs;
780:   PetscReal _zero = info->zeropivot * _rs;

782:   PetscFunctionBegin;
783:   if (PetscRealPart(sctx->pv) <= _zero && !PetscIsNanScalar(sctx->pv)) {
784:     /* force matfactor to be diagonally dominant */
785:     if (sctx->nshift == sctx->nshift_max) {
786:       sctx->shift_fraction = sctx->shift_hi;
787:     } else {
788:       sctx->shift_lo       = sctx->shift_fraction;
789:       sctx->shift_fraction = (sctx->shift_hi + sctx->shift_lo) / (PetscReal)2.;
790:     }
791:     sctx->shift_amount = sctx->shift_fraction * sctx->shift_top;
792:     sctx->nshift++;
793:     sctx->newshift = PETSC_TRUE;
794:   } else {
795:     sctx->newshift = PETSC_FALSE;
796:   }
797:   PetscFunctionReturn(PETSC_SUCCESS);
798: }

800: static inline PetscErrorCode MatPivotCheck_inblocks(PETSC_UNUSED Mat mat, const MatFactorInfo *info, FactorShiftCtx *sctx, PETSC_UNUSED PetscInt row)
801: {
802:   PetscReal _zero = info->zeropivot;

804:   PetscFunctionBegin;
805:   if (PetscAbsScalar(sctx->pv) <= _zero && !PetscIsNanScalar(sctx->pv)) {
806:     sctx->pv += info->shiftamount;
807:     sctx->shift_amount = 0.0;
808:     sctx->nshift++;
809:   }
810:   sctx->newshift = PETSC_FALSE;
811:   PetscFunctionReturn(PETSC_SUCCESS);
812: }

814: static inline PetscErrorCode MatPivotCheck_none(Mat fact, Mat mat, const MatFactorInfo *info, FactorShiftCtx *sctx, PetscInt row)
815: {
816:   PetscReal _zero = info->zeropivot;

818:   PetscFunctionBegin;
819:   sctx->newshift = PETSC_FALSE;
820:   if (PetscAbsScalar(sctx->pv) <= _zero && !PetscIsNanScalar(sctx->pv)) {
821:     PetscCheck(!mat->erroriffailure, PETSC_COMM_SELF, PETSC_ERR_MAT_LU_ZRPVT, "Zero pivot row %" PetscInt_FMT " value %g tolerance %g", row, (double)PetscAbsScalar(sctx->pv), (double)_zero);
822:     PetscCall(PetscInfo(mat, "Detected zero pivot in factorization in row %" PetscInt_FMT " value %g tolerance %g\n", row, (double)PetscAbsScalar(sctx->pv), (double)_zero));
823:     fact->factorerrortype             = MAT_FACTOR_NUMERIC_ZEROPIVOT;
824:     fact->factorerror_zeropivot_value = PetscAbsScalar(sctx->pv);
825:     fact->factorerror_zeropivot_row   = row;
826:   }
827:   PetscFunctionReturn(PETSC_SUCCESS);
828: }

830: static inline PetscErrorCode MatPivotCheck(Mat fact, Mat mat, const MatFactorInfo *info, FactorShiftCtx *sctx, PetscInt row)
831: {
832:   PetscFunctionBegin;
833:   if (info->shifttype == (PetscReal)MAT_SHIFT_NONZERO) PetscCall(MatPivotCheck_nz(mat, info, sctx, row));
834:   else if (info->shifttype == (PetscReal)MAT_SHIFT_POSITIVE_DEFINITE) PetscCall(MatPivotCheck_pd(mat, info, sctx, row));
835:   else if (info->shifttype == (PetscReal)MAT_SHIFT_INBLOCKS) PetscCall(MatPivotCheck_inblocks(mat, info, sctx, row));
836:   else PetscCall(MatPivotCheck_none(fact, mat, info, sctx, row));
837:   PetscFunctionReturn(PETSC_SUCCESS);
838: }

840: #include <petscbt.h>
841: /*
842:   Create and initialize a linked list
843:   Input Parameters:
844:     idx_start - starting index of the list
845:     lnk_max   - max value of lnk indicating the end of the list
846:     nlnk      - max length of the list
847:   Output Parameters:
848:     lnk       - list initialized
849:     bt        - PetscBT (bitarray) with all bits set to false
850:     lnk_empty - flg indicating the list is empty
851: */
852: #define PetscLLCreate(idx_start, lnk_max, nlnk, lnk, bt) ((PetscErrorCode)(PetscMalloc1(nlnk, &lnk) || PetscBTCreate(nlnk, &(bt)) || (lnk[idx_start] = lnk_max, PETSC_SUCCESS)))

854: #define PetscLLCreate_new(idx_start, lnk_max, nlnk, lnk, bt, lnk_empty) ((PetscErrorCode)(PetscMalloc1(nlnk, &lnk) || PetscBTCreate(nlnk, &(bt)) || (lnk_empty = PETSC_TRUE, 0) || (lnk[idx_start] = lnk_max, PETSC_SUCCESS)))

856: static inline PetscErrorCode PetscLLInsertLocation_Private(PetscBool assume_sorted, PetscInt k, PetscInt idx_start, PetscInt entry, PetscInt *PETSC_RESTRICT nlnk, PetscInt *PETSC_RESTRICT lnkdata, PetscInt *PETSC_RESTRICT lnk)
857: {
858:   PetscInt location;

860:   PetscFunctionBegin;
861:   /* start from the beginning if entry < previous entry */
862:   if (!assume_sorted && k && entry < *lnkdata) *lnkdata = idx_start;
863:   /* search for insertion location */
864:   do {
865:     location = *lnkdata;
866:     *lnkdata = lnk[location];
867:   } while (entry > *lnkdata);
868:   /* insertion location is found, add entry into lnk */
869:   lnk[location] = entry;
870:   lnk[entry]    = *lnkdata;
871:   ++(*nlnk);
872:   *lnkdata = entry; /* next search starts from here if next_entry > entry */
873:   PetscFunctionReturn(PETSC_SUCCESS);
874: }

876: static inline PetscErrorCode PetscLLAdd_Private(PetscInt nidx, const PetscInt *PETSC_RESTRICT indices, PetscInt idx_start, PetscInt *PETSC_RESTRICT nlnk, PetscInt *PETSC_RESTRICT lnk, PetscBT bt, PetscBool assume_sorted)
877: {
878:   PetscFunctionBegin;
879:   *nlnk = 0;
880:   for (PetscInt k = 0, lnkdata = idx_start; k < nidx; ++k) {
881:     const PetscInt entry = indices[k];

883:     if (!PetscBTLookupSet(bt, entry)) PetscCall(PetscLLInsertLocation_Private(assume_sorted, k, idx_start, entry, nlnk, &lnkdata, lnk));
884:   }
885:   PetscFunctionReturn(PETSC_SUCCESS);
886: }

888: /*
889:   Add an index set into a sorted linked list
890:   Input Parameters:
891:     nidx      - number of input indices
892:     indices   - integer array
893:     idx_start - starting index of the list
894:     lnk       - linked list(an integer array) that is created
895:     bt        - PetscBT (bitarray), bt[idx]=true marks idx is in lnk
896:   output Parameters:
897:     nlnk      - number of newly added indices
898:     lnk       - the sorted(increasing order) linked list containing new and non-redundate entries from indices
899:     bt        - updated PetscBT (bitarray)
900: */
901: static inline PetscErrorCode PetscLLAdd(PetscInt nidx, const PetscInt *PETSC_RESTRICT indices, PetscInt idx_start, PetscInt *PETSC_RESTRICT nlnk, PetscInt *PETSC_RESTRICT lnk, PetscBT bt)
902: {
903:   PetscFunctionBegin;
904:   PetscCall(PetscLLAdd_Private(nidx, indices, idx_start, nlnk, lnk, bt, PETSC_FALSE));
905:   PetscFunctionReturn(PETSC_SUCCESS);
906: }

908: /*
909:   Add a SORTED ascending index set into a sorted linked list - same as PetscLLAdd() bus skip 'if (_k && _entry < _lnkdata) _lnkdata  = idx_start;'
910:   Input Parameters:
911:     nidx      - number of input indices
912:     indices   - sorted integer array
913:     idx_start - starting index of the list
914:     lnk       - linked list(an integer array) that is created
915:     bt        - PetscBT (bitarray), bt[idx]=true marks idx is in lnk
916:   output Parameters:
917:     nlnk      - number of newly added indices
918:     lnk       - the sorted(increasing order) linked list containing new and non-redundate entries from indices
919:     bt        - updated PetscBT (bitarray)
920: */
921: static inline PetscErrorCode PetscLLAddSorted(PetscInt nidx, const PetscInt *PETSC_RESTRICT indices, PetscInt idx_start, PetscInt *PETSC_RESTRICT nlnk, PetscInt *PETSC_RESTRICT lnk, PetscBT bt)
922: {
923:   PetscFunctionBegin;
924:   PetscCall(PetscLLAdd_Private(nidx, indices, idx_start, nlnk, lnk, bt, PETSC_TRUE));
925:   PetscFunctionReturn(PETSC_SUCCESS);
926: }

928: /*
929:   Add a permuted index set into a sorted linked list
930:   Input Parameters:
931:     nidx      - number of input indices
932:     indices   - integer array
933:     perm      - permutation of indices
934:     idx_start - starting index of the list
935:     lnk       - linked list(an integer array) that is created
936:     bt        - PetscBT (bitarray), bt[idx]=true marks idx is in lnk
937:   output Parameters:
938:     nlnk      - number of newly added indices
939:     lnk       - the sorted(increasing order) linked list containing new and non-redundate entries from indices
940:     bt        - updated PetscBT (bitarray)
941: */
942: static inline PetscErrorCode PetscLLAddPerm(PetscInt nidx, const PetscInt *PETSC_RESTRICT indices, const PetscInt *PETSC_RESTRICT perm, PetscInt idx_start, PetscInt *PETSC_RESTRICT nlnk, PetscInt *PETSC_RESTRICT lnk, PetscBT bt)
943: {
944:   PetscFunctionBegin;
945:   *nlnk = 0;
946:   for (PetscInt k = 0, lnkdata = idx_start; k < nidx; ++k) {
947:     const PetscInt entry = perm[indices[k]];

949:     if (!PetscBTLookupSet(bt, entry)) PetscCall(PetscLLInsertLocation_Private(PETSC_FALSE, k, idx_start, entry, nlnk, &lnkdata, lnk));
950:   }
951:   PetscFunctionReturn(PETSC_SUCCESS);
952: }

954: #if 0
955: /* this appears to be unused? */
956: static inline PetscErrorCode PetscLLAddSorted_new(PetscInt nidx, PetscInt *indices, PetscInt idx_start, PetscBool *lnk_empty, PetscInt *nlnk, PetscInt *lnk, PetscBT bt)
957: {
958:   PetscInt lnkdata = idx_start;

960:   PetscFunctionBegin;
961:   if (*lnk_empty) {
962:     for (PetscInt k = 0; k < nidx; ++k) {
963:       const PetscInt entry = indices[k], location = lnkdata;

965:       PetscCall(PetscBTSet(bt,entry)); /* mark the new entry */
966:       lnkdata       = lnk[location];
967:       /* insertion location is found, add entry into lnk */
968:       lnk[location] = entry;
969:       lnk[entry]    = lnkdata;
970:       lnkdata       = entry; /* next search starts from here */
971:     }
972:     /* lnk[indices[nidx-1]] = lnk[idx_start];
973:        lnk[idx_start]       = indices[0];
974:        PetscCall(PetscBTSet(bt,indices[0]));
975:        for (_k=1; _k<nidx; _k++) {
976:        PetscCall(PetscBTSet(bt,indices[_k]));
977:        lnk[indices[_k-1]] = indices[_k];
978:        }
979:     */
980:     *nlnk      = nidx;
981:     *lnk_empty = PETSC_FALSE;
982:   } else {
983:     *nlnk = 0;
984:     for (PetscInt k = 0; k < nidx; ++k) {
985:       const PetscInt entry = indices[k];

987:       if (!PetscBTLookupSet(bt,entry)) PetscCall(PetscLLInsertLocation_Private(PETSC_TRUE,k,idx_start,entry,nlnk,&lnkdata,lnk));
988:     }
989:   }
990:   PetscFunctionReturn(PETSC_SUCCESS);
991: }
992: #endif

994: /*
995:   Add a SORTED index set into a sorted linked list used for LUFactorSymbolic()
996:   Same as PetscLLAddSorted() with an additional operation:
997:        count the number of input indices that are no larger than 'diag'
998:   Input Parameters:
999:     indices   - sorted integer array
1000:     idx_start - starting index of the list, index of pivot row
1001:     lnk       - linked list(an integer array) that is created
1002:     bt        - PetscBT (bitarray), bt[idx]=true marks idx is in lnk
1003:     diag      - index of the active row in LUFactorSymbolic
1004:     nzbd      - number of input indices with indices <= idx_start
1005:     im        - im[idx_start] is initialized as num of nonzero entries in row=idx_start
1006:   output Parameters:
1007:     nlnk      - number of newly added indices
1008:     lnk       - the sorted(increasing order) linked list containing new and non-redundate entries from indices
1009:     bt        - updated PetscBT (bitarray)
1010:     im        - im[idx_start]: unchanged if diag is not an entry
1011:                              : num of entries with indices <= diag if diag is an entry
1012: */
1013: static inline PetscErrorCode PetscLLAddSortedLU(const PetscInt *PETSC_RESTRICT indices, PetscInt idx_start, PetscInt *PETSC_RESTRICT nlnk, PetscInt *PETSC_RESTRICT lnk, PetscBT bt, PetscInt diag, PetscInt nzbd, PetscInt *PETSC_RESTRICT im)
1014: {
1015:   const PetscInt nidx = im[idx_start] - nzbd; /* num of entries with idx_start < index <= diag */

1017:   PetscFunctionBegin;
1018:   *nlnk = 0;
1019:   for (PetscInt k = 0, lnkdata = idx_start; k < nidx; ++k) {
1020:     const PetscInt entry = indices[k];

1022:     ++nzbd;
1023:     if (entry == diag) im[idx_start] = nzbd;
1024:     if (!PetscBTLookupSet(bt, entry)) PetscCall(PetscLLInsertLocation_Private(PETSC_TRUE, k, idx_start, entry, nlnk, &lnkdata, lnk));
1025:   }
1026:   PetscFunctionReturn(PETSC_SUCCESS);
1027: }

1029: /*
1030:   Copy data on the list into an array, then initialize the list
1031:   Input Parameters:
1032:     idx_start - starting index of the list
1033:     lnk_max   - max value of lnk indicating the end of the list
1034:     nlnk      - number of data on the list to be copied
1035:     lnk       - linked list
1036:     bt        - PetscBT (bitarray), bt[idx]=true marks idx is in lnk
1037:   output Parameters:
1038:     indices   - array that contains the copied data
1039:     lnk       - linked list that is cleaned and initialize
1040:     bt        - PetscBT (bitarray) with all bits set to false
1041: */
1042: static inline PetscErrorCode PetscLLClean(PetscInt idx_start, PetscInt lnk_max, PetscInt nlnk, PetscInt *PETSC_RESTRICT lnk, PetscInt *PETSC_RESTRICT indices, PetscBT bt)
1043: {
1044:   PetscFunctionBegin;
1045:   for (PetscInt j = 0, idx = idx_start; j < nlnk; ++j) {
1046:     idx        = lnk[idx];
1047:     indices[j] = idx;
1048:     PetscCall(PetscBTClear(bt, idx));
1049:   }
1050:   lnk[idx_start] = lnk_max;
1051:   PetscFunctionReturn(PETSC_SUCCESS);
1052: }

1054: /*
1055:   Free memories used by the list
1056: */
1057: #define PetscLLDestroy(lnk, bt) ((PetscErrorCode)(PetscFree(lnk) || PetscBTDestroy(&(bt))))

1059: /* Routines below are used for incomplete matrix factorization */
1060: /*
1061:   Create and initialize a linked list and its levels
1062:   Input Parameters:
1063:     idx_start - starting index of the list
1064:     lnk_max   - max value of lnk indicating the end of the list
1065:     nlnk      - max length of the list
1066:   Output Parameters:
1067:     lnk       - list initialized
1068:     lnk_lvl   - array of size nlnk for storing levels of lnk
1069:     bt        - PetscBT (bitarray) with all bits set to false
1070: */
1071: #define PetscIncompleteLLCreate(idx_start, lnk_max, nlnk, lnk, lnk_lvl, bt) \
1072:   ((PetscErrorCode)(PetscIntMultError(2, nlnk, NULL) || PetscMalloc1(2 * nlnk, &lnk) || PetscBTCreate(nlnk, &(bt)) || (lnk[idx_start] = lnk_max, lnk_lvl = lnk + nlnk, PETSC_SUCCESS)))

1074: static inline PetscErrorCode PetscIncompleteLLInsertLocation_Private(PetscBool assume_sorted, PetscInt k, PetscInt idx_start, PetscInt entry, PetscInt *PETSC_RESTRICT nlnk, PetscInt *PETSC_RESTRICT lnkdata, PetscInt *PETSC_RESTRICT lnk, PetscInt *PETSC_RESTRICT lnklvl, PetscInt newval)
1075: {
1076:   PetscFunctionBegin;
1077:   PetscCall(PetscLLInsertLocation_Private(assume_sorted, k, idx_start, entry, nlnk, lnkdata, lnk));
1078:   lnklvl[entry] = newval;
1079:   PetscFunctionReturn(PETSC_SUCCESS);
1080: }

1082: /*
1083:   Initialize a sorted linked list used for ILU and ICC
1084:   Input Parameters:
1085:     nidx      - number of input idx
1086:     idx       - integer array used for storing column indices
1087:     idx_start - starting index of the list
1088:     perm      - indices of an IS
1089:     lnk       - linked list(an integer array) that is created
1090:     lnklvl    - levels of lnk
1091:     bt        - PetscBT (bitarray), bt[idx]=true marks idx is in lnk
1092:   output Parameters:
1093:     nlnk     - number of newly added idx
1094:     lnk      - the sorted(increasing order) linked list containing new and non-redundate entries from idx
1095:     lnklvl   - levels of lnk
1096:     bt       - updated PetscBT (bitarray)
1097: */
1098: static inline PetscErrorCode PetscIncompleteLLInit(PetscInt nidx, const PetscInt *PETSC_RESTRICT idx, PetscInt idx_start, const PetscInt *PETSC_RESTRICT perm, PetscInt *PETSC_RESTRICT nlnk, PetscInt *PETSC_RESTRICT lnk, PetscInt *PETSC_RESTRICT lnklvl, PetscBT bt)
1099: {
1100:   PetscFunctionBegin;
1101:   *nlnk = 0;
1102:   for (PetscInt k = 0, lnkdata = idx_start; k < nidx; ++k) {
1103:     const PetscInt entry = perm[idx[k]];

1105:     if (!PetscBTLookupSet(bt, entry)) PetscCall(PetscIncompleteLLInsertLocation_Private(PETSC_FALSE, k, idx_start, entry, nlnk, &lnkdata, lnk, lnklvl, 0));
1106:   }
1107:   PetscFunctionReturn(PETSC_SUCCESS);
1108: }

1110: static inline PetscErrorCode PetscIncompleteLLAdd_Private(PetscInt nidx, const PetscInt *PETSC_RESTRICT idx, PetscReal level, const PetscInt *PETSC_RESTRICT idxlvl, PetscInt idx_start, PetscInt *PETSC_RESTRICT nlnk, PetscInt *PETSC_RESTRICT lnk, PetscInt *PETSC_RESTRICT lnklvl, PetscBT bt, PetscInt prow_offset, PetscBool assume_sorted)
1111: {
1112:   PetscFunctionBegin;
1113:   *nlnk = 0;
1114:   for (PetscInt k = 0, lnkdata = idx_start; k < nidx; ++k) {
1115:     const PetscInt incrlev = idxlvl[k] + prow_offset + 1;

1117:     if (incrlev <= level) {
1118:       const PetscInt entry = idx[k];

1120:       if (!PetscBTLookupSet(bt, entry)) PetscCall(PetscIncompleteLLInsertLocation_Private(assume_sorted, k, idx_start, entry, nlnk, &lnkdata, lnk, lnklvl, incrlev));
1121:       else if (lnklvl[entry] > incrlev) lnklvl[entry] = incrlev; /* existing entry */
1122:     }
1123:   }
1124:   PetscFunctionReturn(PETSC_SUCCESS);
1125: }

1127: /*
1128:   Add a SORTED index set into a sorted linked list for ICC
1129:   Input Parameters:
1130:     nidx      - number of input indices
1131:     idx       - sorted integer array used for storing column indices
1132:     level     - level of fill, e.g., ICC(level)
1133:     idxlvl    - level of idx
1134:     idx_start - starting index of the list
1135:     lnk       - linked list(an integer array) that is created
1136:     lnklvl    - levels of lnk
1137:     bt        - PetscBT (bitarray), bt[idx]=true marks idx is in lnk
1138:     idxlvl_prow - idxlvl[prow], where prow is the row number of the idx
1139:   output Parameters:
1140:     nlnk   - number of newly added indices
1141:     lnk    - the sorted(increasing order) linked list containing new and non-redundate entries from idx
1142:     lnklvl - levels of lnk
1143:     bt     - updated PetscBT (bitarray)
1144:   Note: the level of U(i,j) is set as lvl(i,j) = min{ lvl(i,j), lvl(prow,i)+lvl(prow,j)+1)
1145:         where idx = non-zero columns of U(prow,prow+1:n-1), prow<i
1146: */
1147: static inline PetscErrorCode PetscICCLLAddSorted(PetscInt nidx, const PetscInt *PETSC_RESTRICT idx, PetscReal level, const PetscInt *PETSC_RESTRICT idxlvl, PetscInt idx_start, PetscInt *PETSC_RESTRICT nlnk, PetscInt *PETSC_RESTRICT lnk, PetscInt *PETSC_RESTRICT lnklvl, PetscBT bt, PetscInt idxlvl_prow)
1148: {
1149:   PetscFunctionBegin;
1150:   PetscCall(PetscIncompleteLLAdd_Private(nidx, idx, level, idxlvl, idx_start, nlnk, lnk, lnklvl, bt, idxlvl_prow, PETSC_TRUE));
1151:   PetscFunctionReturn(PETSC_SUCCESS);
1152: }

1154: /*
1155:   Add a SORTED index set into a sorted linked list for ILU
1156:   Input Parameters:
1157:     nidx      - number of input indices
1158:     idx       - sorted integer array used for storing column indices
1159:     level     - level of fill, e.g., ICC(level)
1160:     idxlvl    - level of idx
1161:     idx_start - starting index of the list
1162:     lnk       - linked list(an integer array) that is created
1163:     lnklvl    - levels of lnk
1164:     bt        - PetscBT (bitarray), bt[idx]=true marks idx is in lnk
1165:     prow      - the row number of idx
1166:   output Parameters:
1167:     nlnk     - number of newly added idx
1168:     lnk      - the sorted(increasing order) linked list containing new and non-redundate entries from idx
1169:     lnklvl   - levels of lnk
1170:     bt       - updated PetscBT (bitarray)

1172:   Note: the level of factor(i,j) is set as lvl(i,j) = min{ lvl(i,j), lvl(i,prow)+lvl(prow,j)+1)
1173:         where idx = non-zero columns of U(prow,prow+1:n-1), prow<i
1174: */
1175: static inline PetscErrorCode PetscILULLAddSorted(PetscInt nidx, const PetscInt *PETSC_RESTRICT idx, PetscInt level, const PetscInt *PETSC_RESTRICT idxlvl, PetscInt idx_start, PetscInt *PETSC_RESTRICT nlnk, PetscInt *PETSC_RESTRICT lnk, PetscInt *PETSC_RESTRICT lnklvl, PetscBT bt, PetscInt prow)
1176: {
1177:   PetscFunctionBegin;
1178:   PetscCall(PetscIncompleteLLAdd_Private(nidx, idx, level, idxlvl, idx_start, nlnk, lnk, lnklvl, bt, lnklvl[prow], PETSC_TRUE));
1179:   PetscFunctionReturn(PETSC_SUCCESS);
1180: }

1182: /*
1183:   Add a index set into a sorted linked list
1184:   Input Parameters:
1185:     nidx      - number of input idx
1186:     idx   - integer array used for storing column indices
1187:     level     - level of fill, e.g., ICC(level)
1188:     idxlvl - level of idx
1189:     idx_start - starting index of the list
1190:     lnk       - linked list(an integer array) that is created
1191:     lnklvl   - levels of lnk
1192:     bt        - PetscBT (bitarray), bt[idx]=true marks idx is in lnk
1193:   output Parameters:
1194:     nlnk      - number of newly added idx
1195:     lnk       - the sorted(increasing order) linked list containing new and non-redundate entries from idx
1196:     lnklvl   - levels of lnk
1197:     bt        - updated PetscBT (bitarray)
1198: */
1199: static inline PetscErrorCode PetscIncompleteLLAdd(PetscInt nidx, const PetscInt *PETSC_RESTRICT idx, PetscReal level, const PetscInt *PETSC_RESTRICT idxlvl, PetscInt idx_start, PetscInt *PETSC_RESTRICT nlnk, PetscInt *PETSC_RESTRICT lnk, PetscInt *PETSC_RESTRICT lnklvl, PetscBT bt)
1200: {
1201:   PetscFunctionBegin;
1202:   PetscCall(PetscIncompleteLLAdd_Private(nidx, idx, level, idxlvl, idx_start, nlnk, lnk, lnklvl, bt, 0, PETSC_FALSE));
1203:   PetscFunctionReturn(PETSC_SUCCESS);
1204: }

1206: /*
1207:   Add a SORTED index set into a sorted linked list
1208:   Input Parameters:
1209:     nidx      - number of input indices
1210:     idx   - sorted integer array used for storing column indices
1211:     level     - level of fill, e.g., ICC(level)
1212:     idxlvl - level of idx
1213:     idx_start - starting index of the list
1214:     lnk       - linked list(an integer array) that is created
1215:     lnklvl    - levels of lnk
1216:     bt        - PetscBT (bitarray), bt[idx]=true marks idx is in lnk
1217:   output Parameters:
1218:     nlnk      - number of newly added idx
1219:     lnk       - the sorted(increasing order) linked list containing new and non-redundate entries from idx
1220:     lnklvl    - levels of lnk
1221:     bt        - updated PetscBT (bitarray)
1222: */
1223: static inline PetscErrorCode PetscIncompleteLLAddSorted(PetscInt nidx, const PetscInt *PETSC_RESTRICT idx, PetscReal level, const PetscInt *PETSC_RESTRICT idxlvl, PetscInt idx_start, PetscInt *PETSC_RESTRICT nlnk, PetscInt *PETSC_RESTRICT lnk, PetscInt *PETSC_RESTRICT lnklvl, PetscBT bt)
1224: {
1225:   PetscFunctionBegin;
1226:   PetscCall(PetscIncompleteLLAdd_Private(nidx, idx, level, idxlvl, idx_start, nlnk, lnk, lnklvl, bt, 0, PETSC_TRUE));
1227:   PetscFunctionReturn(PETSC_SUCCESS);
1228: }

1230: /*
1231:   Copy data on the list into an array, then initialize the list
1232:   Input Parameters:
1233:     idx_start - starting index of the list
1234:     lnk_max   - max value of lnk indicating the end of the list
1235:     nlnk      - number of data on the list to be copied
1236:     lnk       - linked list
1237:     lnklvl    - level of lnk
1238:     bt        - PetscBT (bitarray), bt[idx]=true marks idx is in lnk
1239:   output Parameters:
1240:     indices - array that contains the copied data
1241:     lnk     - linked list that is cleaned and initialize
1242:     lnklvl  - level of lnk that is reinitialized
1243:     bt      - PetscBT (bitarray) with all bits set to false
1244: */
1245: static inline PetscErrorCode PetscIncompleteLLClean(PetscInt idx_start, PetscInt lnk_max, PetscInt nlnk, PetscInt *PETSC_RESTRICT lnk, PetscInt *PETSC_RESTRICT lnklvl, PetscInt *PETSC_RESTRICT indices, PetscInt *PETSC_RESTRICT indiceslvl, PetscBT bt)
1246: {
1247:   PetscFunctionBegin;
1248:   for (PetscInt j = 0, idx = idx_start; j < nlnk; ++j) {
1249:     idx           = lnk[idx];
1250:     indices[j]    = idx;
1251:     indiceslvl[j] = lnklvl[idx];
1252:     lnklvl[idx]   = -1;
1253:     PetscCall(PetscBTClear(bt, idx));
1254:   }
1255:   lnk[idx_start] = lnk_max;
1256:   PetscFunctionReturn(PETSC_SUCCESS);
1257: }

1259: /*
1260:   Free memories used by the list
1261: */
1262: #define PetscIncompleteLLDestroy(lnk, bt) ((PetscErrorCode)(PetscFree(lnk) || PetscBTDestroy(&(bt))))

1264: #if !defined(PETSC_CLANG_STATIC_ANALYZER)
1265:   #define MatCheckSameLocalSize(A, ar1, B, ar2) \
1266:     do { \
1267:       PetscCheckSameComm(A, ar1, B, ar2); \
1268:       PetscCheck(((A)->rmap->n == (B)->rmap->n) && ((A)->cmap->n == (B)->cmap->n), PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Incompatible matrix local sizes: parameter # %d (%" PetscInt_FMT " x %" PetscInt_FMT ") != parameter # %d (%" PetscInt_FMT " x %" PetscInt_FMT ")", ar1, \
1269:                  (A)->rmap->n, (A)->cmap->n, ar2, (B)->rmap->n, (B)->cmap->n); \
1270:     } while (0)
1271:   #define MatCheckSameSize(A, ar1, B, ar2) \
1272:     do { \
1273:       PetscCheck(((A)->rmap->N == (B)->rmap->N) && ((A)->cmap->N == (B)->cmap->N), PetscObjectComm((PetscObject)(A)), PETSC_ERR_ARG_INCOMP, "Incompatible matrix global sizes: parameter # %d (%" PetscInt_FMT " x %" PetscInt_FMT ") != parameter # %d (%" PetscInt_FMT " x %" PetscInt_FMT ")", ar1, \
1274:                  (A)->rmap->N, (A)->cmap->N, ar2, (B)->rmap->N, (B)->cmap->N); \
1275:       MatCheckSameLocalSize(A, ar1, B, ar2); \
1276:     } while (0)
1277: #else
1278: template <typename Tm>
1279: extern void MatCheckSameLocalSize(Tm, int, Tm, int);
1280: template <typename Tm>
1281: extern void MatCheckSameSize(Tm, int, Tm, int);
1282: #endif

1284: #define VecCheckMatCompatible(M, x, ar1, b, ar2) \
1285:   do { \
1286:     PetscCheck((M)->cmap->N == (x)->map->N, PetscObjectComm((PetscObject)(M)), PETSC_ERR_ARG_SIZ, "Vector global length incompatible with matrix: parameter # %d global size %" PetscInt_FMT " != matrix column global size %" PetscInt_FMT, ar1, (x)->map->N, \
1287:                (M)->cmap->N); \
1288:     PetscCheck((M)->rmap->N == (b)->map->N, PetscObjectComm((PetscObject)(M)), PETSC_ERR_ARG_SIZ, "Vector global length incompatible with matrix: parameter # %d global size %" PetscInt_FMT " != matrix row global size %" PetscInt_FMT, ar2, (b)->map->N, \
1289:                (M)->rmap->N); \
1290:   } while (0)

1292: /* -------------------------------------------------------------------------------------------------------*/
1293: /*
1294:   Create and initialize a condensed linked list -
1295:     same as PetscLLCreate(), but uses a scalable array 'lnk' with size of max number of entries, not O(N).
1296:     Barry suggested this approach (Dec. 6, 2011):
1297:       I've thought of an alternative way of representing a linked list that is efficient but doesn't have the O(N) scaling issue
1298:       (it may be faster than the O(N) even sequentially due to less crazy memory access).

1300:       Instead of having some like  a  2  -> 4 -> 11 ->  22  list that uses slot 2  4 11 and 22 in a big array use a small array with two slots
1301:       for each entry for example  [ 2 1 | 4 3 | 22 -1 | 11 2]   so the first number (of the pair) is the value while the second tells you where
1302:       in the list the next entry is. Inserting a new link means just append another pair at the end. For example say we want to insert 13 into the
1303:       list it would then become [2 1 | 4 3 | 22 -1 | 11 4 | 13 2 ] you just add a pair at the end and fix the point for the one that points to it.
1304:       That is 11 use to point to the 2 slot, after the change 11 points to the 4th slot which has the value 13. Note that values are always next
1305:       to each other so memory access is much better than using the big array.

1307:   Example:
1308:      nlnk_max=5, lnk_max=36:
1309:      Initial list: [0, 0 | 36, 2 | 0, 0 | 0, 0 | 0, 0 | 0, 0 | 0, 0]
1310:      here, head_node has index 2 with value lnk[2]=lnk_max=36,
1311:            0-th entry is used to store the number of entries in the list,
1312:      The initial lnk represents head -> tail(marked by 36) with number of entries = lnk[0]=0.

1314:      Now adding a sorted set {2,4}, the list becomes
1315:      [2, 0 | 36, 4 |2, 6 | 4, 2 | 0, 0 | 0, 0 | 0, 0 ]
1316:      represents head -> 2 -> 4 -> tail with number of entries = lnk[0]=2.

1318:      Then adding a sorted set {0,3,35}, the list
1319:      [5, 0 | 36, 8 | 2, 10 | 4, 12 | 0, 4 | 3, 6 | 35, 2 ]
1320:      represents head -> 0 -> 2 -> 3 -> 4 -> 35 -> tail with number of entries = lnk[0]=5.

1322:   Input Parameters:
1323:     nlnk_max  - max length of the list
1324:     lnk_max   - max value of the entries
1325:   Output Parameters:
1326:     lnk       - list created and initialized
1327:     bt        - PetscBT (bitarray) with all bits set to false. Note: bt has size lnk_max, not nln_max!
1328: */
1329: static inline PetscErrorCode PetscLLCondensedCreate(PetscInt nlnk_max, PetscInt lnk_max, PetscInt **lnk, PetscBT *bt)
1330: {
1331:   PetscInt *llnk, lsize = 0;

1333:   PetscFunctionBegin;
1334:   PetscCall(PetscIntMultError(2, nlnk_max + 2, &lsize));
1335:   PetscCall(PetscMalloc1(lsize, lnk));
1336:   PetscCall(PetscBTCreate(lnk_max, bt));
1337:   llnk    = *lnk;
1338:   llnk[0] = 0;       /* number of entries on the list */
1339:   llnk[2] = lnk_max; /* value in the head node */
1340:   llnk[3] = 2;       /* next for the head node */
1341:   PetscFunctionReturn(PETSC_SUCCESS);
1342: }

1344: /*
1345:   Add a SORTED ascending index set into a sorted linked list. See PetscLLCondensedCreate() for detailed description.
1346:   Input Parameters:
1347:     nidx      - number of input indices
1348:     indices   - sorted integer array
1349:     lnk       - condensed linked list(an integer array) that is created
1350:     bt        - PetscBT (bitarray), bt[idx]=true marks idx is in lnk
1351:   output Parameters:
1352:     lnk       - the sorted(increasing order) linked list containing previous and newly added non-redundate indices
1353:     bt        - updated PetscBT (bitarray)
1354: */
1355: static inline PetscErrorCode PetscLLCondensedAddSorted(PetscInt nidx, const PetscInt indices[], PetscInt lnk[], PetscBT bt)
1356: {
1357:   PetscInt location = 2;      /* head */
1358:   PetscInt nlnk     = lnk[0]; /* num of entries on the input lnk */

1360:   PetscFunctionBegin;
1361:   for (PetscInt k = 0; k < nidx; k++) {
1362:     const PetscInt entry = indices[k];
1363:     if (!PetscBTLookupSet(bt, entry)) { /* new entry */
1364:       PetscInt next, lnkdata;

1366:       /* search for insertion location */
1367:       do {
1368:         next     = location + 1;  /* link from previous node to next node */
1369:         location = lnk[next];     /* idx of next node */
1370:         lnkdata  = lnk[location]; /* value of next node */
1371:       } while (entry > lnkdata);
1372:       /* insertion location is found, add entry into lnk */
1373:       const PetscInt newnode = 2 * (nlnk + 2); /* index for this new node */
1374:       lnk[next]              = newnode;        /* connect previous node to the new node */
1375:       lnk[newnode]           = entry;          /* set value of the new node */
1376:       lnk[newnode + 1]       = location;       /* connect new node to next node */
1377:       location               = newnode;        /* next search starts from the new node */
1378:       nlnk++;
1379:     }
1380:   }
1381:   lnk[0] = nlnk; /* number of entries in the list */
1382:   PetscFunctionReturn(PETSC_SUCCESS);
1383: }

1385: static inline PetscErrorCode PetscLLCondensedClean(PetscInt lnk_max, PETSC_UNUSED PetscInt nidx, PetscInt *indices, PetscInt lnk[], PetscBT bt)
1386: {
1387:   const PetscInt nlnk = lnk[0]; /* num of entries on the list */
1388:   PetscInt       next = lnk[3]; /* head node */

1390:   PetscFunctionBegin;
1391:   for (PetscInt k = 0; k < nlnk; k++) {
1392:     indices[k] = lnk[next];
1393:     next       = lnk[next + 1];
1394:     PetscCall(PetscBTClear(bt, indices[k]));
1395:   }
1396:   lnk[0] = 0;       /* num of entries on the list */
1397:   lnk[2] = lnk_max; /* initialize head node */
1398:   lnk[3] = 2;       /* head node */
1399:   PetscFunctionReturn(PETSC_SUCCESS);
1400: }

1402: static inline PetscErrorCode PetscLLCondensedView(PetscInt *lnk)
1403: {
1404:   PetscFunctionBegin;
1405:   PetscCall(PetscPrintf(PETSC_COMM_SELF, "LLCondensed of size %" PetscInt_FMT ", (val,  next)\n", lnk[0]));
1406:   for (PetscInt k = 2; k < lnk[0] + 2; ++k) PetscCall(PetscPrintf(PETSC_COMM_SELF, " %" PetscInt_FMT ": (%" PetscInt_FMT ", %" PetscInt_FMT ")\n", 2 * k, lnk[2 * k], lnk[2 * k + 1]));
1407:   PetscFunctionReturn(PETSC_SUCCESS);
1408: }

1410: /*
1411:   Free memories used by the list
1412: */
1413: static inline PetscErrorCode PetscLLCondensedDestroy(PetscInt *lnk, PetscBT bt)
1414: {
1415:   PetscFunctionBegin;
1416:   PetscCall(PetscFree(lnk));
1417:   PetscCall(PetscBTDestroy(&bt));
1418:   PetscFunctionReturn(PETSC_SUCCESS);
1419: }

1421: /* -------------------------------------------------------------------------------------------------------*/
1422: /*
1423:  Same as PetscLLCondensedCreate(), but does not use non-scalable O(lnk_max) bitarray
1424:   Input Parameters:
1425:     nlnk_max  - max length of the list
1426:   Output Parameters:
1427:     lnk       - list created and initialized
1428: */
1429: static inline PetscErrorCode PetscLLCondensedCreate_Scalable(PetscInt nlnk_max, PetscInt **lnk)
1430: {
1431:   PetscInt *llnk, lsize = 0;

1433:   PetscFunctionBegin;
1434:   PetscCall(PetscIntMultError(2, nlnk_max + 2, &lsize));
1435:   PetscCall(PetscMalloc1(lsize, lnk));
1436:   llnk    = *lnk;
1437:   llnk[0] = 0;             /* number of entries on the list */
1438:   llnk[2] = PETSC_MAX_INT; /* value in the head node */
1439:   llnk[3] = 2;             /* next for the head node */
1440:   PetscFunctionReturn(PETSC_SUCCESS);
1441: }

1443: static inline PetscErrorCode PetscLLCondensedExpand_Scalable(PetscInt nlnk_max, PetscInt **lnk)
1444: {
1445:   PetscInt lsize = 0;

1447:   PetscFunctionBegin;
1448:   PetscCall(PetscIntMultError(2, nlnk_max + 2, &lsize));
1449:   PetscCall(PetscRealloc(lsize * sizeof(PetscInt), lnk));
1450:   PetscFunctionReturn(PETSC_SUCCESS);
1451: }

1453: static inline PetscErrorCode PetscLLCondensedAddSorted_Scalable(PetscInt nidx, const PetscInt indices[], PetscInt lnk[])
1454: {
1455:   PetscInt location = 2;      /* head */
1456:   PetscInt nlnk     = lnk[0]; /* num of entries on the input lnk */

1458:   for (PetscInt k = 0; k < nidx; k++) {
1459:     const PetscInt entry = indices[k];
1460:     PetscInt       next, lnkdata;

1462:     /* search for insertion location */
1463:     do {
1464:       next     = location + 1;  /* link from previous node to next node */
1465:       location = lnk[next];     /* idx of next node */
1466:       lnkdata  = lnk[location]; /* value of next node */
1467:     } while (entry > lnkdata);
1468:     if (entry < lnkdata) {
1469:       /* insertion location is found, add entry into lnk */
1470:       const PetscInt newnode = 2 * (nlnk + 2); /* index for this new node */
1471:       lnk[next]              = newnode;        /* connect previous node to the new node */
1472:       lnk[newnode]           = entry;          /* set value of the new node */
1473:       lnk[newnode + 1]       = location;       /* connect new node to next node */
1474:       location               = newnode;        /* next search starts from the new node */
1475:       nlnk++;
1476:     }
1477:   }
1478:   lnk[0] = nlnk; /* number of entries in the list */
1479:   return PETSC_SUCCESS;
1480: }

1482: static inline PetscErrorCode PetscLLCondensedClean_Scalable(PETSC_UNUSED PetscInt nidx, PetscInt *indices, PetscInt *lnk)
1483: {
1484:   const PetscInt nlnk = lnk[0];
1485:   PetscInt       next = lnk[3]; /* head node */

1487:   for (PetscInt k = 0; k < nlnk; k++) {
1488:     indices[k] = lnk[next];
1489:     next       = lnk[next + 1];
1490:   }
1491:   lnk[0] = 0; /* num of entries on the list */
1492:   lnk[3] = 2; /* head node */
1493:   return PETSC_SUCCESS;
1494: }

1496: static inline PetscErrorCode PetscLLCondensedDestroy_Scalable(PetscInt *lnk)
1497: {
1498:   return PetscFree(lnk);
1499: }

1501: /* -------------------------------------------------------------------------------------------------------*/
1502: /*
1503:       lnk[0]   number of links
1504:       lnk[1]   number of entries
1505:       lnk[3n]  value
1506:       lnk[3n+1] len
1507:       lnk[3n+2] link to next value

1509:       The next three are always the first link

1511:       lnk[3]    PETSC_MIN_INT+1
1512:       lnk[4]    1
1513:       lnk[5]    link to first real entry

1515:       The next three are always the last link

1517:       lnk[6]    PETSC_MAX_INT - 1
1518:       lnk[7]    1
1519:       lnk[8]    next valid link (this is the same as lnk[0] but without the decreases)
1520: */

1522: static inline PetscErrorCode PetscLLCondensedCreate_fast(PetscInt nlnk_max, PetscInt **lnk)
1523: {
1524:   PetscInt *llnk;
1525:   PetscInt  lsize = 0;

1527:   PetscFunctionBegin;
1528:   PetscCall(PetscIntMultError(3, nlnk_max + 3, &lsize));
1529:   PetscCall(PetscMalloc1(lsize, lnk));
1530:   llnk    = *lnk;
1531:   llnk[0] = 0;                 /* nlnk: number of entries on the list */
1532:   llnk[1] = 0;                 /* number of integer entries represented in list */
1533:   llnk[3] = PETSC_MIN_INT + 1; /* value in the first node */
1534:   llnk[4] = 1;                 /* count for the first node */
1535:   llnk[5] = 6;                 /* next for the first node */
1536:   llnk[6] = PETSC_MAX_INT - 1; /* value in the last node */
1537:   llnk[7] = 1;                 /* count for the last node */
1538:   llnk[8] = 0;                 /* next valid node to be used */
1539:   PetscFunctionReturn(PETSC_SUCCESS);
1540: }

1542: static inline PetscErrorCode PetscLLCondensedAddSorted_fast(PetscInt nidx, const PetscInt indices[], PetscInt lnk[])
1543: {
1544:   for (PetscInt k = 0, prev = 3 /* first value */; k < nidx; k++) {
1545:     const PetscInt entry = indices[k];
1546:     PetscInt       next  = lnk[prev + 2];

1548:     /* search for insertion location */
1549:     while (entry >= lnk[next]) {
1550:       prev = next;
1551:       next = lnk[next + 2];
1552:     }
1553:     /* entry is in range of previous list */
1554:     if (entry < lnk[prev] + lnk[prev + 1]) continue;
1555:     lnk[1]++;
1556:     /* entry is right after previous list */
1557:     if (entry == lnk[prev] + lnk[prev + 1]) {
1558:       lnk[prev + 1]++;
1559:       if (lnk[next] == entry + 1) { /* combine two contiguous strings */
1560:         lnk[prev + 1] += lnk[next + 1];
1561:         lnk[prev + 2] = lnk[next + 2];
1562:         next          = lnk[next + 2];
1563:         lnk[0]--;
1564:       }
1565:       continue;
1566:     }
1567:     /* entry is right before next list */
1568:     if (entry == lnk[next] - 1) {
1569:       lnk[next]--;
1570:       lnk[next + 1]++;
1571:       prev = next;
1572:       next = lnk[prev + 2];
1573:       continue;
1574:     }
1575:     /*  add entry into lnk */
1576:     lnk[prev + 2] = 3 * ((lnk[8]++) + 3); /* connect previous node to the new node */
1577:     prev          = lnk[prev + 2];
1578:     lnk[prev]     = entry; /* set value of the new node */
1579:     lnk[prev + 1] = 1;     /* number of values in contiguous string is one to start */
1580:     lnk[prev + 2] = next;  /* connect new node to next node */
1581:     lnk[0]++;
1582:   }
1583:   return PETSC_SUCCESS;
1584: }

1586: static inline PetscErrorCode PetscLLCondensedClean_fast(PETSC_UNUSED PetscInt nidx, PetscInt *indices, PetscInt *lnk)
1587: {
1588:   const PetscInt nlnk = lnk[0];
1589:   PetscInt       next = lnk[5]; /* first node */

1591:   for (PetscInt k = 0, cnt = 0; k < nlnk; k++) {
1592:     for (PetscInt j = 0; j < lnk[next + 1]; j++) indices[cnt++] = lnk[next] + j;
1593:     next = lnk[next + 2];
1594:   }
1595:   lnk[0] = 0;                 /* nlnk: number of links */
1596:   lnk[1] = 0;                 /* number of integer entries represented in list */
1597:   lnk[3] = PETSC_MIN_INT + 1; /* value in the first node */
1598:   lnk[4] = 1;                 /* count for the first node */
1599:   lnk[5] = 6;                 /* next for the first node */
1600:   lnk[6] = PETSC_MAX_INT - 1; /* value in the last node */
1601:   lnk[7] = 1;                 /* count for the last node */
1602:   lnk[8] = 0;                 /* next valid location to make link */
1603:   return PETSC_SUCCESS;
1604: }

1606: static inline PetscErrorCode PetscLLCondensedView_fast(const PetscInt *lnk)
1607: {
1608:   const PetscInt nlnk = lnk[0];
1609:   PetscInt       next = lnk[5]; /* first node */

1611:   for (PetscInt k = 0; k < nlnk; k++) {
1612: #if 0 /* Debugging code */
1613:     printf("%d value %d len %d next %d\n", next, lnk[next], lnk[next + 1], lnk[next + 2]);
1614: #endif
1615:     next = lnk[next + 2];
1616:   }
1617:   return PETSC_SUCCESS;
1618: }

1620: static inline PetscErrorCode PetscLLCondensedDestroy_fast(PetscInt *lnk)
1621: {
1622:   return PetscFree(lnk);
1623: }

1625: PETSC_EXTERN PetscErrorCode PetscCDCreate(PetscInt, PetscCoarsenData **);
1626: PETSC_EXTERN PetscErrorCode PetscCDDestroy(PetscCoarsenData *);
1627: PETSC_EXTERN PetscErrorCode PetscCDIntNdSetID(PetscCDIntNd *, PetscInt);
1628: PETSC_EXTERN PetscErrorCode PetscCDIntNdGetID(const PetscCDIntNd *, PetscInt *);
1629: PETSC_EXTERN PetscErrorCode PetscCDAppendID(PetscCoarsenData *, PetscInt, PetscInt);
1630: PETSC_EXTERN PetscErrorCode PetscCDMoveAppend(PetscCoarsenData *, PetscInt, PetscInt);
1631: PETSC_EXTERN PetscErrorCode PetscCDAppendNode(PetscCoarsenData *, PetscInt, PetscCDIntNd *);
1632: PETSC_EXTERN PetscErrorCode PetscCDRemoveNextNode(PetscCoarsenData *, PetscInt, PetscCDIntNd *);
1633: PETSC_EXTERN PetscErrorCode PetscCDCountAt(const PetscCoarsenData *, PetscInt, PetscInt *);
1634: PETSC_EXTERN PetscErrorCode PetscCDIsEmptyAt(const PetscCoarsenData *, PetscInt, PetscBool *);
1635: PETSC_EXTERN PetscErrorCode PetscCDSetChunkSize(PetscCoarsenData *, PetscInt);
1636: PETSC_EXTERN PetscErrorCode PetscCDPrint(const PetscCoarsenData *, PetscInt, MPI_Comm);
1637: PETSC_EXTERN PetscErrorCode PetscCDGetNonemptyIS(PetscCoarsenData *, IS *);
1638: PETSC_EXTERN PetscErrorCode PetscCDGetMat(PetscCoarsenData *, Mat *);
1639: PETSC_EXTERN PetscErrorCode PetscCDSetMat(PetscCoarsenData *, Mat);
1640: PETSC_EXTERN PetscErrorCode PetscCDClearMat(PetscCoarsenData *);
1641: PETSC_EXTERN PetscErrorCode PetscCDRemoveAllAt(PetscCoarsenData *, PetscInt);
1642: PETSC_EXTERN PetscErrorCode PetscCDCount(const PetscCoarsenData *, PetscInt *_sz);

1644: PETSC_EXTERN PetscErrorCode PetscCDGetHeadPos(const PetscCoarsenData *, PetscInt, PetscCDIntNd **);
1645: PETSC_EXTERN PetscErrorCode PetscCDGetNextPos(const PetscCoarsenData *, PetscInt, PetscCDIntNd **);
1646: PETSC_EXTERN PetscErrorCode PetscCDGetASMBlocks(const PetscCoarsenData *, const PetscInt, PetscInt *, IS **);

1648: /* this is extern because it is used in MatFDColoringUseDM() which is in the DM library */
1649: PETSC_EXTERN PetscErrorCode MatFDColoringApply_AIJ(Mat, MatFDColoring, Vec, void *);

1651: PETSC_EXTERN PetscLogEvent MAT_Mult;
1652: PETSC_EXTERN PetscLogEvent MAT_MultAdd;
1653: PETSC_EXTERN PetscLogEvent MAT_MultTranspose;
1654: PETSC_EXTERN PetscLogEvent MAT_MultHermitianTranspose;
1655: PETSC_EXTERN PetscLogEvent MAT_MultTransposeAdd;
1656: PETSC_EXTERN PetscLogEvent MAT_MultHermitianTransposeAdd;
1657: PETSC_EXTERN PetscLogEvent MAT_Solve;
1658: PETSC_EXTERN PetscLogEvent MAT_Solves;
1659: PETSC_EXTERN PetscLogEvent MAT_SolveAdd;
1660: PETSC_EXTERN PetscLogEvent MAT_SolveTranspose;
1661: PETSC_EXTERN PetscLogEvent MAT_SolveTransposeAdd;
1662: PETSC_EXTERN PetscLogEvent MAT_SOR;
1663: PETSC_EXTERN PetscLogEvent MAT_ForwardSolve;
1664: PETSC_EXTERN PetscLogEvent MAT_BackwardSolve;
1665: PETSC_EXTERN PetscLogEvent MAT_LUFactor;
1666: PETSC_EXTERN PetscLogEvent MAT_LUFactorSymbolic;
1667: PETSC_EXTERN PetscLogEvent MAT_LUFactorNumeric;
1668: PETSC_EXTERN PetscLogEvent MAT_QRFactor;
1669: PETSC_EXTERN PetscLogEvent MAT_QRFactorSymbolic;
1670: PETSC_EXTERN PetscLogEvent MAT_QRFactorNumeric;
1671: PETSC_EXTERN PetscLogEvent MAT_CholeskyFactor;
1672: PETSC_EXTERN PetscLogEvent MAT_CholeskyFactorSymbolic;
1673: PETSC_EXTERN PetscLogEvent MAT_CholeskyFactorNumeric;
1674: PETSC_EXTERN PetscLogEvent MAT_ILUFactor;
1675: PETSC_EXTERN PetscLogEvent MAT_ILUFactorSymbolic;
1676: PETSC_EXTERN PetscLogEvent MAT_ICCFactorSymbolic;
1677: PETSC_EXTERN PetscLogEvent MAT_Copy;
1678: PETSC_EXTERN PetscLogEvent MAT_Convert;
1679: PETSC_EXTERN PetscLogEvent MAT_Scale;
1680: PETSC_EXTERN PetscLogEvent MAT_AssemblyBegin;
1681: PETSC_EXTERN PetscLogEvent MAT_AssemblyEnd;
1682: PETSC_EXTERN PetscLogEvent MAT_SetValues;
1683: PETSC_EXTERN PetscLogEvent MAT_GetValues;
1684: PETSC_EXTERN PetscLogEvent MAT_GetRow;
1685: PETSC_EXTERN PetscLogEvent MAT_GetRowIJ;
1686: PETSC_EXTERN PetscLogEvent MAT_CreateSubMats;
1687: PETSC_EXTERN PetscLogEvent MAT_GetOrdering;
1688: PETSC_EXTERN PetscLogEvent MAT_RedundantMat;
1689: PETSC_EXTERN PetscLogEvent MAT_IncreaseOverlap;
1690: PETSC_EXTERN PetscLogEvent MAT_Partitioning;
1691: PETSC_EXTERN PetscLogEvent MAT_PartitioningND;
1692: PETSC_EXTERN PetscLogEvent MAT_Coarsen;
1693: PETSC_EXTERN PetscLogEvent MAT_ZeroEntries;
1694: PETSC_EXTERN PetscLogEvent MAT_Load;
1695: PETSC_EXTERN PetscLogEvent MAT_View;
1696: PETSC_EXTERN PetscLogEvent MAT_AXPY;
1697: PETSC_EXTERN PetscLogEvent MAT_FDColoringCreate;
1698: PETSC_EXTERN PetscLogEvent MAT_TransposeColoringCreate;
1699: PETSC_EXTERN PetscLogEvent MAT_FDColoringSetUp;
1700: PETSC_EXTERN PetscLogEvent MAT_FDColoringApply;
1701: PETSC_EXTERN PetscLogEvent MAT_Transpose;
1702: PETSC_EXTERN PetscLogEvent MAT_FDColoringFunction;
1703: PETSC_EXTERN PetscLogEvent MAT_CreateSubMat;
1704: PETSC_EXTERN PetscLogEvent MAT_MatSolve;
1705: PETSC_EXTERN PetscLogEvent MAT_MatTrSolve;
1706: PETSC_EXTERN PetscLogEvent MAT_MatMultSymbolic;
1707: PETSC_EXTERN PetscLogEvent MAT_MatMultNumeric;
1708: PETSC_EXTERN PetscLogEvent MAT_Getlocalmatcondensed;
1709: PETSC_EXTERN PetscLogEvent MAT_GetBrowsOfAcols;
1710: PETSC_EXTERN PetscLogEvent MAT_GetBrowsOfAocols;
1711: PETSC_EXTERN PetscLogEvent MAT_PtAPSymbolic;
1712: PETSC_EXTERN PetscLogEvent MAT_PtAPNumeric;
1713: PETSC_EXTERN PetscLogEvent MAT_Seqstompinum;
1714: PETSC_EXTERN PetscLogEvent MAT_Seqstompisym;
1715: PETSC_EXTERN PetscLogEvent MAT_Seqstompi;
1716: PETSC_EXTERN PetscLogEvent MAT_Getlocalmat;
1717: PETSC_EXTERN PetscLogEvent MAT_RARtSymbolic;
1718: PETSC_EXTERN PetscLogEvent MAT_RARtNumeric;
1719: PETSC_EXTERN PetscLogEvent MAT_MatTransposeMultSymbolic;
1720: PETSC_EXTERN PetscLogEvent MAT_MatTransposeMultNumeric;
1721: PETSC_EXTERN PetscLogEvent MAT_TransposeMatMultSymbolic;
1722: PETSC_EXTERN PetscLogEvent MAT_TransposeMatMultNumeric;
1723: PETSC_EXTERN PetscLogEvent MAT_MatMatMultSymbolic;
1724: PETSC_EXTERN PetscLogEvent MAT_MatMatMultNumeric;
1725: PETSC_EXTERN PetscLogEvent MAT_Getsymtransreduced;
1726: PETSC_EXTERN PetscLogEvent MAT_GetSeqNonzeroStructure;
1727: PETSC_EXTERN PetscLogEvent MATMFFD_Mult;
1728: PETSC_EXTERN PetscLogEvent MAT_GetMultiProcBlock;
1729: PETSC_EXTERN PetscLogEvent MAT_CUSPARSECopyToGPU;
1730: PETSC_EXTERN PetscLogEvent MAT_CUSPARSECopyFromGPU;
1731: PETSC_EXTERN PetscLogEvent MAT_CUSPARSEGenerateTranspose;
1732: PETSC_EXTERN PetscLogEvent MAT_CUSPARSESolveAnalysis;
1733: PETSC_EXTERN PetscLogEvent MAT_HIPSPARSECopyToGPU;
1734: PETSC_EXTERN PetscLogEvent MAT_HIPSPARSECopyFromGPU;
1735: PETSC_EXTERN PetscLogEvent MAT_HIPSPARSEGenerateTranspose;
1736: PETSC_EXTERN PetscLogEvent MAT_HIPSPARSESolveAnalysis;
1737: PETSC_EXTERN PetscLogEvent MAT_SetValuesBatch;
1738: PETSC_EXTERN PetscLogEvent MAT_ViennaCLCopyToGPU;
1739: PETSC_EXTERN PetscLogEvent MAT_DenseCopyToGPU;
1740: PETSC_EXTERN PetscLogEvent MAT_DenseCopyFromGPU;
1741: PETSC_EXTERN PetscLogEvent MAT_Merge;
1742: PETSC_EXTERN PetscLogEvent MAT_Residual;
1743: PETSC_EXTERN PetscLogEvent MAT_SetRandom;
1744: PETSC_EXTERN PetscLogEvent MAT_FactorFactS;
1745: PETSC_EXTERN PetscLogEvent MAT_FactorInvS;
1746: PETSC_EXTERN PetscLogEvent MAT_PreallCOO;
1747: PETSC_EXTERN PetscLogEvent MAT_SetVCOO;
1748: PETSC_EXTERN PetscLogEvent MATCOLORING_Apply;
1749: PETSC_EXTERN PetscLogEvent MATCOLORING_Comm;
1750: PETSC_EXTERN PetscLogEvent MATCOLORING_Local;
1751: PETSC_EXTERN PetscLogEvent MATCOLORING_ISCreate;
1752: PETSC_EXTERN PetscLogEvent MATCOLORING_SetUp;
1753: PETSC_EXTERN PetscLogEvent MATCOLORING_Weights;
1754: PETSC_EXTERN PetscLogEvent MAT_H2Opus_Build;
1755: PETSC_EXTERN PetscLogEvent MAT_H2Opus_Compress;
1756: PETSC_EXTERN PetscLogEvent MAT_H2Opus_Orthog;
1757: PETSC_EXTERN PetscLogEvent MAT_H2Opus_LR;
1758: PETSC_EXTERN PetscLogEvent MAT_CUDACopyToGPU;
1759: PETSC_EXTERN PetscLogEvent MAT_HIPCopyToGPU;