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 MatMeshToCellGraphRegisterAllCalled;
13: PETSC_EXTERN PetscBool MatCoarsenRegisterAllCalled;
14: PETSC_EXTERN PetscErrorCode MatRegisterAll(void);
15: PETSC_EXTERN PetscErrorCode MatOrderingRegisterAll(void);
16: PETSC_EXTERN PetscErrorCode MatColoringRegisterAll(void);
17: PETSC_EXTERN PetscErrorCode MatPartitioningRegisterAll(void);
18: PETSC_EXTERN PetscErrorCode MatMeshToCellGraphRegisterAll(void);
19: PETSC_EXTERN PetscErrorCode MatCoarsenRegisterAll(void);
20: PETSC_EXTERN PetscErrorCode MatSeqAIJRegisterAll(void);
22: /* Gets the root type of the input matrix's type (e.g., MATAIJ for MATSEQAIJ) */
23: PETSC_EXTERN PetscErrorCode MatGetRootType_Private(Mat, MatType *);
25: /* Gets the MPI type corresponding to the input matrix's type (e.g., MATMPIAIJ for MATSEQAIJ) */
26: PETSC_INTERN PetscErrorCode MatGetMPIMatType_Private(Mat, MatType *);
28: /*
29: This file defines the parts of the matrix data structure that are
30: shared by all matrix types.
31: */
33: /*
34: If you add entries here also add them to the MATOP enum
35: in include/petscmat.h
36: */
37: typedef struct _MatOps *MatOps;
38: struct _MatOps {
39: /* 0*/
40: PetscErrorCode (*setvalues)(Mat, PetscInt, const PetscInt[], PetscInt, const PetscInt[], const PetscScalar[], InsertMode);
41: PetscErrorCode (*getrow)(Mat, PetscInt, PetscInt *, PetscInt *[], PetscScalar *[]);
42: PetscErrorCode (*restorerow)(Mat, PetscInt, PetscInt *, PetscInt *[], PetscScalar *[]);
43: PetscErrorCode (*mult)(Mat, Vec, Vec);
44: PetscErrorCode (*multadd)(Mat, Vec, Vec, Vec);
45: /* 5*/
46: PetscErrorCode (*multtranspose)(Mat, Vec, Vec);
47: PetscErrorCode (*multtransposeadd)(Mat, Vec, Vec, Vec);
48: PetscErrorCode (*solve)(Mat, Vec, Vec);
49: PetscErrorCode (*solveadd)(Mat, Vec, Vec, Vec);
50: PetscErrorCode (*solvetranspose)(Mat, Vec, Vec);
51: /*10*/
52: PetscErrorCode (*solvetransposeadd)(Mat, Vec, Vec, Vec);
53: PetscErrorCode (*lufactor)(Mat, IS, IS, const MatFactorInfo *);
54: PetscErrorCode (*choleskyfactor)(Mat, IS, const MatFactorInfo *);
55: PetscErrorCode (*sor)(Mat, Vec, PetscReal, MatSORType, PetscReal, PetscInt, PetscInt, Vec);
56: PetscErrorCode (*transpose)(Mat, MatReuse, Mat *);
57: /*15*/
58: PetscErrorCode (*getinfo)(Mat, MatInfoType, MatInfo *);
59: PetscErrorCode (*equal)(Mat, Mat, PetscBool *);
60: PetscErrorCode (*getdiagonal)(Mat, Vec);
61: PetscErrorCode (*diagonalscale)(Mat, Vec, Vec);
62: PetscErrorCode (*norm)(Mat, NormType, PetscReal *);
63: /*20*/
64: PetscErrorCode (*assemblybegin)(Mat, MatAssemblyType);
65: PetscErrorCode (*assemblyend)(Mat, MatAssemblyType);
66: PetscErrorCode (*setoption)(Mat, MatOption, PetscBool);
67: PetscErrorCode (*zeroentries)(Mat);
68: /*24*/
69: PetscErrorCode (*zerorows)(Mat, PetscInt, const PetscInt[], PetscScalar, Vec, Vec);
70: PetscErrorCode (*lufactorsymbolic)(Mat, Mat, IS, IS, const MatFactorInfo *);
71: PetscErrorCode (*lufactornumeric)(Mat, Mat, const MatFactorInfo *);
72: PetscErrorCode (*choleskyfactorsymbolic)(Mat, Mat, IS, const MatFactorInfo *);
73: PetscErrorCode (*choleskyfactornumeric)(Mat, Mat, const MatFactorInfo *);
74: /*29*/
75: PetscErrorCode (*setup)(Mat);
76: PetscErrorCode (*ilufactorsymbolic)(Mat, Mat, IS, IS, const MatFactorInfo *);
77: PetscErrorCode (*iccfactorsymbolic)(Mat, Mat, IS, const MatFactorInfo *);
78: PetscErrorCode (*getdiagonalblock)(Mat, Mat *);
79: PetscErrorCode (*setinf)(Mat);
80: /*34*/
81: PetscErrorCode (*duplicate)(Mat, MatDuplicateOption, Mat *);
82: PetscErrorCode (*forwardsolve)(Mat, Vec, Vec);
83: PetscErrorCode (*backwardsolve)(Mat, Vec, Vec);
84: PetscErrorCode (*ilufactor)(Mat, IS, IS, const MatFactorInfo *);
85: PetscErrorCode (*iccfactor)(Mat, IS, const MatFactorInfo *);
86: /*39*/
87: PetscErrorCode (*axpy)(Mat, PetscScalar, Mat, MatStructure);
88: PetscErrorCode (*createsubmatrices)(Mat, PetscInt, const IS[], const IS[], MatReuse, Mat *[]);
89: PetscErrorCode (*increaseoverlap)(Mat, PetscInt, IS[], PetscInt);
90: PetscErrorCode (*getvalues)(Mat, PetscInt, const PetscInt[], PetscInt, const PetscInt[], PetscScalar[]);
91: PetscErrorCode (*copy)(Mat, Mat, MatStructure);
92: /*44*/
93: PetscErrorCode (*getrowmax)(Mat, Vec, PetscInt[]);
94: PetscErrorCode (*scale)(Mat, PetscScalar);
95: PetscErrorCode (*shift)(Mat, PetscScalar);
96: PetscErrorCode (*diagonalset)(Mat, Vec, InsertMode);
97: PetscErrorCode (*zerorowscolumns)(Mat, PetscInt, const PetscInt[], PetscScalar, Vec, Vec);
98: /*49*/
99: PetscErrorCode (*setrandom)(Mat, PetscRandom);
100: PetscErrorCode (*getrowij)(Mat, PetscInt, PetscBool, PetscBool, PetscInt *, const PetscInt *[], const PetscInt *[], PetscBool *);
101: PetscErrorCode (*restorerowij)(Mat, PetscInt, PetscBool, PetscBool, PetscInt *, const PetscInt *[], const PetscInt *[], PetscBool *);
102: PetscErrorCode (*getcolumnij)(Mat, PetscInt, PetscBool, PetscBool, PetscInt *, const PetscInt *[], const PetscInt *[], PetscBool *);
103: PetscErrorCode (*restorecolumnij)(Mat, PetscInt, PetscBool, PetscBool, PetscInt *, const PetscInt *[], const PetscInt *[], PetscBool *);
104: /*54*/
105: PetscErrorCode (*fdcoloringcreate)(Mat, ISColoring, MatFDColoring);
106: PetscErrorCode (*coloringpatch)(Mat, PetscInt, PetscInt, ISColoringValue[], ISColoring *);
107: PetscErrorCode (*setunfactored)(Mat);
108: PetscErrorCode (*permute)(Mat, IS, IS, Mat *);
109: PetscErrorCode (*setvaluesblocked)(Mat, PetscInt, const PetscInt[], PetscInt, const PetscInt[], const PetscScalar[], InsertMode);
110: /*59*/
111: PetscErrorCode (*createsubmatrix)(Mat, IS, IS, MatReuse, Mat *);
112: PetscErrorCode (*destroy)(Mat);
113: PetscErrorCode (*view)(Mat, PetscViewer);
114: PetscErrorCode (*convertfrom)(Mat, MatType, MatReuse, Mat *);
115: PetscErrorCode (*matmatmultsymbolic)(Mat, Mat, Mat, PetscReal, Mat);
116: /*64*/
117: PetscErrorCode (*matmatmultnumeric)(Mat, Mat, Mat, Mat);
118: PetscErrorCode (*setlocaltoglobalmapping)(Mat, ISLocalToGlobalMapping, ISLocalToGlobalMapping);
119: PetscErrorCode (*setvalueslocal)(Mat, PetscInt, const PetscInt[], PetscInt, const PetscInt[], const PetscScalar[], InsertMode);
120: PetscErrorCode (*zerorowslocal)(Mat, PetscInt, const PetscInt[], PetscScalar, Vec, Vec);
121: PetscErrorCode (*getrowmaxabs)(Mat, Vec, PetscInt[]);
122: /*69*/
123: PetscErrorCode (*getrowminabs)(Mat, Vec, PetscInt[]);
124: PetscErrorCode (*convert)(Mat, MatType, MatReuse, Mat *);
125: PetscErrorCode (*hasoperation)(Mat, MatOperation, PetscBool *);
126: PetscErrorCode (*fdcoloringapply)(Mat, MatFDColoring, Vec, void *);
127: PetscErrorCode (*setfromoptions)(Mat, PetscOptionItems);
128: /*74*/
129: PetscErrorCode (*findzerodiagonals)(Mat, IS *);
130: PetscErrorCode (*mults)(Mat, Vecs, Vecs);
131: PetscErrorCode (*solves)(Mat, Vecs, Vecs);
132: PetscErrorCode (*getinertia)(Mat, PetscInt *, PetscInt *, PetscInt *);
133: PetscErrorCode (*load)(Mat, PetscViewer);
134: /*79*/
135: PetscErrorCode (*issymmetric)(Mat, PetscReal, PetscBool *);
136: PetscErrorCode (*ishermitian)(Mat, PetscReal, PetscBool *);
137: PetscErrorCode (*isstructurallysymmetric)(Mat, PetscBool *);
138: PetscErrorCode (*setvaluesblockedlocal)(Mat, PetscInt, const PetscInt[], PetscInt, const PetscInt[], const PetscScalar[], InsertMode);
139: PetscErrorCode (*getvecs)(Mat, Vec *, Vec *);
140: /*84*/
141: PetscErrorCode (*matmultsymbolic)(Mat, Mat, PetscReal, Mat);
142: PetscErrorCode (*matmultnumeric)(Mat, Mat, Mat);
143: PetscErrorCode (*ptapnumeric)(Mat, Mat, Mat); /* double dispatch wrapper routine */
144: PetscErrorCode (*mattransposemultsymbolic)(Mat, Mat, PetscReal, Mat);
145: PetscErrorCode (*mattransposemultnumeric)(Mat, Mat, Mat);
146: /*89*/
147: PetscErrorCode (*bindtocpu)(Mat, PetscBool);
148: PetscErrorCode (*productsetfromoptions)(Mat);
149: PetscErrorCode (*productsymbolic)(Mat);
150: PetscErrorCode (*productnumeric)(Mat);
151: PetscErrorCode (*conjugate)(Mat); /* complex conjugate */
152: /*94*/
153: PetscErrorCode (*viewnative)(Mat, PetscViewer);
154: PetscErrorCode (*setvaluesrow)(Mat, PetscInt, const PetscScalar[]);
155: PetscErrorCode (*realpart)(Mat);
156: PetscErrorCode (*imaginarypart)(Mat);
157: PetscErrorCode (*getrowuppertriangular)(Mat);
158: /*99*/
159: PetscErrorCode (*restorerowuppertriangular)(Mat);
160: PetscErrorCode (*matsolve)(Mat, Mat, Mat);
161: PetscErrorCode (*matsolvetranspose)(Mat, Mat, Mat);
162: PetscErrorCode (*getrowmin)(Mat, Vec, PetscInt[]);
163: PetscErrorCode (*getcolumnvector)(Mat, Vec, PetscInt);
164: /*104*/
165: PetscErrorCode (*getseqnonzerostructure)(Mat, Mat *);
166: PetscErrorCode (*create)(Mat);
167: PetscErrorCode (*getghosts)(Mat, PetscInt *, const PetscInt *[]);
168: PetscErrorCode (*getlocalsubmatrix)(Mat, IS, IS, Mat *);
169: PetscErrorCode (*restorelocalsubmatrix)(Mat, IS, IS, Mat *);
170: /*109*/
171: PetscErrorCode (*multdiagonalblock)(Mat, Vec, Vec);
172: PetscErrorCode (*hermitiantranspose)(Mat, MatReuse, Mat *);
173: PetscErrorCode (*multhermitiantranspose)(Mat, Vec, Vec);
174: PetscErrorCode (*multhermitiantransposeadd)(Mat, Vec, Vec, Vec);
175: PetscErrorCode (*getmultiprocblock)(Mat, MPI_Comm, MatReuse, Mat *);
176: /*114*/
177: PetscErrorCode (*findnonzerorows)(Mat, IS *);
178: PetscErrorCode (*getcolumnreductions)(Mat, PetscInt, PetscReal *);
179: PetscErrorCode (*invertblockdiagonal)(Mat, const PetscScalar **);
180: PetscErrorCode (*invertvariableblockdiagonal)(Mat, PetscInt, const PetscInt *, PetscScalar *);
181: PetscErrorCode (*createsubmatricesmpi)(Mat, PetscInt, const IS[], const IS[], MatReuse, Mat **);
182: /*119*/
183: PetscErrorCode (*transposematmultsymbolic)(Mat, Mat, PetscReal, Mat);
184: PetscErrorCode (*transposematmultnumeric)(Mat, Mat, Mat);
185: PetscErrorCode (*transposecoloringcreate)(Mat, ISColoring, MatTransposeColoring);
186: PetscErrorCode (*transcoloringapplysptoden)(MatTransposeColoring, Mat, Mat);
187: PetscErrorCode (*transcoloringapplydentosp)(MatTransposeColoring, Mat, Mat);
188: /*124*/
189: PetscErrorCode (*rartnumeric)(Mat, Mat, Mat); /* double dispatch wrapper routine */
190: PetscErrorCode (*setblocksizes)(Mat, PetscInt, PetscInt);
191: PetscErrorCode (*residual)(Mat, Vec, Vec, Vec);
192: PetscErrorCode (*fdcoloringsetup)(Mat, ISColoring, MatFDColoring);
193: PetscErrorCode (*findoffblockdiagonalentries)(Mat, IS *);
194: /*129*/
195: PetscErrorCode (*creatempimatconcatenateseqmat)(MPI_Comm, Mat, PetscInt, MatReuse, Mat *);
196: PetscErrorCode (*destroysubmatrices)(PetscInt, Mat *[]);
197: PetscErrorCode (*mattransposesolve)(Mat, Mat, Mat);
198: PetscErrorCode (*getvalueslocal)(Mat, PetscInt, const PetscInt[], PetscInt, const PetscInt[], PetscScalar[]);
199: PetscErrorCode (*creategraph)(Mat, PetscBool, PetscBool, PetscReal, PetscInt, PetscInt[], Mat *);
200: /*134*/
201: PetscErrorCode (*transposesymbolic)(Mat, Mat *);
202: PetscErrorCode (*eliminatezeros)(Mat, PetscBool);
203: PetscErrorCode (*getrowsumabs)(Mat, Vec);
204: PetscErrorCode (*getfactor)(Mat, MatSolverType, MatFactorType, Mat *);
205: PetscErrorCode (*getblockdiagonal)(Mat, Mat *); // NOTE: the caller of get{block, vblock}diagonal owns the returned matrix;
206: /*139*/
207: PetscErrorCode (*getvblockdiagonal)(Mat, Mat *); // they must destroy it after use
208: PetscErrorCode (*copyhashtoxaij)(Mat, Mat);
209: PetscErrorCode (*getcurrentmemtype)(Mat, PetscMemType *);
210: PetscErrorCode (*zerorowscolumnslocal)(Mat, PetscInt, const PetscInt[], PetscScalar, Vec, Vec);
211: PetscErrorCode (*adot)(Mat, Vec, Vec, PetscScalar *); /* induced vector inner product */
212: /*144*/
213: PetscErrorCode (*anorm)(Mat, Vec, PetscReal *); /* induced vector norm */
214: PetscErrorCode (*adot_local)(Mat, Vec, Vec, PetscScalar *);
215: PetscErrorCode (*anorm_local)(Mat, Vec, PetscReal *);
216: PetscErrorCode (*getordering)(Mat, MatOrderingType, IS *, IS *);
217: };
218: /*
219: If you add MatOps entries above also add them to the MATOP enum
220: in include/petscmat.h
221: */
223: #include <petscsys.h>
225: typedef struct _p_MatRootName *MatRootName;
226: struct _p_MatRootName {
227: char *rname, *sname, *mname;
228: MatRootName next;
229: };
231: PETSC_EXTERN MatRootName MatRootNameList;
233: /*
234: Utility private matrix routines used outside Mat
235: */
236: PETSC_SINGLE_LIBRARY_INTERN PetscErrorCode MatFindNonzeroRowsOrCols_Basic(Mat, PetscBool, PetscReal, IS *);
237: PETSC_EXTERN PetscErrorCode MatShellGetScalingShifts(Mat, PetscScalar *, PetscScalar *, Vec *, Vec *, Vec *, Mat *, IS *, IS *);
239: #define MAT_SHELL_NOT_ALLOWED (void *)-1
241: /*
242: Utility private matrix routines
243: */
244: PETSC_INTERN PetscErrorCode MatConvert_Basic(Mat, MatType, MatReuse, Mat *);
245: PETSC_INTERN PetscErrorCode MatConvert_Shell(Mat, MatType, MatReuse, Mat *);
246: PETSC_INTERN PetscErrorCode MatConvertFrom_Shell(Mat, MatType, MatReuse, Mat *);
247: PETSC_INTERN PetscErrorCode MatShellSetContext_Immutable(Mat, void *);
248: PETSC_INTERN PetscErrorCode MatShellSetContextDestroy_Immutable(Mat, PetscCtxDestroyFn *);
249: PETSC_INTERN PetscErrorCode MatShellSetManageScalingShifts_Immutable(Mat);
250: PETSC_INTERN PetscErrorCode MatCopy_Basic(Mat, Mat, MatStructure);
251: PETSC_INTERN PetscErrorCode MatDiagonalSet_Default(Mat, Vec, InsertMode);
252: #if PetscDefined(HAVE_SCALAPACK) && (PetscDefined(USE_REAL_SINGLE) || PetscDefined(USE_REAL_DOUBLE))
253: PETSC_INTERN PetscErrorCode MatConvert_Dense_ScaLAPACK(Mat, MatType, MatReuse, Mat *);
254: #endif
255: PETSC_INTERN PetscErrorCode MatSetPreallocationCOO_Basic(Mat, PetscCount, PetscInt[], PetscInt[]);
256: PETSC_INTERN PetscErrorCode MatSetValuesCOO_Basic(Mat, const PetscScalar[], InsertMode);
258: /* Scattering of dense matrices with strided PetscSF */
259: PETSC_EXTERN PetscErrorCode MatDenseScatter_Private(PetscSF, Mat, Mat, InsertMode, ScatterMode);
261: /* This can be moved to the public header after implementing some missing MatProducts */
262: PETSC_INTERN PetscErrorCode MatCreateFromISLocalToGlobalMapping(ISLocalToGlobalMapping, Mat, PetscBool, PetscBool, MatType, Mat *);
264: /* these callbacks rely on the old matrix function pointers for
265: matmat operations. They are unsafe, and should be removed.
266: However, the amount of work needed to clean up all the
267: implementations is not negligible */
268: PETSC_INTERN PetscErrorCode MatProductSymbolic_AB(Mat);
269: PETSC_INTERN PetscErrorCode MatProductNumeric_AB(Mat);
270: PETSC_INTERN PetscErrorCode MatProductSymbolic_AtB(Mat);
271: PETSC_INTERN PetscErrorCode MatProductNumeric_AtB(Mat);
272: PETSC_INTERN PetscErrorCode MatProductSymbolic_ABt(Mat);
273: PETSC_INTERN PetscErrorCode MatProductNumeric_ABt(Mat);
274: PETSC_INTERN PetscErrorCode MatProductNumeric_PtAP(Mat);
275: PETSC_INTERN PetscErrorCode MatProductNumeric_RARt(Mat);
276: PETSC_INTERN PetscErrorCode MatProductSymbolic_ABC(Mat);
277: PETSC_INTERN PetscErrorCode MatProductNumeric_ABC(Mat);
279: PETSC_INTERN PetscErrorCode MatProductCreate_Private(Mat, Mat, Mat, Mat);
280: /* this callback handles all the different triple products and
281: does not rely on the function pointers; used by cuSPARSE/hipSPARSE and KOKKOS-KERNELS */
282: PETSC_INTERN PetscErrorCode MatProductSymbolic_ABC_Basic(Mat);
284: /* CreateGraph is common to AIJ seq and mpi */
285: PETSC_INTERN PetscErrorCode MatCreateGraph_Simple_AIJ(Mat, PetscBool, PetscBool, PetscReal, PetscInt, PetscInt[], Mat *);
287: #if PetscDefined(CLANG_STATIC_ANALYZER)
288: template <typename Tm>
289: extern void MatCheckPreallocated(Tm, int);
290: template <typename Tm>
291: extern void MatCheckProduct(Tm, int);
292: #else /* PETSC_CLANG_STATIC_ANALYZER */
293: #define MatCheckPreallocated(A, arg) \
294: do { \
295: if (!(A)->preallocated) PetscCall(MatSetUp(A)); \
296: } while (0)
298: #if PetscDefined(USE_DEBUG)
299: #define MatCheckProduct(A, arg) \
300: do { \
301: PetscCheck((A)->product, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Argument %d \"%s\" is not a matrix obtained from MatProductCreate()", (arg), #A); \
302: } while (0)
303: #else
304: #define MatCheckProduct(A, arg) \
305: do { \
306: } while (0)
307: #endif
308: #endif /* PETSC_CLANG_STATIC_ANALYZER */
310: /*
311: The stash is used to temporarily store inserted matrix values that
312: belong to another processor. During the assembly phase the stashed
313: values are moved to the correct processor and
314: */
316: typedef struct _MatStashSpace *PetscMatStashSpace;
318: struct _MatStashSpace {
319: PetscMatStashSpace next;
320: PetscScalar *space_head, *val;
321: PetscInt *idx, *idy;
322: PetscInt total_space_size;
323: PetscInt local_used;
324: PetscInt local_remaining;
325: };
327: PETSC_EXTERN PetscErrorCode PetscMatStashSpaceGet(PetscInt, PetscInt, PetscMatStashSpace *);
328: PETSC_EXTERN PetscErrorCode PetscMatStashSpaceContiguous(PetscInt, PetscMatStashSpace *, PetscScalar *, PetscInt *, PetscInt *);
329: PETSC_EXTERN PetscErrorCode PetscMatStashSpaceDestroy(PetscMatStashSpace *);
331: typedef struct {
332: PetscInt count;
333: } MatStashHeader;
335: typedef struct {
336: void *buffer; /* Of type blocktype, dynamically constructed */
337: PetscInt count;
338: char pending;
339: } MatStashFrame;
341: typedef struct _MatStash MatStash;
342: struct _MatStash {
343: PetscInt nmax; /* maximum stash size */
344: PetscInt umax; /* user specified max-size */
345: PetscInt oldnmax; /* the nmax value used previously */
346: PetscInt n; /* stash size */
347: PetscInt bs; /* block size of the stash */
348: PetscInt reallocs; /* preserve the no of mallocs invoked */
349: PetscMatStashSpace space_head, space; /* linked list to hold stashed global row/column numbers and matrix values */
351: PetscErrorCode (*ScatterBegin)(Mat, MatStash *, PetscInt *);
352: PetscErrorCode (*ScatterGetMesg)(MatStash *, PetscMPIInt *, PetscInt **, PetscInt **, PetscScalar **, PetscInt *);
353: PetscErrorCode (*ScatterEnd)(MatStash *);
354: PetscErrorCode (*ScatterDestroy)(MatStash *);
356: /* The following variables are used for communication */
357: MPI_Comm comm;
358: PetscMPIInt size, rank;
359: PetscMPIInt tag1, tag2;
360: MPI_Request *send_waits; /* array of send requests */
361: MPI_Request *recv_waits; /* array of receive requests */
362: MPI_Status *send_status; /* array of send status */
363: PetscMPIInt nsends, nrecvs; /* numbers of sends and receives */
364: PetscScalar *svalues; /* sending data */
365: PetscInt *sindices;
366: PetscScalar **rvalues; /* receiving data (values) */
367: PetscInt **rindices; /* receiving data (indices) */
368: PetscMPIInt nprocessed; /* number of messages already processed */
369: PetscMPIInt *flg_v; /* indicates what messages have arrived so far and from whom */
370: PetscBool reproduce;
371: PetscMPIInt reproduce_count;
373: /* The following variables are used for BTS communication */
374: PetscBool first_assembly_done; /* Is the first time matrix assembly done? */
375: PetscBool use_status; /* Use MPI_Status to determine number of items in each message */
376: PetscMPIInt nsendranks;
377: PetscMPIInt nrecvranks;
378: PetscMPIInt *sendranks;
379: PetscMPIInt *recvranks;
380: MatStashHeader *sendhdr, *recvhdr;
381: MatStashFrame *sendframes; /* pointers to the main messages */
382: MatStashFrame *recvframes;
383: MatStashFrame *recvframe_active;
384: PetscInt recvframe_i; /* index of block within active frame */
385: PetscInt recvframe_count; /* Count actually sent for current frame */
386: PetscMPIInt recvcount; /* Number of receives processed so far */
387: PetscMPIInt *some_indices; /* From last call to MPI_Waitsome */
388: MPI_Status *some_statuses; /* Statuses from last call to MPI_Waitsome */
389: PetscMPIInt some_count; /* Number of requests completed in last call to MPI_Waitsome */
390: PetscMPIInt some_i; /* Index of request currently being processed */
391: MPI_Request *sendreqs;
392: MPI_Request *recvreqs;
393: PetscSegBuffer segsendblocks;
394: PetscSegBuffer segrecvframe;
395: PetscSegBuffer segrecvblocks;
396: MPI_Datatype blocktype;
397: size_t blocktype_size;
398: InsertMode *insertmode; /* Pointer to check mat->insertmode and set upon message arrival in case no local values have been set. */
399: };
401: #if !PetscDefined(HAVE_MPIUNI)
402: PETSC_INTERN PetscErrorCode MatStashScatterDestroy_BTS(MatStash *);
403: #endif
404: PETSC_INTERN PetscErrorCode MatStashCreate_Private(MPI_Comm, PetscInt, MatStash *);
405: PETSC_INTERN PetscErrorCode MatStashDestroy_Private(MatStash *);
406: PETSC_INTERN PetscErrorCode MatStashScatterEnd_Private(MatStash *);
407: PETSC_INTERN PetscErrorCode MatStashSetInitialSize_Private(MatStash *, PetscInt);
408: PETSC_INTERN PetscErrorCode MatStashGetInfo_Private(MatStash *, PetscInt *, PetscInt *);
409: PETSC_INTERN PetscErrorCode MatStashValuesRow_Private(MatStash *, PetscInt, PetscInt, const PetscInt[], const PetscScalar[], PetscBool);
410: PETSC_INTERN PetscErrorCode MatStashValuesCol_Private(MatStash *, PetscInt, PetscInt, const PetscInt[], const PetscScalar[], PetscInt, PetscBool);
411: PETSC_INTERN PetscErrorCode MatStashValuesRowBlocked_Private(MatStash *, PetscInt, PetscInt, const PetscInt[], const PetscScalar[], PetscInt, PetscInt, PetscInt);
412: PETSC_INTERN PetscErrorCode MatStashValuesColBlocked_Private(MatStash *, PetscInt, PetscInt, const PetscInt[], const PetscScalar[], PetscInt, PetscInt, PetscInt);
413: PETSC_INTERN PetscErrorCode MatStashScatterBegin_Private(Mat, MatStash *, PetscInt *);
414: PETSC_INTERN PetscErrorCode MatStashScatterGetMesg_Private(MatStash *, PetscMPIInt *, PetscInt **, PetscInt **, PetscScalar **, PetscInt *);
415: PETSC_INTERN PetscErrorCode MatGetInfo_External(Mat, MatInfoType, MatInfo *);
417: typedef struct {
418: PetscInt dim;
419: PetscInt dims[4];
420: PetscInt starts[4];
421: PetscBool noc; /* this is a single component problem, hence user will not set MatStencil.c */
422: } MatStencilInfo;
424: /* Info about using compressed row format */
425: typedef struct {
426: PetscBool use; /* indicates compressed rows have been checked and will be used */
427: PetscInt nrows; /* number of non-zero rows */
428: PetscInt *i; /* compressed row pointer */
429: PetscInt *rindex; /* compressed row index */
430: } Mat_CompressedRow;
431: PETSC_EXTERN PetscErrorCode MatCheckCompressedRow(Mat, PetscInt, Mat_CompressedRow *, PetscInt *, PetscInt, PetscReal);
433: typedef struct { /* used by MatCreateRedundantMatrix() for reusing matredundant */
434: PetscInt nzlocal, nsends, nrecvs;
435: PetscMPIInt *send_rank, *recv_rank;
436: PetscInt *sbuf_nz, *rbuf_nz, *sbuf_j, **rbuf_j;
437: PetscScalar *sbuf_a, **rbuf_a;
438: MPI_Comm subcomm; /* when user does not provide a subcomm */
439: IS isrow, iscol;
440: Mat *matseq;
441: } Mat_Redundant;
443: typedef struct { /* used by MatProduct() */
444: MatProductType type;
445: char *alg;
446: Mat A, B, C, Dwork;
447: 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, .. */
448: 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 .. */
449: PetscBool symbolic_used_the_fact_C_is_symmetric; /* MatMatMult(A,B,MAT_REUSE_MATRIX,..&C) is still legitimate), we need to redo symbolic! */
450: PetscObjectParameterDeclare(PetscReal, fill);
451: 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 */
452: PetscBool setfromoptionscalled;
454: /* Some products may display the information on the algorithm used */
455: PetscErrorCode (*view)(Mat, PetscViewer);
457: /* many products have intermediate data structures, each specific to Mat types and product type */
458: PetscBool clear; /* whether or not to clear the data structures after MatProductNumeric has been called */
459: void *data; /* where to stash those structures */
460: PetscCtxDestroyFn *destroy; /* freeing data */
461: } Mat_Product;
463: struct _p_Mat {
464: PETSCHEADER(struct _MatOps);
465: PetscLayout rmap, cmap;
466: void *data; /* implementation-specific data */
467: MatFactorType factortype; /* MAT_FACTOR_LU, ILU, CHOLESKY or ICC */
468: PetscBool trivialsymbolic; /* indicates the symbolic factorization doesn't actually do a symbolic factorization, it is delayed to the numeric factorization */
469: PetscBool canuseordering; /* factorization can use ordering provide to routine (most PETSc implementations) */
470: MatOrderingType preferredordering[MAT_FACTOR_NUM_TYPES]; /* what is the preferred (or default) ordering for the matrix solver type */
471: PetscBool assembled; /* is the matrix assembled? */
472: PetscBool was_assembled; /* new values inserted into assembled mat */
473: PetscInt num_ass; /* number of times matrix has been assembled */
474: PetscObjectState nonzerostate; /* each time new nonzeros locations are introduced into the matrix this is updated */
475: PetscObjectState ass_nonzerostate; /* nonzero state at last assembly */
476: MatInfo info; /* matrix information */
477: InsertMode insertmode; /* have values been inserted in matrix or added? */
478: MatStash stash, bstash; /* used for assembling off-proc mat emements */
479: MatNullSpace nullsp; /* null space (operator is singular) */
480: MatNullSpace transnullsp; /* null space of transpose of operator */
481: MatNullSpace nearnullsp; /* near null space to be used by multigrid methods */
482: PetscInt congruentlayouts; /* are the rows and columns layouts congruent? */
483: PetscBool preallocated;
484: MatStencilInfo stencil; /* information for structured grid */
485: PetscBool3 symmetric, hermitian, structurally_symmetric, spd;
486: PetscBool symmetry_eternal, structural_symmetry_eternal, spd_eternal;
487: PetscBool nooffprocentries, nooffproczerorows;
488: PetscBool assembly_subset; /* set by MAT_SUBSET_OFF_PROC_ENTRIES */
489: PetscBool submat_singleis; /* for efficient PCSetUp_ASM() */
490: PetscBool structure_only;
491: PetscBool sortedfull; /* full, sorted rows are inserted */
492: PetscBool force_diagonals; /* set by MAT_FORCE_DIAGONAL_ENTRIES */
493: #if PetscDefined(HAVE_DEVICE)
494: PetscOffloadMask offloadmask; /* a mask which indicates where the valid matrix data is (GPU, CPU or both) */
495: PetscBool boundtocpu;
496: PetscBool bindingpropagates;
497: #endif
498: char *defaultrandtype;
499: void *spptr; /* pointer for special library like SuperLU */
500: char *solvertype;
501: PetscBool checksymmetryonassembly, checknullspaceonassembly;
502: PetscReal checksymmetrytol;
503: Mat schur; /* Schur complement matrix */
504: MatFactorSchurStatus schur_status; /* status of the Schur complement matrix */
505: Mat_Redundant *redundant; /* used by MatCreateRedundantMatrix() */
506: PetscBool erroriffailure; /* Generate an error if detected (for example a zero pivot) instead of returning */
507: MatFactorError factorerrortype; /* type of error in factorization */
508: PetscReal factorerror_zeropivot_value; /* If numerical zero pivot was detected this is the computed value */
509: PetscInt factorerror_zeropivot_row; /* Row where zero pivot was detected */
510: PetscInt nblocks, *bsizes; /* support for MatSetVariableBlockSizes() */
511: PetscInt p_cstart, p_rank, p_cend, n_rank; /* Information from parallel MatComputeVariableBlockEnvelope() */
512: PetscBool p_parallel;
513: char *defaultvectype;
514: Mat_Product *product;
515: PetscBool form_explicit_transpose; /* hint to generate an explicit mat tranpsose for operations like MatMultTranspose() */
516: PetscBool transupdated; /* whether or not the explicitly generated transpose is up-to-date */
517: char *factorprefix; /* the prefix to use with factored matrix that is created */
518: PetscBool hash_active; /* indicates MatSetValues() is being handled by hashing */
519: Vec dot_vec; /* work vector used by MatADot_Default() */
520: };
522: PETSC_INTERN PetscErrorCode MatAXPY_Basic(Mat, PetscScalar, Mat, MatStructure);
523: PETSC_INTERN PetscErrorCode MatAXPY_BasicWithPreallocation(Mat, Mat, PetscScalar, Mat, MatStructure);
524: PETSC_INTERN PetscErrorCode MatAXPY_Basic_Preallocate(Mat, Mat, Mat *);
525: PETSC_INTERN PetscErrorCode MatAXPY_Dense_Nest(Mat, PetscScalar, Mat);
527: /*
528: Utility for MatZeroRows
529: */
530: PETSC_INTERN PetscErrorCode MatZeroRowsMapLocal_Private(Mat, PetscInt, const PetscInt *, PetscInt *, PetscInt **);
532: /*
533: Utility for MatView/MatLoad
534: */
535: PETSC_INTERN PetscErrorCode MatView_Binary_BlockSizes(Mat, PetscViewer);
536: PETSC_INTERN PetscErrorCode MatLoad_Binary_BlockSizes(Mat, PetscViewer);
538: /*
539: Object for partitioning graphs
540: */
542: typedef struct _MatPartitioningOps *MatPartitioningOps;
543: struct _MatPartitioningOps {
544: PetscErrorCode (*apply)(MatPartitioning, IS *);
545: PetscErrorCode (*applynd)(MatPartitioning, IS *);
546: PetscErrorCode (*setfromoptions)(MatPartitioning, PetscOptionItems);
547: PetscErrorCode (*destroy)(MatPartitioning);
548: PetscErrorCode (*view)(MatPartitioning, PetscViewer);
549: PetscErrorCode (*improve)(MatPartitioning, IS *);
550: };
552: struct _p_MatPartitioning {
553: PETSCHEADER(struct _MatPartitioningOps);
554: Mat adj;
555: PetscInt *vertex_weights;
556: PetscReal *part_weights;
557: PetscInt n; /* number of partitions */
558: PetscInt ncon; /* number of vertex weights per vertex */
559: void *data;
560: PetscBool use_edge_weights; /* A flag indicates whether or not to use edge weights */
561: };
563: /* needed for parallel nested dissection by ParMetis and PTSCOTCH */
564: PETSC_INTERN PetscErrorCode MatPartitioningSizesToSep_Private(PetscInt, PetscInt[], PetscInt[], PetscInt[]);
566: /*
567: Object for coarsen graphs
568: */
569: typedef struct _MatCoarsenOps *MatCoarsenOps;
570: struct _MatCoarsenOps {
571: PetscErrorCode (*apply)(MatCoarsen);
572: PetscErrorCode (*setfromoptions)(MatCoarsen, PetscOptionItems);
573: PetscErrorCode (*destroy)(MatCoarsen);
574: PetscErrorCode (*view)(MatCoarsen, PetscViewer);
575: };
577: #define MAT_COARSEN_STRENGTH_INDEX_SIZE 3
578: struct _p_MatCoarsen {
579: PETSCHEADER(struct _MatCoarsenOps);
580: Mat graph;
581: void *subctx;
582: /* */
583: PetscBool strict_aggs;
584: IS perm;
585: PetscCoarsenData *agg_lists;
586: PetscInt max_it; /* number of iterations in HEM */
587: PetscReal threshold; /* HEM can filter interim graphs */
588: PetscInt strength_index_size;
589: PetscInt strength_index[MAT_COARSEN_STRENGTH_INDEX_SIZE];
590: };
592: PETSC_EXTERN PetscErrorCode MatCoarsenMISKSetDistance(MatCoarsen, PetscInt);
593: PETSC_EXTERN PetscErrorCode MatCoarsenMISKGetDistance(MatCoarsen, PetscInt *);
595: /*
596: Used in aijdevice.h
597: */
598: typedef struct {
599: PetscInt *i;
600: PetscInt *j;
601: PetscScalar *a;
602: PetscInt n;
603: PetscInt ignorezeroentries;
604: } PetscCSRDataStructure;
606: /*
607: MatFDColoring is used to compute Jacobian matrices efficiently
608: via coloring. The data structure is explained below in an example.
610: Color = 0 1 0 2 | 2 3 0
611: ---------------------------------------------------
612: 00 01 | 05
613: 10 11 | 14 15 Processor 0
614: 22 23 | 25
615: 32 33 |
616: ===================================================
617: | 44 45 46
618: 50 | 55 Processor 1
619: | 64 66
620: ---------------------------------------------------
622: ncolors = 4;
624: ncolumns = {2,1,1,0}
625: columns = {{0,2},{1},{3},{}}
626: nrows = {4,2,3,3}
627: rows = {{0,1,2,3},{0,1},{1,2,3},{0,1,2}}
628: vwscale = {dx(0),dx(1),dx(2),dx(3)} MPI Vec
629: vscale = {dx(0),dx(1),dx(2),dx(3),dx(4),dx(5)} Seq Vec
631: ncolumns = {1,0,1,1}
632: columns = {{6},{},{4},{5}}
633: nrows = {3,0,2,2}
634: rows = {{0,1,2},{},{1,2},{1,2}}
635: vwscale = {dx(4),dx(5),dx(6)} MPI Vec
636: vscale = {dx(0),dx(4),dx(5),dx(6)} Seq Vec
638: See the routine MatFDColoringApply() for how this data is used
639: to compute the Jacobian.
641: */
642: typedef struct {
643: PetscInt row;
644: PetscInt col;
645: PetscScalar *valaddr; /* address of value */
646: } MatEntry;
648: typedef struct {
649: PetscInt row;
650: PetscScalar *valaddr; /* address of value */
651: } MatEntry2;
653: struct _p_MatFDColoring {
654: PETSCHEADER(int);
655: PetscInt M, N, m; /* total rows, columns; local rows */
656: PetscInt rstart; /* first row owned by local processor */
657: PetscInt ncolors; /* number of colors */
658: PetscInt *ncolumns; /* number of local columns for a color */
659: PetscInt **columns; /* lists the local columns of each color (using global column numbering) */
660: IS *isa; /* these are the IS that contain the column values given in columns */
661: PetscInt *nrows; /* number of local rows for each color */
662: MatEntry *matentry; /* holds (row, column, address of value) for Jacobian matrix entry */
663: MatEntry2 *matentry2; /* holds (row, address of value) for Jacobian matrix entry */
664: PetscScalar *dy; /* store a block of F(x+dx)-F(x) when J is in BAIJ format */
665: PetscReal error_rel; /* square root of relative error in computing function */
666: PetscReal umin; /* minimum allowable u'dx value */
667: Vec w1, w2, w3; /* work vectors used in computing Jacobian */
668: PetscBool fset; /* indicates that the initial function value F(X) is set */
669: MatFDColoringFn *f; /* function that defines Jacobian */
670: void *fctx; /* optional user-defined context for use by the function f */
671: Vec vscale; /* holds FD scaling, i.e. 1/dx for each perturbed column */
672: PetscInt currentcolor; /* color for which function evaluation is being done now */
673: const char *htype; /* "wp" or "ds" */
674: ISColoringType ctype; /* IS_COLORING_GLOBAL or IS_COLORING_LOCAL */
675: PetscInt brows, bcols; /* number of block rows or columns for speedup inserting the dense matrix into sparse Jacobian */
676: PetscBool setupcalled; /* true if setup has been called */
677: PetscBool viewed; /* true if the -mat_fd_coloring_view has been triggered already */
678: PetscFortranCallbackFn *ftn_func_pointer; /* serve the same purpose as *fortran_func_pointers in PETSc objects */
679: void *ftn_func_cntx;
680: PetscObjectId matid; /* matrix this object was created with, must always be the same */
681: };
683: typedef struct _MatColoringOps *MatColoringOps;
684: struct _MatColoringOps {
685: PetscErrorCode (*destroy)(MatColoring);
686: PetscErrorCode (*setfromoptions)(MatColoring, PetscOptionItems);
687: PetscErrorCode (*view)(MatColoring, PetscViewer);
688: PetscErrorCode (*apply)(MatColoring, ISColoring *);
689: PetscErrorCode (*weights)(MatColoring, PetscReal **, PetscInt **);
690: };
692: struct _p_MatColoring {
693: PETSCHEADER(struct _MatColoringOps);
694: Mat mat;
695: PetscInt dist; /* distance of the coloring */
696: PetscInt maxcolors; /* the maximum number of colors returned, maxcolors=1 for MIS */
697: void *data; /* inner context */
698: PetscBool valid; /* check to see if what is produced is a valid coloring */
699: MatColoringWeightType weight_type; /* type of weight computation to be performed */
700: PetscReal *user_weights; /* custom weights and permutation */
701: PetscInt *user_lperm;
702: PetscBool valid_iscoloring; /* check to see if matcoloring is produced a valid iscoloring */
703: };
705: struct _p_MatTransposeColoring {
706: PETSCHEADER(int);
707: PetscInt M, N, m; /* total rows, columns; local rows */
708: PetscInt rstart; /* first row owned by local processor */
709: PetscInt ncolors; /* number of colors */
710: PetscInt *ncolumns; /* number of local columns for a color */
711: PetscInt *nrows; /* number of local rows for each color */
712: PetscInt currentcolor; /* color for which function evaluation is being done now */
713: ISColoringType ctype; /* IS_COLORING_GLOBAL or IS_COLORING_LOCAL */
715: PetscInt *colorforrow, *colorforcol; /* pointer to rows and columns */
716: PetscInt *rows; /* lists the local rows for each color (using the local row numbering) */
717: PetscInt *den2sp; /* maps (row,color) in the dense matrix to index of sparse matrix array a->a */
718: PetscInt *columns; /* lists the local columns of each color (using global column numbering) */
719: PetscInt brows; /* number of rows for efficient implementation of MatTransColoringApplyDenToSp() */
720: PetscInt *lstart; /* array used for loop over row blocks of Csparse */
721: };
723: /*
724: Null space context for preconditioner/operators
725: */
726: struct _p_MatNullSpace {
727: PETSCHEADER(int);
728: PetscBool has_cnst;
729: PetscInt n;
730: Vec *vecs;
731: PetscScalar *alpha; /* for projections */
732: MatNullSpaceRemoveFn *remove; /* for user provided removal function */
733: void *rmctx; /* context for remove() function */
734: };
736: /*
737: Internal data structure for MATMPIDENSE
738: */
739: typedef struct {
740: Mat A; /* local submatrix */
742: /* The following variables are used for matrix assembly */
743: PetscBool donotstash; /* Flag indicating if values should be stashed */
744: MPI_Request *send_waits; /* array of send requests */
745: MPI_Request *recv_waits; /* array of receive requests */
746: PetscInt nsends, nrecvs; /* numbers of sends and receives */
747: PetscScalar *svalues, *rvalues; /* sending and receiving data */
748: PetscInt rmax; /* maximum message length */
750: /* The following variables are used for matrix-vector products */
751: Vec lvec; /* local vector */
752: PetscSF Mvctx; /* for mat-mult communications */
753: PetscBool roworiented; /* if true, row-oriented input (default) */
755: /* Support for MatDenseGetColumnVec and MatDenseGetSubMatrix */
756: Mat cmat; /* matrix representation of a given subset of columns */
757: Vec cvec; /* vector representation of a given column */
758: const PetscScalar *ptrinuse; /* holds array to be restored (just a placeholder) */
759: PetscInt vecinuse; /* if cvec is in use (col = vecinuse-1) */
760: PetscInt matinuse; /* if cmat is in use (cbegin = matinuse-1) */
761: /* if this is from MatDenseGetSubMatrix, which columns and rows does it correspond to? */
762: PetscInt sub_rbegin;
763: PetscInt sub_rend;
764: PetscInt sub_cbegin;
765: PetscInt sub_cend;
766: } Mat_MPIDense;
768: /*
769: Checking zero pivot for LU, ILU preconditioners.
770: */
771: typedef struct {
772: PetscInt nshift, nshift_max;
773: PetscReal shift_amount, shift_lo, shift_hi, shift_top, shift_fraction;
774: PetscBool newshift;
775: PetscReal rs; /* active row sum of abs(off-diagonals) */
776: PetscScalar pv; /* pivot of the active row */
777: } FactorShiftCtx;
779: PETSC_SINGLE_LIBRARY_INTERN PetscErrorCode MatTransposeCheckNonzeroState_Private(Mat, Mat);
781: PETSC_EXTERN PetscErrorCode MatFactorDumpMatrix(Mat);
782: PETSC_INTERN PetscErrorCode MatSetBlockSizes_Default(Mat, PetscInt, PetscInt);
784: PETSC_SINGLE_LIBRARY_INTERN PetscErrorCode MatShift_Basic(Mat, PetscScalar);
786: static inline PetscErrorCode MatPivotCheck_nz(PETSC_UNUSED Mat mat, const MatFactorInfo *info, FactorShiftCtx *sctx, PETSC_UNUSED PetscInt row)
787: {
788: PetscReal _rs = sctx->rs;
789: PetscReal _zero = info->zeropivot * _rs;
791: PetscFunctionBegin;
792: if (PetscAbsScalar(sctx->pv) <= _zero && !PetscIsNanScalar(sctx->pv)) {
793: /* force |diag| > zeropivot*rs */
794: if (!sctx->nshift) sctx->shift_amount = info->shiftamount;
795: else sctx->shift_amount *= 2.0;
796: sctx->newshift = PETSC_TRUE;
797: (sctx->nshift)++;
798: } else {
799: sctx->newshift = PETSC_FALSE;
800: }
801: PetscFunctionReturn(PETSC_SUCCESS);
802: }
804: static inline PetscErrorCode MatPivotCheck_pd(PETSC_UNUSED Mat mat, const MatFactorInfo *info, FactorShiftCtx *sctx, PETSC_UNUSED PetscInt row)
805: {
806: PetscReal _rs = sctx->rs;
807: PetscReal _zero = info->zeropivot * _rs;
809: PetscFunctionBegin;
810: if (PetscRealPart(sctx->pv) <= _zero && !PetscIsNanScalar(sctx->pv)) {
811: /* force matfactor to be diagonally dominant */
812: if (sctx->nshift == sctx->nshift_max) {
813: sctx->shift_fraction = sctx->shift_hi;
814: } else {
815: sctx->shift_lo = sctx->shift_fraction;
816: sctx->shift_fraction = (sctx->shift_hi + sctx->shift_lo) / (PetscReal)2.;
817: }
818: sctx->shift_amount = sctx->shift_fraction * sctx->shift_top;
819: sctx->nshift++;
820: sctx->newshift = PETSC_TRUE;
821: } else {
822: sctx->newshift = PETSC_FALSE;
823: }
824: PetscFunctionReturn(PETSC_SUCCESS);
825: }
827: static inline PetscErrorCode MatPivotCheck_inblocks(PETSC_UNUSED Mat mat, const MatFactorInfo *info, FactorShiftCtx *sctx, PETSC_UNUSED PetscInt row)
828: {
829: PetscReal _zero = info->zeropivot;
831: PetscFunctionBegin;
832: if (PetscAbsScalar(sctx->pv) <= _zero && !PetscIsNanScalar(sctx->pv)) {
833: sctx->pv += info->shiftamount;
834: sctx->shift_amount = 0.0;
835: sctx->nshift++;
836: }
837: sctx->newshift = PETSC_FALSE;
838: PetscFunctionReturn(PETSC_SUCCESS);
839: }
841: static inline PetscErrorCode MatPivotCheck_none(Mat fact, Mat mat, const MatFactorInfo *info, FactorShiftCtx *sctx, PetscInt row)
842: {
843: PetscReal _zero = info->zeropivot;
845: PetscFunctionBegin;
846: sctx->newshift = PETSC_FALSE;
847: if (PetscAbsScalar(sctx->pv) <= _zero && !PetscIsNanScalar(sctx->pv)) {
848: 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);
849: PetscCall(PetscInfo(mat, "Detected zero pivot in factorization in row %" PetscInt_FMT " value %g tolerance %g\n", row, (double)PetscAbsScalar(sctx->pv), (double)_zero));
850: fact->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
851: fact->factorerror_zeropivot_value = PetscAbsScalar(sctx->pv);
852: fact->factorerror_zeropivot_row = row;
853: }
854: PetscFunctionReturn(PETSC_SUCCESS);
855: }
857: static inline PetscErrorCode MatPivotCheck(Mat fact, Mat mat, const MatFactorInfo *info, FactorShiftCtx *sctx, PetscInt row)
858: {
859: PetscFunctionBegin;
860: if (info->shifttype == (PetscReal)MAT_SHIFT_NONZERO) PetscCall(MatPivotCheck_nz(mat, info, sctx, row));
861: else if (info->shifttype == (PetscReal)MAT_SHIFT_POSITIVE_DEFINITE) PetscCall(MatPivotCheck_pd(mat, info, sctx, row));
862: else if (info->shifttype == (PetscReal)MAT_SHIFT_INBLOCKS) PetscCall(MatPivotCheck_inblocks(mat, info, sctx, row));
863: else PetscCall(MatPivotCheck_none(fact, mat, info, sctx, row));
864: PetscFunctionReturn(PETSC_SUCCESS);
865: }
867: #include <petscbt.h>
868: /*
869: Create and initialize a linked list
870: Input Parameters:
871: idx_start - starting index of the list
872: lnk_max - max value of lnk indicating the end of the list
873: nlnk - max length of the list
874: Output Parameters:
875: lnk - list initialized
876: bt - PetscBT (bitarray) with all bits set to false
877: lnk_empty - flg indicating the list is empty
878: */
879: #define PetscLLCreate(idx_start, lnk_max, nlnk, lnk, bt) ((PetscErrorCode)(PetscMalloc1(nlnk, &lnk) || PetscBTCreate(nlnk, &(bt)) || (lnk[idx_start] = lnk_max, PETSC_SUCCESS)))
881: #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)))
883: 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)
884: {
885: PetscInt location;
887: PetscFunctionBegin;
888: /* start from the beginning if entry < previous entry */
889: if (!assume_sorted && k && entry < *lnkdata) *lnkdata = idx_start;
890: /* search for insertion location */
891: do {
892: location = *lnkdata;
893: *lnkdata = lnk[location];
894: } while (entry > *lnkdata);
895: /* insertion location is found, add entry into lnk */
896: lnk[location] = entry;
897: lnk[entry] = *lnkdata;
898: ++(*nlnk);
899: *lnkdata = entry; /* next search starts from here if next_entry > entry */
900: PetscFunctionReturn(PETSC_SUCCESS);
901: }
903: 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)
904: {
905: PetscFunctionBegin;
906: *nlnk = 0;
907: for (PetscInt k = 0, lnkdata = idx_start; k < nidx; ++k) {
908: const PetscInt entry = indices[k];
910: if (!PetscBTLookupSet(bt, entry)) PetscCall(PetscLLInsertLocation_Private(assume_sorted, k, idx_start, entry, nlnk, &lnkdata, lnk));
911: }
912: PetscFunctionReturn(PETSC_SUCCESS);
913: }
915: /*
916: Add an index set into a sorted linked list
917: Input Parameters:
918: nidx - number of input indices
919: indices - integer array
920: idx_start - starting index of the list
921: lnk - linked list(an integer array) that is created
922: bt - PetscBT (bitarray), bt[idx]=true marks idx is in lnk
923: output Parameters:
924: nlnk - number of newly added indices
925: lnk - the sorted(increasing order) linked list containing new and non-redundate entries from indices
926: bt - updated PetscBT (bitarray)
927: */
928: static inline PetscErrorCode PetscLLAdd(PetscInt nidx, const PetscInt *PETSC_RESTRICT indices, PetscInt idx_start, PetscInt *PETSC_RESTRICT nlnk, PetscInt *PETSC_RESTRICT lnk, PetscBT bt)
929: {
930: PetscFunctionBegin;
931: PetscCall(PetscLLAdd_Private(nidx, indices, idx_start, nlnk, lnk, bt, PETSC_FALSE));
932: PetscFunctionReturn(PETSC_SUCCESS);
933: }
935: /*
936: Add a SORTED ascending index set into a sorted linked list - same as PetscLLAdd() bus skip 'if (_k && _entry < _lnkdata) _lnkdata = idx_start;'
937: Input Parameters:
938: nidx - number of input indices
939: indices - sorted integer array
940: idx_start - starting index of the list
941: lnk - linked list(an integer array) that is created
942: bt - PetscBT (bitarray), bt[idx]=true marks idx is in lnk
943: output Parameters:
944: nlnk - number of newly added indices
945: lnk - the sorted(increasing order) linked list containing new and non-redundate entries from indices
946: bt - updated PetscBT (bitarray)
947: */
948: static inline PetscErrorCode PetscLLAddSorted(PetscInt nidx, const PetscInt *PETSC_RESTRICT indices, PetscInt idx_start, PetscInt *PETSC_RESTRICT nlnk, PetscInt *PETSC_RESTRICT lnk, PetscBT bt)
949: {
950: PetscFunctionBegin;
951: PetscCall(PetscLLAdd_Private(nidx, indices, idx_start, nlnk, lnk, bt, PETSC_TRUE));
952: PetscFunctionReturn(PETSC_SUCCESS);
953: }
955: /*
956: Add a permuted index set into a sorted linked list
957: Input Parameters:
958: nidx - number of input indices
959: indices - integer array
960: perm - permutation of indices
961: idx_start - starting index of the list
962: lnk - linked list(an integer array) that is created
963: bt - PetscBT (bitarray), bt[idx]=true marks idx is in lnk
964: output Parameters:
965: nlnk - number of newly added indices
966: lnk - the sorted(increasing order) linked list containing new and non-redundate entries from indices
967: bt - updated PetscBT (bitarray)
968: */
969: 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)
970: {
971: PetscFunctionBegin;
972: *nlnk = 0;
973: for (PetscInt k = 0, lnkdata = idx_start; k < nidx; ++k) {
974: const PetscInt entry = perm[indices[k]];
976: if (!PetscBTLookupSet(bt, entry)) PetscCall(PetscLLInsertLocation_Private(PETSC_FALSE, k, idx_start, entry, nlnk, &lnkdata, lnk));
977: }
978: PetscFunctionReturn(PETSC_SUCCESS);
979: }
981: #if 0
982: /* this appears to be unused? */
983: static inline PetscErrorCode PetscLLAddSorted_new(PetscInt nidx, PetscInt *indices, PetscInt idx_start, PetscBool *lnk_empty, PetscInt *nlnk, PetscInt *lnk, PetscBT bt)
984: {
985: PetscInt lnkdata = idx_start;
987: PetscFunctionBegin;
988: if (*lnk_empty) {
989: for (PetscInt k = 0; k < nidx; ++k) {
990: const PetscInt entry = indices[k], location = lnkdata;
992: PetscCall(PetscBTSet(bt,entry)); /* mark the new entry */
993: lnkdata = lnk[location];
994: /* insertion location is found, add entry into lnk */
995: lnk[location] = entry;
996: lnk[entry] = lnkdata;
997: lnkdata = entry; /* next search starts from here */
998: }
999: /* lnk[indices[nidx-1]] = lnk[idx_start];
1000: lnk[idx_start] = indices[0];
1001: PetscCall(PetscBTSet(bt,indices[0]));
1002: for (_k=1; _k<nidx; _k++) {
1003: PetscCall(PetscBTSet(bt,indices[_k]));
1004: lnk[indices[_k-1]] = indices[_k];
1005: }
1006: */
1007: *nlnk = nidx;
1008: *lnk_empty = PETSC_FALSE;
1009: } else {
1010: *nlnk = 0;
1011: for (PetscInt k = 0; k < nidx; ++k) {
1012: const PetscInt entry = indices[k];
1014: if (!PetscBTLookupSet(bt,entry)) PetscCall(PetscLLInsertLocation_Private(PETSC_TRUE,k,idx_start,entry,nlnk,&lnkdata,lnk));
1015: }
1016: }
1017: PetscFunctionReturn(PETSC_SUCCESS);
1018: }
1019: #endif
1021: /*
1022: Add a SORTED index set into a sorted linked list used for LUFactorSymbolic()
1023: Same as PetscLLAddSorted() with an additional operation:
1024: count the number of input indices that are no larger than 'diag'
1025: Input Parameters:
1026: indices - sorted integer array
1027: idx_start - starting index of the list, index of pivot row
1028: lnk - linked list(an integer array) that is created
1029: bt - PetscBT (bitarray), bt[idx]=true marks idx is in lnk
1030: diag - index of the active row in LUFactorSymbolic
1031: nzbd - number of input indices with indices <= idx_start
1032: im - im[idx_start] is initialized as num of nonzero entries in row=idx_start
1033: output Parameters:
1034: nlnk - number of newly added indices
1035: lnk - the sorted(increasing order) linked list containing new and non-redundate entries from indices
1036: bt - updated PetscBT (bitarray)
1037: im - im[idx_start]: unchanged if diag is not an entry
1038: : num of entries with indices <= diag if diag is an entry
1039: */
1040: 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)
1041: {
1042: const PetscInt nidx = im[idx_start] - nzbd; /* num of entries with idx_start < index <= diag */
1044: PetscFunctionBegin;
1045: *nlnk = 0;
1046: for (PetscInt k = 0, lnkdata = idx_start; k < nidx; ++k) {
1047: const PetscInt entry = indices[k];
1049: ++nzbd;
1050: if (entry == diag) im[idx_start] = nzbd;
1051: if (!PetscBTLookupSet(bt, entry)) PetscCall(PetscLLInsertLocation_Private(PETSC_TRUE, k, idx_start, entry, nlnk, &lnkdata, lnk));
1052: }
1053: PetscFunctionReturn(PETSC_SUCCESS);
1054: }
1056: /*
1057: Copy data on the list into an array, then initialize the list
1058: Input Parameters:
1059: idx_start - starting index of the list
1060: lnk_max - max value of lnk indicating the end of the list
1061: nlnk - number of data on the list to be copied
1062: lnk - linked list
1063: bt - PetscBT (bitarray), bt[idx]=true marks idx is in lnk
1064: output Parameters:
1065: indices - array that contains the copied data
1066: lnk - linked list that is cleaned and initialize
1067: bt - PetscBT (bitarray) with all bits set to false
1068: */
1069: static inline PetscErrorCode PetscLLClean(PetscInt idx_start, PetscInt lnk_max, PetscInt nlnk, PetscInt *PETSC_RESTRICT lnk, PetscInt *PETSC_RESTRICT indices, PetscBT bt)
1070: {
1071: PetscFunctionBegin;
1072: for (PetscInt j = 0, idx = idx_start; j < nlnk; ++j) {
1073: idx = lnk[idx];
1074: indices[j] = idx;
1075: PetscCall(PetscBTClear(bt, idx));
1076: }
1077: lnk[idx_start] = lnk_max;
1078: PetscFunctionReturn(PETSC_SUCCESS);
1079: }
1081: /*
1082: Free memories used by the list
1083: */
1084: #define PetscLLDestroy(lnk, bt) ((PetscErrorCode)(PetscFree(lnk) || PetscBTDestroy(&(bt))))
1086: /* Routines below are used for incomplete matrix factorization */
1087: /*
1088: Create and initialize a linked list and its levels
1089: Input Parameters:
1090: idx_start - starting index of the list
1091: lnk_max - max value of lnk indicating the end of the list
1092: nlnk - max length of the list
1093: Output Parameters:
1094: lnk - list initialized
1095: lnk_lvl - array of size nlnk for storing levels of lnk
1096: bt - PetscBT (bitarray) with all bits set to false
1097: */
1098: #define PetscIncompleteLLCreate(idx_start, lnk_max, nlnk, lnk, lnk_lvl, bt) \
1099: ((PetscErrorCode)(PetscIntMultError(2, nlnk, NULL) || PetscMalloc1(2 * nlnk, &lnk) || PetscBTCreate(nlnk, &(bt)) || (lnk[idx_start] = lnk_max, lnk_lvl = lnk + nlnk, PETSC_SUCCESS)))
1101: 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)
1102: {
1103: PetscFunctionBegin;
1104: PetscCall(PetscLLInsertLocation_Private(assume_sorted, k, idx_start, entry, nlnk, lnkdata, lnk));
1105: lnklvl[entry] = newval;
1106: PetscFunctionReturn(PETSC_SUCCESS);
1107: }
1109: /*
1110: Initialize a sorted linked list used for ILU and ICC
1111: Input Parameters:
1112: nidx - number of input idx
1113: idx - integer array used for storing column indices
1114: idx_start - starting index of the list
1115: perm - indices of an IS
1116: lnk - linked list(an integer array) that is created
1117: lnklvl - levels of lnk
1118: bt - PetscBT (bitarray), bt[idx]=true marks idx is in lnk
1119: output Parameters:
1120: nlnk - number of newly added idx
1121: lnk - the sorted(increasing order) linked list containing new and non-redundate entries from idx
1122: lnklvl - levels of lnk
1123: bt - updated PetscBT (bitarray)
1124: */
1125: 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)
1126: {
1127: PetscFunctionBegin;
1128: *nlnk = 0;
1129: for (PetscInt k = 0, lnkdata = idx_start; k < nidx; ++k) {
1130: const PetscInt entry = perm[idx[k]];
1132: if (!PetscBTLookupSet(bt, entry)) PetscCall(PetscIncompleteLLInsertLocation_Private(PETSC_FALSE, k, idx_start, entry, nlnk, &lnkdata, lnk, lnklvl, 0));
1133: }
1134: PetscFunctionReturn(PETSC_SUCCESS);
1135: }
1137: 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)
1138: {
1139: PetscFunctionBegin;
1140: *nlnk = 0;
1141: for (PetscInt k = 0, lnkdata = idx_start; k < nidx; ++k) {
1142: const PetscInt incrlev = idxlvl[k] + prow_offset + 1;
1144: if (incrlev <= level) {
1145: const PetscInt entry = idx[k];
1147: if (!PetscBTLookupSet(bt, entry)) PetscCall(PetscIncompleteLLInsertLocation_Private(assume_sorted, k, idx_start, entry, nlnk, &lnkdata, lnk, lnklvl, incrlev));
1148: else if (lnklvl[entry] > incrlev) lnklvl[entry] = incrlev; /* existing entry */
1149: }
1150: }
1151: PetscFunctionReturn(PETSC_SUCCESS);
1152: }
1154: /*
1155: Add a SORTED index set into a sorted linked list for ICC
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: idxlvl_prow - idxlvl[prow], where prow is the row number of the idx
1166: output Parameters:
1167: nlnk - number of newly added indices
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)
1171: Note: the level of U(i,j) is set as lvl(i,j) = min{ lvl(i,j), lvl(prow,i)+lvl(prow,j)+1)
1172: where idx = non-zero columns of U(prow,prow+1:n-1), prow<i
1173: */
1174: 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)
1175: {
1176: PetscFunctionBegin;
1177: PetscCall(PetscIncompleteLLAdd_Private(nidx, idx, level, idxlvl, idx_start, nlnk, lnk, lnklvl, bt, idxlvl_prow, PETSC_TRUE));
1178: PetscFunctionReturn(PETSC_SUCCESS);
1179: }
1181: /*
1182: Add a SORTED index set into a sorted linked list for ILU
1183: Input Parameters:
1184: nidx - number of input indices
1185: idx - sorted integer array used for storing column indices
1186: level - level of fill, e.g., ICC(level)
1187: idxlvl - level of idx
1188: idx_start - starting index of the list
1189: lnk - linked list(an integer array) that is created
1190: lnklvl - levels of lnk
1191: bt - PetscBT (bitarray), bt[idx]=true marks idx is in lnk
1192: prow - the row number of idx
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)
1199: Note: the level of factor(i,j) is set as lvl(i,j) = min{ lvl(i,j), lvl(i,prow)+lvl(prow,j)+1)
1200: where idx = non-zero columns of U(prow,prow+1:n-1), prow<i
1201: */
1202: 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)
1203: {
1204: PetscFunctionBegin;
1205: PetscCall(PetscIncompleteLLAdd_Private(nidx, idx, level, idxlvl, idx_start, nlnk, lnk, lnklvl, bt, lnklvl[prow], PETSC_TRUE));
1206: PetscFunctionReturn(PETSC_SUCCESS);
1207: }
1209: /*
1210: Add a index set into a sorted linked list
1211: Input Parameters:
1212: nidx - number of input idx
1213: idx - integer array used for storing column indices
1214: level - level of fill, e.g., ICC(level)
1215: idxlvl - level of idx
1216: idx_start - starting index of the list
1217: lnk - linked list(an integer array) that is created
1218: lnklvl - levels of lnk
1219: bt - PetscBT (bitarray), bt[idx]=true marks idx is in lnk
1220: output Parameters:
1221: nlnk - number of newly added idx
1222: lnk - the sorted(increasing order) linked list containing new and non-redundate entries from idx
1223: lnklvl - levels of lnk
1224: bt - updated PetscBT (bitarray)
1225: */
1226: 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)
1227: {
1228: PetscFunctionBegin;
1229: PetscCall(PetscIncompleteLLAdd_Private(nidx, idx, level, idxlvl, idx_start, nlnk, lnk, lnklvl, bt, 0, PETSC_FALSE));
1230: PetscFunctionReturn(PETSC_SUCCESS);
1231: }
1233: /*
1234: Add a SORTED index set into a sorted linked list
1235: Input Parameters:
1236: nidx - number of input indices
1237: idx - sorted integer array used for storing column indices
1238: level - level of fill, e.g., ICC(level)
1239: idxlvl - level of idx
1240: idx_start - starting index of the list
1241: lnk - linked list(an integer array) that is created
1242: lnklvl - levels of lnk
1243: bt - PetscBT (bitarray), bt[idx]=true marks idx is in lnk
1244: output Parameters:
1245: nlnk - number of newly added idx
1246: lnk - the sorted(increasing order) linked list containing new and non-redundate entries from idx
1247: lnklvl - levels of lnk
1248: bt - updated PetscBT (bitarray)
1249: */
1250: 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)
1251: {
1252: PetscFunctionBegin;
1253: PetscCall(PetscIncompleteLLAdd_Private(nidx, idx, level, idxlvl, idx_start, nlnk, lnk, lnklvl, bt, 0, PETSC_TRUE));
1254: PetscFunctionReturn(PETSC_SUCCESS);
1255: }
1257: /*
1258: Copy data on the list into an array, then initialize the list
1259: Input Parameters:
1260: idx_start - starting index of the list
1261: lnk_max - max value of lnk indicating the end of the list
1262: nlnk - number of data on the list to be copied
1263: lnk - linked list
1264: lnklvl - level of lnk
1265: bt - PetscBT (bitarray), bt[idx]=true marks idx is in lnk
1266: output Parameters:
1267: indices - array that contains the copied data
1268: lnk - linked list that is cleaned and initialize
1269: lnklvl - level of lnk that is reinitialized
1270: bt - PetscBT (bitarray) with all bits set to false
1271: */
1272: 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)
1273: {
1274: PetscFunctionBegin;
1275: for (PetscInt j = 0, idx = idx_start; j < nlnk; ++j) {
1276: idx = lnk[idx];
1277: indices[j] = idx;
1278: indiceslvl[j] = lnklvl[idx];
1279: lnklvl[idx] = -1;
1280: PetscCall(PetscBTClear(bt, idx));
1281: }
1282: lnk[idx_start] = lnk_max;
1283: PetscFunctionReturn(PETSC_SUCCESS);
1284: }
1286: /*
1287: Free memories used by the list
1288: */
1289: #define PetscIncompleteLLDestroy(lnk, bt) ((PetscErrorCode)(PetscFree(lnk) || PetscBTDestroy(&(bt))))
1291: #if !PetscDefined(CLANG_STATIC_ANALYZER)
1292: #define MatCheckSameLocalSize(A, ar1, B, ar2) \
1293: do { \
1294: PetscCheckSameComm(A, ar1, B, ar2); \
1295: 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, \
1296: (A)->rmap->n, (A)->cmap->n, ar2, (B)->rmap->n, (B)->cmap->n); \
1297: } while (0)
1298: #define MatCheckSameSize(A, ar1, B, ar2) \
1299: do { \
1300: 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, \
1301: (A)->rmap->N, (A)->cmap->N, ar2, (B)->rmap->N, (B)->cmap->N); \
1302: MatCheckSameLocalSize(A, ar1, B, ar2); \
1303: } while (0)
1304: #else
1305: template <typename Tm>
1306: extern void MatCheckSameLocalSize(Tm, int, Tm, int);
1307: template <typename Tm>
1308: extern void MatCheckSameSize(Tm, int, Tm, int);
1309: #endif
1311: #define VecCheckMatCompatible(M, x, ar1, b, ar2) \
1312: do { \
1313: 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, \
1314: (M)->cmap->N); \
1315: 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, \
1316: (M)->rmap->N); \
1317: } while (0)
1319: /*
1320: Create and initialize a condensed linked list -
1321: same as PetscLLCreate(), but uses a scalable array 'lnk' with size of max number of entries, not O(N).
1322: Barry suggested this approach (Dec. 6, 2011):
1323: I've thought of an alternative way of representing a linked list that is efficient but doesn't have the O(N) scaling issue
1324: (it may be faster than the O(N) even sequentially due to less crazy memory access).
1326: 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
1327: 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
1328: 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
1329: 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.
1330: 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
1331: to each other so memory access is much better than using the big array.
1333: Example:
1334: nlnk_max=5, lnk_max=36:
1335: Initial list: [0, 0 | 36, 2 | 0, 0 | 0, 0 | 0, 0 | 0, 0 | 0, 0]
1336: here, head_node has index 2 with value lnk[2]=lnk_max=36,
1337: 0-th entry is used to store the number of entries in the list,
1338: The initial lnk represents head -> tail(marked by 36) with number of entries = lnk[0]=0.
1340: Now adding a sorted set {2,4}, the list becomes
1341: [2, 0 | 36, 4 |2, 6 | 4, 2 | 0, 0 | 0, 0 | 0, 0 ]
1342: represents head -> 2 -> 4 -> tail with number of entries = lnk[0]=2.
1344: Then adding a sorted set {0,3,35}, the list
1345: [5, 0 | 36, 8 | 2, 10 | 4, 12 | 0, 4 | 3, 6 | 35, 2 ]
1346: represents head -> 0 -> 2 -> 3 -> 4 -> 35 -> tail with number of entries = lnk[0]=5.
1348: Input Parameters:
1349: nlnk_max - max length of the list
1350: lnk_max - max value of the entries
1351: Output Parameters:
1352: lnk - list created and initialized
1353: bt - PetscBT (bitarray) with all bits set to false. Note: bt has size lnk_max, not nln_max!
1354: */
1355: static inline PetscErrorCode PetscLLCondensedCreate(PetscInt nlnk_max, PetscInt lnk_max, PetscInt **lnk, PetscBT *bt)
1356: {
1357: PetscInt *llnk, lsize = 0;
1359: PetscFunctionBegin;
1360: PetscCall(PetscIntMultError(2, nlnk_max + 2, &lsize));
1361: PetscCall(PetscMalloc1(lsize, lnk));
1362: PetscCall(PetscBTCreate(lnk_max, bt));
1363: llnk = *lnk;
1364: llnk[0] = 0; /* number of entries on the list */
1365: llnk[2] = lnk_max; /* value in the head node */
1366: llnk[3] = 2; /* next for the head node */
1367: PetscFunctionReturn(PETSC_SUCCESS);
1368: }
1370: /*
1371: Add a SORTED ascending index set into a sorted linked list. See PetscLLCondensedCreate() for detailed description.
1372: Input Parameters:
1373: nidx - number of input indices
1374: indices - sorted integer array
1375: lnk - condensed linked list(an integer array) that is created
1376: bt - PetscBT (bitarray), bt[idx]=true marks idx is in lnk
1377: output Parameters:
1378: lnk - the sorted(increasing order) linked list containing previous and newly added non-redundate indices
1379: bt - updated PetscBT (bitarray)
1380: */
1381: static inline PetscErrorCode PetscLLCondensedAddSorted(PetscInt nidx, const PetscInt indices[], PetscInt lnk[], PetscBT bt)
1382: {
1383: PetscInt location = 2; /* head */
1384: PetscInt nlnk = lnk[0]; /* num of entries on the input lnk */
1386: PetscFunctionBegin;
1387: for (PetscInt k = 0; k < nidx; k++) {
1388: const PetscInt entry = indices[k];
1389: if (!PetscBTLookupSet(bt, entry)) { /* new entry */
1390: PetscInt next, lnkdata;
1392: /* search for insertion location */
1393: do {
1394: next = location + 1; /* link from previous node to next node */
1395: location = lnk[next]; /* idx of next node */
1396: lnkdata = lnk[location]; /* value of next node */
1397: } while (entry > lnkdata);
1398: /* insertion location is found, add entry into lnk */
1399: const PetscInt newnode = 2 * (nlnk + 2); /* index for this new node */
1400: lnk[next] = newnode; /* connect previous node to the new node */
1401: lnk[newnode] = entry; /* set value of the new node */
1402: lnk[newnode + 1] = location; /* connect new node to next node */
1403: location = newnode; /* next search starts from the new node */
1404: nlnk++;
1405: }
1406: }
1407: lnk[0] = nlnk; /* number of entries in the list */
1408: PetscFunctionReturn(PETSC_SUCCESS);
1409: }
1411: static inline PetscErrorCode PetscLLCondensedClean(PetscInt lnk_max, PETSC_UNUSED PetscInt nidx, PetscInt *indices, PetscInt lnk[], PetscBT bt)
1412: {
1413: const PetscInt nlnk = lnk[0]; /* num of entries on the list */
1414: PetscInt next = lnk[3]; /* head node */
1416: PetscFunctionBegin;
1417: for (PetscInt k = 0; k < nlnk; k++) {
1418: indices[k] = lnk[next];
1419: next = lnk[next + 1];
1420: PetscCall(PetscBTClear(bt, indices[k]));
1421: }
1422: lnk[0] = 0; /* num of entries on the list */
1423: lnk[2] = lnk_max; /* initialize head node */
1424: lnk[3] = 2; /* head node */
1425: PetscFunctionReturn(PETSC_SUCCESS);
1426: }
1428: static inline PetscErrorCode PetscLLCondensedView(PetscInt *lnk)
1429: {
1430: PetscFunctionBegin;
1431: PetscCall(PetscPrintf(PETSC_COMM_SELF, "LLCondensed of size %" PetscInt_FMT ", (val, next)\n", lnk[0]));
1432: 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]));
1433: PetscFunctionReturn(PETSC_SUCCESS);
1434: }
1436: /*
1437: Free memories used by the list
1438: */
1439: static inline PetscErrorCode PetscLLCondensedDestroy(PetscInt *lnk, PetscBT bt)
1440: {
1441: PetscFunctionBegin;
1442: PetscCall(PetscFree(lnk));
1443: PetscCall(PetscBTDestroy(&bt));
1444: PetscFunctionReturn(PETSC_SUCCESS);
1445: }
1447: /*
1448: Same as PetscLLCondensedCreate(), but does not use non-scalable O(lnk_max) bitarray
1449: Input Parameters:
1450: nlnk_max - max length of the list
1451: Output Parameters:
1452: lnk - list created and initialized
1453: */
1454: static inline PetscErrorCode PetscLLCondensedCreate_Scalable(PetscInt nlnk_max, PetscInt **lnk)
1455: {
1456: PetscInt *llnk, lsize = 0;
1458: PetscFunctionBegin;
1459: PetscCall(PetscIntMultError(2, nlnk_max + 2, &lsize));
1460: PetscCall(PetscMalloc1(lsize, lnk));
1461: llnk = *lnk;
1462: llnk[0] = 0; /* number of entries on the list */
1463: llnk[2] = PETSC_INT_MAX; /* value in the head node */
1464: llnk[3] = 2; /* next for the head node */
1465: PetscFunctionReturn(PETSC_SUCCESS);
1466: }
1468: static inline PetscErrorCode PetscLLCondensedExpand_Scalable(PetscInt nlnk_max, PetscInt **lnk)
1469: {
1470: PetscInt lsize = 0;
1472: PetscFunctionBegin;
1473: PetscCall(PetscIntMultError(2, nlnk_max + 2, &lsize));
1474: PetscCall(PetscRealloc((size_t)lsize * sizeof(PetscInt), lnk));
1475: PetscFunctionReturn(PETSC_SUCCESS);
1476: }
1478: static inline PetscErrorCode PetscLLCondensedAddSorted_Scalable(PetscInt nidx, const PetscInt indices[], PetscInt lnk[])
1479: {
1480: PetscInt location = 2; /* head */
1481: PetscInt nlnk = lnk[0]; /* num of entries on the input lnk */
1483: for (PetscInt k = 0; k < nidx; k++) {
1484: const PetscInt entry = indices[k];
1485: PetscInt next, lnkdata;
1487: /* search for insertion location */
1488: do {
1489: next = location + 1; /* link from previous node to next node */
1490: location = lnk[next]; /* idx of next node */
1491: lnkdata = lnk[location]; /* value of next node */
1492: } while (entry > lnkdata);
1493: if (entry < lnkdata) {
1494: /* insertion location is found, add entry into lnk */
1495: const PetscInt newnode = 2 * (nlnk + 2); /* index for this new node */
1496: lnk[next] = newnode; /* connect previous node to the new node */
1497: lnk[newnode] = entry; /* set value of the new node */
1498: lnk[newnode + 1] = location; /* connect new node to next node */
1499: location = newnode; /* next search starts from the new node */
1500: nlnk++;
1501: }
1502: }
1503: lnk[0] = nlnk; /* number of entries in the list */
1504: return PETSC_SUCCESS;
1505: }
1507: static inline PetscErrorCode PetscLLCondensedClean_Scalable(PETSC_UNUSED PetscInt nidx, PetscInt *indices, PetscInt *lnk)
1508: {
1509: const PetscInt nlnk = lnk[0];
1510: PetscInt next = lnk[3]; /* head node */
1512: for (PetscInt k = 0; k < nlnk; k++) {
1513: indices[k] = lnk[next];
1514: next = lnk[next + 1];
1515: }
1516: lnk[0] = 0; /* num of entries on the list */
1517: lnk[3] = 2; /* head node */
1518: return PETSC_SUCCESS;
1519: }
1521: static inline PetscErrorCode PetscLLCondensedDestroy_Scalable(PetscInt *lnk)
1522: {
1523: return PetscFree(lnk);
1524: }
1526: /*
1527: lnk[0] number of links
1528: lnk[1] number of entries
1529: lnk[3n] value
1530: lnk[3n+1] len
1531: lnk[3n+2] link to next value
1533: The next three are always the first link
1535: lnk[3] PETSC_INT_MIN+1
1536: lnk[4] 1
1537: lnk[5] link to first real entry
1539: The next three are always the last link
1541: lnk[6] PETSC_INT_MAX - 1
1542: lnk[7] 1
1543: lnk[8] next valid link (this is the same as lnk[0] but without the decreases)
1544: */
1546: static inline PetscErrorCode PetscLLCondensedCreate_fast(PetscInt nlnk_max, PetscInt **lnk)
1547: {
1548: PetscInt *llnk;
1549: PetscInt lsize = 0;
1551: PetscFunctionBegin;
1552: PetscCall(PetscIntMultError(3, nlnk_max + 3, &lsize));
1553: PetscCall(PetscMalloc1(lsize, lnk));
1554: llnk = *lnk;
1555: llnk[0] = 0; /* nlnk: number of entries on the list */
1556: llnk[1] = 0; /* number of integer entries represented in list */
1557: llnk[3] = PETSC_INT_MIN + 1; /* value in the first node */
1558: llnk[4] = 1; /* count for the first node */
1559: llnk[5] = 6; /* next for the first node */
1560: llnk[6] = PETSC_INT_MAX - 1; /* value in the last node */
1561: llnk[7] = 1; /* count for the last node */
1562: llnk[8] = 0; /* next valid node to be used */
1563: PetscFunctionReturn(PETSC_SUCCESS);
1564: }
1566: static inline PetscErrorCode PetscLLCondensedAddSorted_fast(PetscInt nidx, const PetscInt indices[], PetscInt lnk[])
1567: {
1568: for (PetscInt k = 0, prev = 3 /* first value */; k < nidx; k++) {
1569: const PetscInt entry = indices[k];
1570: PetscInt next = lnk[prev + 2];
1572: /* search for insertion location */
1573: while (entry >= lnk[next]) {
1574: prev = next;
1575: next = lnk[next + 2];
1576: }
1577: /* entry is in range of previous list */
1578: if (entry < lnk[prev] + lnk[prev + 1]) continue;
1579: lnk[1]++;
1580: /* entry is right after previous list */
1581: if (entry == lnk[prev] + lnk[prev + 1]) {
1582: lnk[prev + 1]++;
1583: if (lnk[next] == entry + 1) { /* combine two contiguous strings */
1584: lnk[prev + 1] += lnk[next + 1];
1585: lnk[prev + 2] = lnk[next + 2];
1586: next = lnk[next + 2];
1587: lnk[0]--;
1588: }
1589: continue;
1590: }
1591: /* entry is right before next list */
1592: if (entry == lnk[next] - 1) {
1593: lnk[next]--;
1594: lnk[next + 1]++;
1595: prev = next;
1596: next = lnk[prev + 2];
1597: continue;
1598: }
1599: /* add entry into lnk */
1600: lnk[prev + 2] = 3 * ((lnk[8]++) + 3); /* connect previous node to the new node */
1601: prev = lnk[prev + 2];
1602: lnk[prev] = entry; /* set value of the new node */
1603: lnk[prev + 1] = 1; /* number of values in contiguous string is one to start */
1604: lnk[prev + 2] = next; /* connect new node to next node */
1605: lnk[0]++;
1606: }
1607: return PETSC_SUCCESS;
1608: }
1610: static inline PetscErrorCode PetscLLCondensedClean_fast(PETSC_UNUSED PetscInt nidx, PetscInt *indices, PetscInt *lnk)
1611: {
1612: const PetscInt nlnk = lnk[0];
1613: PetscInt next = lnk[5]; /* first node */
1615: for (PetscInt k = 0, cnt = 0; k < nlnk; k++) {
1616: for (PetscInt j = 0; j < lnk[next + 1]; j++) indices[cnt++] = lnk[next] + j;
1617: next = lnk[next + 2];
1618: }
1619: lnk[0] = 0; /* nlnk: number of links */
1620: lnk[1] = 0; /* number of integer entries represented in list */
1621: lnk[3] = PETSC_INT_MIN + 1; /* value in the first node */
1622: lnk[4] = 1; /* count for the first node */
1623: lnk[5] = 6; /* next for the first node */
1624: lnk[6] = PETSC_INT_MAX - 1; /* value in the last node */
1625: lnk[7] = 1; /* count for the last node */
1626: lnk[8] = 0; /* next valid location to make link */
1627: return PETSC_SUCCESS;
1628: }
1630: static inline PetscErrorCode PetscLLCondensedView_fast(const PetscInt *lnk)
1631: {
1632: const PetscInt nlnk = lnk[0];
1633: PetscInt next = lnk[5]; /* first node */
1635: for (PetscInt k = 0; k < nlnk; k++) {
1636: #if 0 /* Debugging code */
1637: printf("%d value %d len %d next %d\n", next, lnk[next], lnk[next + 1], lnk[next + 2]);
1638: #endif
1639: next = lnk[next + 2];
1640: }
1641: return PETSC_SUCCESS;
1642: }
1644: static inline PetscErrorCode PetscLLCondensedDestroy_fast(PetscInt *lnk)
1645: {
1646: return PetscFree(lnk);
1647: }
1649: PETSC_EXTERN PetscErrorCode PetscCDCreate(PetscInt, PetscCoarsenData **);
1650: PETSC_EXTERN PetscErrorCode PetscCDDestroy(PetscCoarsenData *);
1651: PETSC_EXTERN PetscErrorCode PetscCDIntNdSetID(PetscCDIntNd *, PetscInt);
1652: PETSC_EXTERN PetscErrorCode PetscCDIntNdGetID(const PetscCDIntNd *, PetscInt *);
1653: PETSC_EXTERN PetscErrorCode PetscCDAppendID(PetscCoarsenData *, PetscInt, PetscInt);
1654: PETSC_EXTERN PetscErrorCode PetscCDMoveAppend(PetscCoarsenData *, PetscInt, PetscInt);
1655: PETSC_EXTERN PetscErrorCode PetscCDAppendNode(PetscCoarsenData *, PetscInt, PetscCDIntNd *);
1656: PETSC_EXTERN PetscErrorCode PetscCDRemoveNextNode(PetscCoarsenData *, PetscInt, PetscCDIntNd *);
1657: PETSC_EXTERN PetscErrorCode PetscCDCountAt(const PetscCoarsenData *, PetscInt, PetscInt *);
1658: PETSC_EXTERN PetscErrorCode PetscCDIsEmptyAt(const PetscCoarsenData *, PetscInt, PetscBool *);
1659: PETSC_EXTERN PetscErrorCode PetscCDSetChunkSize(PetscCoarsenData *, PetscInt);
1660: PETSC_EXTERN PetscErrorCode PetscCDPrint(const PetscCoarsenData *, PetscInt, MPI_Comm);
1661: PETSC_EXTERN PetscErrorCode PetscCDGetNonemptyIS(PetscCoarsenData *, IS *);
1662: PETSC_EXTERN PetscErrorCode PetscCDGetMat(PetscCoarsenData *, Mat *);
1663: PETSC_EXTERN PetscErrorCode PetscCDSetMat(PetscCoarsenData *, Mat);
1664: PETSC_EXTERN PetscErrorCode PetscCDClearMat(PetscCoarsenData *);
1665: PETSC_EXTERN PetscErrorCode PetscCDRemoveAllAt(PetscCoarsenData *, PetscInt);
1666: PETSC_EXTERN PetscErrorCode PetscCDCount(const PetscCoarsenData *, PetscInt *_sz);
1668: PETSC_EXTERN PetscErrorCode PetscCDGetHeadPos(const PetscCoarsenData *, PetscInt, PetscCDIntNd **);
1669: PETSC_EXTERN PetscErrorCode PetscCDGetNextPos(const PetscCoarsenData *, PetscInt, PetscCDIntNd **);
1670: PETSC_EXTERN PetscErrorCode PetscCDGetASMBlocks(const PetscCoarsenData *, const PetscInt, PetscInt *, IS **);
1672: PETSC_SINGLE_LIBRARY_VISIBILITY_INTERNAL PetscErrorCode MatFDColoringApply_AIJ(Mat, MatFDColoring, Vec, void *);
1674: typedef struct {
1675: Vec diag;
1676: PetscBool diag_valid;
1677: Vec inv_diag;
1678: PetscBool inv_diag_valid;
1679: PetscObjectState diag_state, inv_diag_state;
1680: PetscInt *col;
1681: PetscScalar *val;
1682: } Mat_Diagonal;
1684: #if PetscDefined(HAVE_CUDA)
1685: PETSC_INTERN PetscErrorCode MatADot_Diagonal_SeqCUDA(Mat, Vec, Vec, PetscScalar *);
1686: PETSC_INTERN PetscErrorCode MatANormSq_Diagonal_SeqCUDA(Mat, Vec, PetscReal *);
1687: #endif
1688: #if PetscDefined(HAVE_HIP)
1689: PETSC_INTERN PetscErrorCode MatADot_Diagonal_SeqHIP(Mat, Vec, Vec, PetscScalar *);
1690: PETSC_INTERN PetscErrorCode MatANormSq_Diagonal_SeqHIP(Mat, Vec, PetscReal *);
1691: #endif
1692: #if PetscDefined(HAVE_KOKKOS_KERNELS)
1693: PETSC_INTERN PetscErrorCode MatADot_Diagonal_SeqKokkos(Mat, Vec, Vec, PetscScalar *);
1694: PETSC_INTERN PetscErrorCode MatANormSq_Diagonal_SeqKokkos(Mat, Vec, PetscReal *);
1695: #endif
1697: PETSC_EXTERN PetscLogEvent MAT_Mult;
1698: PETSC_EXTERN PetscLogEvent MAT_MultAdd;
1699: PETSC_EXTERN PetscLogEvent MAT_MultTranspose;
1700: PETSC_EXTERN PetscLogEvent MAT_MultHermitianTranspose;
1701: PETSC_EXTERN PetscLogEvent MAT_MultTransposeAdd;
1702: PETSC_EXTERN PetscLogEvent MAT_MultHermitianTransposeAdd;
1703: PETSC_EXTERN PetscLogEvent MAT_ADot;
1704: PETSC_EXTERN PetscLogEvent MAT_ANorm;
1705: PETSC_EXTERN PetscLogEvent MAT_Solve;
1706: PETSC_EXTERN PetscLogEvent MAT_Solves;
1707: PETSC_EXTERN PetscLogEvent MAT_SolveAdd;
1708: PETSC_EXTERN PetscLogEvent MAT_SolveTranspose;
1709: PETSC_EXTERN PetscLogEvent MAT_SolveTransposeAdd;
1710: PETSC_EXTERN PetscLogEvent MAT_SOR;
1711: PETSC_EXTERN PetscLogEvent MAT_ForwardSolve;
1712: PETSC_EXTERN PetscLogEvent MAT_BackwardSolve;
1713: PETSC_EXTERN PetscLogEvent MAT_LUFactor;
1714: PETSC_EXTERN PetscLogEvent MAT_LUFactorSymbolic;
1715: PETSC_EXTERN PetscLogEvent MAT_LUFactorNumeric;
1716: PETSC_EXTERN PetscLogEvent MAT_QRFactor;
1717: PETSC_EXTERN PetscLogEvent MAT_QRFactorSymbolic;
1718: PETSC_EXTERN PetscLogEvent MAT_QRFactorNumeric;
1719: PETSC_EXTERN PetscLogEvent MAT_CholeskyFactor;
1720: PETSC_EXTERN PetscLogEvent MAT_CholeskyFactorSymbolic;
1721: PETSC_EXTERN PetscLogEvent MAT_CholeskyFactorNumeric;
1722: PETSC_EXTERN PetscLogEvent MAT_ILUFactor;
1723: PETSC_EXTERN PetscLogEvent MAT_ILUFactorSymbolic;
1724: PETSC_EXTERN PetscLogEvent MAT_ICCFactorSymbolic;
1725: PETSC_EXTERN PetscLogEvent MAT_Copy;
1726: PETSC_EXTERN PetscLogEvent MAT_Convert;
1727: PETSC_EXTERN PetscLogEvent MAT_Scale;
1728: PETSC_EXTERN PetscLogEvent MAT_AssemblyBegin;
1729: PETSC_EXTERN PetscLogEvent MAT_AssemblyEnd;
1730: PETSC_EXTERN PetscLogEvent MAT_SetValues;
1731: PETSC_EXTERN PetscLogEvent MAT_GetValues;
1732: PETSC_EXTERN PetscLogEvent MAT_GetRow;
1733: PETSC_EXTERN PetscLogEvent MAT_GetRowIJ;
1734: PETSC_EXTERN PetscLogEvent MAT_CreateSubMats;
1735: PETSC_EXTERN PetscLogEvent MAT_GetOrdering;
1736: PETSC_EXTERN PetscLogEvent MAT_RedundantMat;
1737: PETSC_EXTERN PetscLogEvent MAT_IncreaseOverlap;
1738: PETSC_EXTERN PetscLogEvent MAT_Partitioning;
1739: PETSC_EXTERN PetscLogEvent MAT_PartitioningND;
1740: PETSC_EXTERN PetscLogEvent MAT_Coarsen;
1741: PETSC_EXTERN PetscLogEvent MAT_ZeroEntries;
1742: PETSC_EXTERN PetscLogEvent MAT_Load;
1743: PETSC_EXTERN PetscLogEvent MAT_View;
1744: PETSC_EXTERN PetscLogEvent MAT_AXPY;
1745: PETSC_EXTERN PetscLogEvent MAT_FDColoringCreate;
1746: PETSC_EXTERN PetscLogEvent MAT_TransposeColoringCreate;
1747: PETSC_EXTERN PetscLogEvent MAT_FDColoringSetUp;
1748: PETSC_EXTERN PetscLogEvent MAT_FDColoringApply;
1749: PETSC_EXTERN PetscLogEvent MAT_Transpose;
1750: PETSC_EXTERN PetscLogEvent MAT_FDColoringFunction;
1751: PETSC_EXTERN PetscLogEvent MAT_CreateSubMat;
1752: PETSC_EXTERN PetscLogEvent MAT_MatSolve;
1753: PETSC_EXTERN PetscLogEvent MAT_MatTrSolve;
1754: PETSC_EXTERN PetscLogEvent MAT_MatMultSymbolic;
1755: PETSC_EXTERN PetscLogEvent MAT_MatMultNumeric;
1756: PETSC_EXTERN PetscLogEvent MAT_Getlocalmatcondensed;
1757: PETSC_EXTERN PetscLogEvent MAT_GetBrowsOfAcols;
1758: PETSC_EXTERN PetscLogEvent MAT_GetBrowsOfAocols;
1759: PETSC_EXTERN PetscLogEvent MAT_PtAPSymbolic;
1760: PETSC_EXTERN PetscLogEvent MAT_PtAPNumeric;
1761: PETSC_EXTERN PetscLogEvent MAT_Seqstompinum;
1762: PETSC_EXTERN PetscLogEvent MAT_Seqstompisym;
1763: PETSC_EXTERN PetscLogEvent MAT_Seqstompi;
1764: PETSC_EXTERN PetscLogEvent MAT_Getlocalmat;
1765: PETSC_EXTERN PetscLogEvent MAT_RARtSymbolic;
1766: PETSC_EXTERN PetscLogEvent MAT_RARtNumeric;
1767: PETSC_EXTERN PetscLogEvent MAT_MatTransposeMultSymbolic;
1768: PETSC_EXTERN PetscLogEvent MAT_MatTransposeMultNumeric;
1769: PETSC_EXTERN PetscLogEvent MAT_TransposeMatMultSymbolic;
1770: PETSC_EXTERN PetscLogEvent MAT_TransposeMatMultNumeric;
1771: PETSC_EXTERN PetscLogEvent MAT_MatMatMultSymbolic;
1772: PETSC_EXTERN PetscLogEvent MAT_MatMatMultNumeric;
1773: PETSC_EXTERN PetscLogEvent MAT_Getsymtransreduced;
1774: PETSC_EXTERN PetscLogEvent MAT_GetSeqNonzeroStructure;
1775: PETSC_EXTERN PetscLogEvent MATMFFD_Mult;
1776: PETSC_EXTERN PetscLogEvent MAT_GetMultiProcBlock;
1777: PETSC_EXTERN PetscLogEvent MAT_CUSPARSECopyToGPU;
1778: PETSC_EXTERN PetscLogEvent MAT_CUSPARSECopyFromGPU;
1779: PETSC_EXTERN PetscLogEvent MAT_CUSPARSEGenerateTranspose;
1780: PETSC_EXTERN PetscLogEvent MAT_CUSPARSESolveAnalysis;
1781: PETSC_EXTERN PetscLogEvent MAT_HIPSPARSECopyToGPU;
1782: PETSC_EXTERN PetscLogEvent MAT_HIPSPARSECopyFromGPU;
1783: PETSC_EXTERN PetscLogEvent MAT_HIPSPARSEGenerateTranspose;
1784: PETSC_EXTERN PetscLogEvent MAT_HIPSPARSESolveAnalysis;
1785: PETSC_EXTERN PetscLogEvent MAT_SetValuesBatch;
1786: PETSC_EXTERN PetscLogEvent MAT_CreateGraph;
1787: PETSC_EXTERN PetscLogEvent MAT_ViennaCLCopyToGPU;
1788: PETSC_EXTERN PetscLogEvent MAT_DenseCopyToGPU;
1789: PETSC_EXTERN PetscLogEvent MAT_DenseCopyFromGPU;
1790: PETSC_EXTERN PetscLogEvent MAT_Merge;
1791: PETSC_EXTERN PetscLogEvent MAT_Residual;
1792: PETSC_EXTERN PetscLogEvent MAT_SetRandom;
1793: PETSC_EXTERN PetscLogEvent MAT_FactorFactS;
1794: PETSC_EXTERN PetscLogEvent MAT_FactorInvS;
1795: PETSC_EXTERN PetscLogEvent MAT_PreallCOO;
1796: PETSC_EXTERN PetscLogEvent MAT_SetVCOO;
1797: PETSC_EXTERN PetscLogEvent MATCOLORING_Apply;
1798: PETSC_EXTERN PetscLogEvent MATCOLORING_Comm;
1799: PETSC_EXTERN PetscLogEvent MATCOLORING_Local;
1800: PETSC_EXTERN PetscLogEvent MATCOLORING_ISCreate;
1801: PETSC_EXTERN PetscLogEvent MATCOLORING_SetUp;
1802: PETSC_EXTERN PetscLogEvent MATCOLORING_Weights;
1803: PETSC_EXTERN PetscLogEvent MAT_H2Opus_Build;
1804: PETSC_EXTERN PetscLogEvent MAT_H2Opus_Compress;
1805: PETSC_EXTERN PetscLogEvent MAT_H2Opus_Orthog;
1806: PETSC_EXTERN PetscLogEvent MAT_H2Opus_LR;
1807: PETSC_EXTERN PetscLogEvent MAT_CUDACopyToGPU;
1808: PETSC_EXTERN PetscLogEvent MAT_HIPCopyToGPU;
1810: #if PetscDefined(CLANG_STATIC_ANALYZER)
1811: #define MatGetDiagonalMarkers(SeqXXX, bs)
1812: #else
1813: /*
1814: Adds diagonal pointers to sparse matrix nonzero structure and determines if all diagonal entries are present
1816: Rechecks the matrix data structure automatically if the nonzero structure of the matrix changed since the last call
1818: Potential optimization: since the a->j[j] are sorted this could use bisection to find the diagonal
1820: Developer Note:
1821: Uses the C preprocessor as a template mechanism to produce MatGetDiagonal_Seq[SB]AIJ() to avoid duplicate code
1822: */
1823: #define MatGetDiagonalMarkers(SeqXXX, bs) \
1824: PetscErrorCode MatGetDiagonalMarkers_##SeqXXX(Mat A, const PetscInt **diag, PetscBool *diagDense) \
1825: { \
1826: Mat_##SeqXXX *a = (Mat_##SeqXXX *)A->data; \
1827: \
1828: PetscFunctionBegin; \
1829: if (A->factortype != MAT_FACTOR_NONE) { \
1830: if (diagDense) *diagDense = PETSC_TRUE; \
1831: if (diag) *diag = a->diag; \
1832: PetscFunctionReturn(PETSC_SUCCESS); \
1833: } \
1834: PetscCheck(diag || diagDense, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "At least one of diag or diagDense must be requested"); \
1835: if (a->diagNonzeroState != A->nonzerostate || (diag && !a->diag)) { \
1836: const PetscInt m = A->rmap->n / bs; \
1837: \
1838: if (!diag && !a->diag) { \
1839: a->diagDense = PETSC_TRUE; \
1840: for (PetscInt i = 0; i < m; i++) { \
1841: PetscBool found = PETSC_FALSE; \
1842: \
1843: for (PetscInt j = a->i[i]; j < a->i[i + 1]; j++) { \
1844: if (a->j[j] == i) { \
1845: found = PETSC_TRUE; \
1846: break; \
1847: } \
1848: } \
1849: if (!found) { \
1850: a->diagDense = PETSC_FALSE; \
1851: *diagDense = a->diagDense; \
1852: a->diagNonzeroState = A->nonzerostate; \
1853: PetscFunctionReturn(PETSC_SUCCESS); \
1854: } \
1855: } \
1856: } else { \
1857: if (!a->diag) PetscCall(PetscMalloc1(m, &a->diag)); \
1858: a->diagDense = PETSC_TRUE; \
1859: for (PetscInt i = 0; i < m; i++) { \
1860: PetscBool found = PETSC_FALSE; \
1861: \
1862: a->diag[i] = a->i[i + 1]; \
1863: for (PetscInt j = a->i[i]; j < a->i[i + 1]; j++) { \
1864: if (a->j[j] == i) { \
1865: a->diag[i] = j; \
1866: found = PETSC_TRUE; \
1867: break; \
1868: } \
1869: } \
1870: if (!found) a->diagDense = PETSC_FALSE; \
1871: } \
1872: } \
1873: a->diagNonzeroState = A->nonzerostate; \
1874: } \
1875: if (diag) *diag = a->diag; \
1876: if (diagDense) *diagDense = a->diagDense; \
1877: PetscFunctionReturn(PETSC_SUCCESS); \
1878: }
1879: #endif