xref: /petsc/src/mat/impls/aij/seq/aij.h (revision e874ec00d637a86419bb2cc912cf88b33e5547ef)
1 
2 #ifndef __AIJ_H
3 #define __AIJ_H
4 
5 #include <petsc/private/matimpl.h>
6 #include <petsc/private/hashmapi.h>
7 
8 /* Operations provided by MATSEQAIJ and its subclasses */
9 typedef struct {
10   PetscErrorCode (*getarray)(Mat, PetscScalar **);
11   PetscErrorCode (*restorearray)(Mat, PetscScalar **);
12   PetscErrorCode (*getarrayread)(Mat, const PetscScalar **);
13   PetscErrorCode (*restorearrayread)(Mat, const PetscScalar **);
14   PetscErrorCode (*getarraywrite)(Mat, PetscScalar **);
15   PetscErrorCode (*restorearraywrite)(Mat, PetscScalar **);
16   PetscErrorCode (*getcsrandmemtype)(Mat, const PetscInt **, const PetscInt **, PetscScalar **, PetscMemType *);
17 } Mat_SeqAIJOps;
18 
19 /*
20     Struct header shared by SeqAIJ, SeqBAIJ and SeqSBAIJ matrix formats
21 */
22 #define SEQAIJHEADER(datatype) \
23   PetscBool         roworiented;  /* if true, row-oriented input, default */ \
24   PetscInt          nonew;        /* 1 don't add new nonzeros, -1 generate error on new */ \
25   PetscInt          nounused;     /* -1 generate error on unused space */ \
26   PetscBool         singlemalloc; /* if true a, i, and j have been obtained with one big malloc */ \
27   PetscInt          maxnz;        /* allocated nonzeros */ \
28   PetscInt         *imax;         /* maximum space allocated for each row */ \
29   PetscInt         *ilen;         /* actual length of each row */ \
30   PetscInt         *ipre;         /* space preallocated for each row by user */ \
31   PetscBool         free_imax_ilen; \
32   PetscInt          reallocs;           /* number of mallocs done during MatSetValues() \
33                                         as more values are set than were prealloced */ \
34   PetscInt          rmax;               /* max nonzeros in any row */ \
35   PetscBool         keepnonzeropattern; /* keeps matrix structure same in calls to MatZeroRows()*/ \
36   PetscBool         ignorezeroentries; \
37   PetscBool         free_ij;       /* free the column indices j and row offsets i when the matrix is destroyed */ \
38   PetscBool         free_a;        /* free the numerical values when matrix is destroy */ \
39   Mat_CompressedRow compressedrow; /* use compressed row format */ \
40   PetscInt          nz;            /* nonzeros */ \
41   PetscInt         *i;             /* pointer to beginning of each row */ \
42   PetscInt         *j;             /* column values: j + i[k] - 1 is start of row k */ \
43   PetscInt         *diag;          /* pointers to diagonal elements */ \
44   PetscInt          nonzerorowcnt; /* how many rows have nonzero entries */ \
45   PetscBool         free_diag; \
46   datatype         *a;              /* nonzero elements */ \
47   PetscScalar      *solve_work;     /* work space used in MatSolve */ \
48   IS                row, col, icol; /* index sets, used for reorderings */ \
49   PetscBool         pivotinblocks;  /* pivot inside factorization of each diagonal block */ \
50   Mat               parent;         /* set if this matrix was formed with MatDuplicate(...,MAT_SHARE_NONZERO_PATTERN,....); \
51                                          means that this shares some data structures with the parent including diag, ilen, imax, i, j */ \
52   Mat_SubSppt      *submatis1;      /* used by MatCreateSubMatrices_MPIXAIJ_Local */ \
53   Mat_SeqAIJOps     ops[1]          /* operations for SeqAIJ and its subclasses */
54 
55 typedef struct {
56   MatTransposeColoring matcoloring;
57   Mat                  Bt_den;  /* dense matrix of B^T */
58   Mat                  ABt_den; /* dense matrix of A*B^T */
59   PetscBool            usecoloring;
60 } Mat_MatMatTransMult;
61 
62 typedef struct { /* used by MatTransposeMatMult() */
63   Mat At;        /* transpose of the first matrix */
64   Mat mA;        /* maij matrix of A */
65   Vec bt, ct;    /* vectors to hold locally transposed arrays of B and C */
66   /* used by PtAP */
67   void *data;
68   PetscErrorCode (*destroy)(void *);
69 } Mat_MatTransMatMult;
70 
71 typedef struct {
72   PetscInt    *api, *apj; /* symbolic structure of A*P */
73   PetscScalar *apa;       /* temporary array for storing one row of A*P */
74 } Mat_AP;
75 
76 typedef struct {
77   MatTransposeColoring matcoloring;
78   Mat                  Rt;   /* sparse or dense matrix of R^T */
79   Mat                  RARt; /* dense matrix of R*A*R^T */
80   Mat                  ARt;  /* A*R^T used for the case -matrart_color_art */
81   MatScalar           *work; /* work array to store columns of A*R^T used in MatMatMatMultNumeric_SeqAIJ_SeqAIJ_SeqDense() */
82   /* free intermediate products needed for PtAP */
83   void *data;
84   PetscErrorCode (*destroy)(void *);
85 } Mat_RARt;
86 
87 typedef struct {
88   Mat BC; /* temp matrix for storing B*C */
89 } Mat_MatMatMatMult;
90 
91 /*
92   MATSEQAIJ format - Compressed row storage (also called Yale sparse matrix
93   format) or compressed sparse row (CSR).  The i[] and j[] arrays start at 0. For example,
94   j[i[k]+p] is the pth column in row k.  Note that the diagonal
95   matrix elements are stored with the rest of the nonzeros (not separately).
96 */
97 
98 /* Info about i-nodes (identical nodes) helper class for SeqAIJ */
99 typedef struct {
100   MatScalar *bdiag, *ibdiag, *ssor_work; /* diagonal blocks of matrix used for MatSOR_SeqAIJ_Inode() */
101   PetscInt   bdiagsize;                  /* length of bdiag and ibdiag */
102   PetscBool  ibdiagvalid;                /* do ibdiag[] and bdiag[] contain the most recent values */
103 
104   PetscBool        use;
105   PetscInt         node_count;       /* number of inodes */
106   PetscInt        *size;             /* size of each inode */
107   PetscInt         limit;            /* inode limit */
108   PetscInt         max_limit;        /* maximum supported inode limit */
109   PetscBool        checked;          /* if inodes have been checked for */
110   PetscObjectState mat_nonzerostate; /* non-zero state when inodes were checked for */
111 } Mat_SeqAIJ_Inode;
112 
113 PETSC_INTERN PetscErrorCode MatView_SeqAIJ_Inode(Mat, PetscViewer);
114 PETSC_INTERN PetscErrorCode MatAssemblyEnd_SeqAIJ_Inode(Mat, MatAssemblyType);
115 PETSC_INTERN PetscErrorCode MatDestroy_SeqAIJ_Inode(Mat);
116 PETSC_INTERN PetscErrorCode MatCreate_SeqAIJ_Inode(Mat);
117 PETSC_INTERN PetscErrorCode MatSetOption_SeqAIJ_Inode(Mat, MatOption, PetscBool);
118 PETSC_INTERN PetscErrorCode MatDuplicate_SeqAIJ_Inode(Mat, MatDuplicateOption, Mat *);
119 PETSC_INTERN PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat, Mat, MatDuplicateOption, PetscBool);
120 PETSC_INTERN PetscErrorCode MatLUFactorNumeric_SeqAIJ_Inode_inplace(Mat, Mat, const MatFactorInfo *);
121 PETSC_INTERN PetscErrorCode MatLUFactorNumeric_SeqAIJ_Inode(Mat, Mat, const MatFactorInfo *);
122 PETSC_INTERN PetscErrorCode MatSeqAIJGetArray_SeqAIJ(Mat, PetscScalar **);
123 PETSC_INTERN PetscErrorCode MatSeqAIJRestoreArray_SeqAIJ(Mat, PetscScalar **);
124 
125 typedef struct {
126   SEQAIJHEADER(MatScalar);
127   Mat_SeqAIJ_Inode inode;
128   MatScalar       *saved_values; /* location for stashing nonzero values of matrix */
129 
130   PetscScalar *idiag, *mdiag, *ssor_work; /* inverse of diagonal entries, diagonal values and workspace for Eisenstat trick */
131   PetscBool    idiagvalid;                /* current idiag[] and mdiag[] are valid */
132   PetscScalar *ibdiag;                    /* inverses of block diagonals */
133   PetscBool    ibdiagvalid;               /* inverses of block diagonals are valid. */
134   PetscBool    diagonaldense;             /* all entries along the diagonal have been set; i.e. no missing diagonal terms */
135   PetscScalar  fshift, omega;             /* last used omega and fshift */
136 
137   /* MatSetValuesCOO() related fields on host */
138   PetscCount  coo_n; /* Number of entries in MatSetPreallocationCOO() */
139   PetscCount  Atot;  /* Total number of valid (i.e., w/ non-negative indices) entries in the COO array */
140   PetscCount *jmap;  /* perm[jmap[i]..jmap[i+1]) give indices of entries in v[] associated with i-th nonzero of the matrix */
141   PetscCount *perm;  /* The permutation array in sorting (i,j) by row and then by col */
142 } Mat_SeqAIJ;
143 
144 /*
145   Frees the a, i, and j arrays from the XAIJ (AIJ, BAIJ, and SBAIJ) matrix types
146 */
147 static inline PetscErrorCode MatSeqXAIJFreeAIJ(Mat AA, MatScalar **a, PetscInt **j, PetscInt **i)
148 {
149   Mat_SeqAIJ *A = (Mat_SeqAIJ *)AA->data;
150   if (A->singlemalloc) {
151     PetscCall(PetscFree3(*a, *j, *i));
152   } else {
153     if (A->free_a) PetscCall(PetscFree(*a));
154     if (A->free_ij) PetscCall(PetscFree(*j));
155     if (A->free_ij) PetscCall(PetscFree(*i));
156   }
157   return 0;
158 }
159 /*
160     Allocates larger a, i, and j arrays for the XAIJ (AIJ, BAIJ, and SBAIJ) matrix types
161     This is a macro because it takes the datatype as an argument which can be either a Mat or a MatScalar
162 */
163 #define MatSeqXAIJReallocateAIJ(Amat, AM, BS2, NROW, ROW, COL, RMAX, AA, AI, AJ, RP, AP, AIMAX, NONEW, datatype) \
164   if (NROW >= RMAX) { \
165     Mat_SeqAIJ *Ain = (Mat_SeqAIJ *)Amat->data; \
166     /* there is no extra room in row, therefore enlarge */ \
167     PetscInt  CHUNKSIZE = 15, new_nz = AI[AM] + CHUNKSIZE, len, *new_i = NULL, *new_j = NULL; \
168     datatype *new_a; \
169 \
170     PetscCheck(NONEW != -2, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "New nonzero at (%" PetscInt_FMT ",%" PetscInt_FMT ") caused a malloc\nUse MatSetOption(A, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE) to turn off this check", ROW, COL); \
171     /* malloc new storage space */ \
172     PetscCall(PetscMalloc3(BS2 *new_nz, &new_a, new_nz, &new_j, AM + 1, &new_i)); \
173 \
174     /* copy over old data into new slots */ \
175     for (ii = 0; ii < ROW + 1; ii++) new_i[ii] = AI[ii]; \
176     for (ii = ROW + 1; ii < AM + 1; ii++) new_i[ii] = AI[ii] + CHUNKSIZE; \
177     PetscCall(PetscArraycpy(new_j, AJ, AI[ROW] + NROW)); \
178     len = (new_nz - CHUNKSIZE - AI[ROW] - NROW); \
179     PetscCall(PetscArraycpy(new_j + AI[ROW] + NROW + CHUNKSIZE, AJ + AI[ROW] + NROW, len)); \
180     PetscCall(PetscArraycpy(new_a, AA, BS2 *(AI[ROW] + NROW))); \
181     PetscCall(PetscArrayzero(new_a + BS2 * (AI[ROW] + NROW), BS2 * CHUNKSIZE)); \
182     PetscCall(PetscArraycpy(new_a + BS2 * (AI[ROW] + NROW + CHUNKSIZE), AA + BS2 * (AI[ROW] + NROW), BS2 * len)); \
183     /* free up old matrix storage */ \
184     PetscCall(MatSeqXAIJFreeAIJ(A, &Ain->a, &Ain->j, &Ain->i)); \
185     AA     = new_a; \
186     Ain->a = (MatScalar *)new_a; \
187     AI = Ain->i = new_i; \
188     AJ = Ain->j       = new_j; \
189     Ain->singlemalloc = PETSC_TRUE; \
190 \
191     RP   = AJ + AI[ROW]; \
192     AP   = AA + BS2 * AI[ROW]; \
193     RMAX = AIMAX[ROW] = AIMAX[ROW] + CHUNKSIZE; \
194     Ain->maxnz += BS2 * CHUNKSIZE; \
195     Ain->reallocs++; \
196   }
197 
198 #define MatSeqXAIJReallocateAIJ_structure_only(Amat, AM, BS2, NROW, ROW, COL, RMAX, AI, AJ, RP, AIMAX, NONEW, datatype) \
199   if (NROW >= RMAX) { \
200     Mat_SeqAIJ *Ain = (Mat_SeqAIJ *)Amat->data; \
201     /* there is no extra room in row, therefore enlarge */ \
202     PetscInt CHUNKSIZE = 15, new_nz = AI[AM] + CHUNKSIZE, len, *new_i = NULL, *new_j = NULL; \
203 \
204     PetscCheck(NONEW != -2, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "New nonzero at (%" PetscInt_FMT ",%" PetscInt_FMT ") caused a malloc\nUse MatSetOption(A, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE) to turn off this check", ROW, COL); \
205     /* malloc new storage space */ \
206     PetscCall(PetscMalloc1(new_nz, &new_j)); \
207     PetscCall(PetscMalloc1(AM + 1, &new_i)); \
208 \
209     /* copy over old data into new slots */ \
210     for (ii = 0; ii < ROW + 1; ii++) new_i[ii] = AI[ii]; \
211     for (ii = ROW + 1; ii < AM + 1; ii++) new_i[ii] = AI[ii] + CHUNKSIZE; \
212     PetscCall(PetscArraycpy(new_j, AJ, AI[ROW] + NROW)); \
213     len = (new_nz - CHUNKSIZE - AI[ROW] - NROW); \
214     PetscCall(PetscArraycpy(new_j + AI[ROW] + NROW + CHUNKSIZE, AJ + AI[ROW] + NROW, len)); \
215 \
216     /* free up old matrix storage */ \
217     PetscCall(MatSeqXAIJFreeAIJ(A, &Ain->a, &Ain->j, &Ain->i)); \
218     Ain->a = NULL; \
219     AI = Ain->i = new_i; \
220     AJ = Ain->j       = new_j; \
221     Ain->singlemalloc = PETSC_FALSE; \
222     Ain->free_a       = PETSC_FALSE; \
223 \
224     RP   = AJ + AI[ROW]; \
225     RMAX = AIMAX[ROW] = AIMAX[ROW] + CHUNKSIZE; \
226     Ain->maxnz += BS2 * CHUNKSIZE; \
227     Ain->reallocs++; \
228   }
229 
230 PETSC_INTERN PetscErrorCode MatSeqAIJSetPreallocation_SeqAIJ(Mat, PetscInt, const PetscInt *);
231 PETSC_INTERN PetscErrorCode MatSetPreallocationCOO_SeqAIJ(Mat, PetscCount, PetscInt[], PetscInt[]);
232 PETSC_INTERN PetscErrorCode MatResetPreallocationCOO_SeqAIJ(Mat);
233 
234 PETSC_INTERN PetscErrorCode MatILUFactorSymbolic_SeqAIJ_inplace(Mat, Mat, IS, IS, const MatFactorInfo *);
235 PETSC_INTERN PetscErrorCode MatILUFactorSymbolic_SeqAIJ(Mat, Mat, IS, IS, const MatFactorInfo *);
236 PETSC_INTERN PetscErrorCode MatILUFactorSymbolic_SeqAIJ_ilu0(Mat, Mat, IS, IS, const MatFactorInfo *);
237 
238 PETSC_INTERN PetscErrorCode MatICCFactorSymbolic_SeqAIJ_inplace(Mat, Mat, IS, const MatFactorInfo *);
239 PETSC_INTERN PetscErrorCode MatICCFactorSymbolic_SeqAIJ(Mat, Mat, IS, const MatFactorInfo *);
240 PETSC_INTERN PetscErrorCode MatCholeskyFactorSymbolic_SeqAIJ_inplace(Mat, Mat, IS, const MatFactorInfo *);
241 PETSC_INTERN PetscErrorCode MatCholeskyFactorSymbolic_SeqAIJ(Mat, Mat, IS, const MatFactorInfo *);
242 PETSC_INTERN PetscErrorCode MatCholeskyFactorNumeric_SeqAIJ_inplace(Mat, Mat, const MatFactorInfo *);
243 PETSC_INTERN PetscErrorCode MatCholeskyFactorNumeric_SeqAIJ(Mat, Mat, const MatFactorInfo *);
244 PETSC_INTERN PetscErrorCode MatDuplicate_SeqAIJ(Mat, MatDuplicateOption, Mat *);
245 PETSC_INTERN PetscErrorCode MatCopy_SeqAIJ(Mat, Mat, MatStructure);
246 PETSC_INTERN PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat, PetscBool *, PetscInt *);
247 PETSC_INTERN PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat);
248 PETSC_INTERN PetscErrorCode MatFindZeroDiagonals_SeqAIJ_Private(Mat, PetscInt *, PetscInt **);
249 
250 PETSC_INTERN PetscErrorCode MatMult_SeqAIJ(Mat, Vec, Vec);
251 PETSC_INTERN PetscErrorCode MatMult_SeqAIJ_Inode(Mat, Vec, Vec);
252 PETSC_INTERN PetscErrorCode MatMultAdd_SeqAIJ(Mat, Vec, Vec, Vec);
253 PETSC_INTERN PetscErrorCode MatMultAdd_SeqAIJ_Inode(Mat, Vec, Vec, Vec);
254 PETSC_INTERN PetscErrorCode MatMultTranspose_SeqAIJ(Mat, Vec, Vec);
255 PETSC_INTERN PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat, Vec, Vec, Vec);
256 PETSC_INTERN PetscErrorCode MatSOR_SeqAIJ(Mat, Vec, PetscReal, MatSORType, PetscReal, PetscInt, PetscInt, Vec);
257 PETSC_INTERN PetscErrorCode MatSOR_SeqAIJ_Inode(Mat, Vec, PetscReal, MatSORType, PetscReal, PetscInt, PetscInt, Vec);
258 
259 PETSC_INTERN PetscErrorCode MatSetOption_SeqAIJ(Mat, MatOption, PetscBool);
260 
261 PETSC_INTERN PetscErrorCode MatGetSymbolicTranspose_SeqAIJ(Mat, PetscInt *[], PetscInt *[]);
262 PETSC_INTERN PetscErrorCode MatRestoreSymbolicTranspose_SeqAIJ(Mat, PetscInt *[], PetscInt *[]);
263 PETSC_INTERN PetscErrorCode MatGetSymbolicTransposeReduced_SeqAIJ(Mat, PetscInt, PetscInt, PetscInt *[], PetscInt *[]);
264 PETSC_INTERN PetscErrorCode MatTransposeSymbolic_SeqAIJ(Mat, Mat *);
265 PETSC_INTERN PetscErrorCode MatTranspose_SeqAIJ(Mat, MatReuse, Mat *);
266 
267 PETSC_INTERN PetscErrorCode MatToSymmetricIJ_SeqAIJ(PetscInt, PetscInt *, PetscInt *, PetscBool, PetscInt, PetscInt, PetscInt **, PetscInt **);
268 PETSC_INTERN PetscErrorCode MatLUFactorSymbolic_SeqAIJ_inplace(Mat, Mat, IS, IS, const MatFactorInfo *);
269 PETSC_INTERN PetscErrorCode MatLUFactorSymbolic_SeqAIJ(Mat, Mat, IS, IS, const MatFactorInfo *);
270 PETSC_INTERN PetscErrorCode MatLUFactorNumeric_SeqAIJ_inplace(Mat, Mat, const MatFactorInfo *);
271 PETSC_INTERN PetscErrorCode MatLUFactorNumeric_SeqAIJ(Mat, Mat, const MatFactorInfo *);
272 PETSC_INTERN PetscErrorCode MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(Mat, Mat, const MatFactorInfo *);
273 PETSC_INTERN PetscErrorCode MatLUFactor_SeqAIJ(Mat, IS, IS, const MatFactorInfo *);
274 PETSC_INTERN PetscErrorCode MatSolve_SeqAIJ_inplace(Mat, Vec, Vec);
275 PETSC_INTERN PetscErrorCode MatSolve_SeqAIJ(Mat, Vec, Vec);
276 PETSC_INTERN PetscErrorCode MatSolve_SeqAIJ_Inode_inplace(Mat, Vec, Vec);
277 PETSC_INTERN PetscErrorCode MatSolve_SeqAIJ_Inode(Mat, Vec, Vec);
278 PETSC_INTERN PetscErrorCode MatSolve_SeqAIJ_NaturalOrdering_inplace(Mat, Vec, Vec);
279 PETSC_INTERN PetscErrorCode MatSolve_SeqAIJ_NaturalOrdering(Mat, Vec, Vec);
280 PETSC_INTERN PetscErrorCode MatSolve_SeqAIJ_InplaceWithPerm(Mat, Vec, Vec);
281 PETSC_INTERN PetscErrorCode MatSolveAdd_SeqAIJ_inplace(Mat, Vec, Vec, Vec);
282 PETSC_INTERN PetscErrorCode MatSolveAdd_SeqAIJ(Mat, Vec, Vec, Vec);
283 PETSC_INTERN PetscErrorCode MatSolveTranspose_SeqAIJ_inplace(Mat, Vec, Vec);
284 PETSC_INTERN PetscErrorCode MatSolveTranspose_SeqAIJ(Mat, Vec, Vec);
285 PETSC_INTERN PetscErrorCode MatSolveTransposeAdd_SeqAIJ_inplace(Mat, Vec, Vec, Vec);
286 PETSC_INTERN PetscErrorCode MatSolveTransposeAdd_SeqAIJ(Mat, Vec, Vec, Vec);
287 PETSC_INTERN PetscErrorCode MatMatSolve_SeqAIJ_inplace(Mat, Mat, Mat);
288 PETSC_INTERN PetscErrorCode MatMatSolve_SeqAIJ(Mat, Mat, Mat);
289 PETSC_INTERN PetscErrorCode MatEqual_SeqAIJ(Mat, Mat, PetscBool *);
290 PETSC_INTERN PetscErrorCode MatFDColoringCreate_SeqXAIJ(Mat, ISColoring, MatFDColoring);
291 PETSC_INTERN PetscErrorCode MatFDColoringSetUp_SeqXAIJ(Mat, ISColoring, MatFDColoring);
292 PETSC_INTERN PetscErrorCode MatFDColoringSetUpBlocked_AIJ_Private(Mat, MatFDColoring, PetscInt);
293 PETSC_INTERN PetscErrorCode MatLoad_AIJ_HDF5(Mat, PetscViewer);
294 PETSC_INTERN PetscErrorCode MatLoad_SeqAIJ_Binary(Mat, PetscViewer);
295 PETSC_INTERN PetscErrorCode MatLoad_SeqAIJ(Mat, PetscViewer);
296 PETSC_INTERN PetscErrorCode RegisterApplyPtAPRoutines_Private(Mat);
297 
298 #if defined(PETSC_HAVE_HYPRE)
299 PETSC_INTERN PetscErrorCode MatProductSetFromOptions_Transpose_AIJ_AIJ(Mat);
300 #endif
301 PETSC_INTERN PetscErrorCode MatProductSetFromOptions_SeqAIJ(Mat);
302 
303 PETSC_INTERN PetscErrorCode MatProductSymbolic_SeqAIJ_SeqAIJ(Mat);
304 PETSC_INTERN PetscErrorCode MatProductSymbolic_PtAP_SeqAIJ_SeqAIJ(Mat);
305 PETSC_INTERN PetscErrorCode MatProductSymbolic_RARt_SeqAIJ_SeqAIJ(Mat);
306 
307 PETSC_INTERN PetscErrorCode MatMatMultSymbolic_SeqAIJ_SeqAIJ(Mat, Mat, PetscReal, Mat);
308 PETSC_INTERN PetscErrorCode MatMatMultSymbolic_SeqAIJ_SeqAIJ_Sorted(Mat, Mat, PetscReal, Mat);
309 PETSC_INTERN PetscErrorCode MatMatMultSymbolic_SeqDense_SeqAIJ(Mat, Mat, PetscReal, Mat);
310 PETSC_INTERN PetscErrorCode MatMatMultSymbolic_SeqAIJ_SeqAIJ_Scalable(Mat, Mat, PetscReal, Mat);
311 PETSC_INTERN PetscErrorCode MatMatMultSymbolic_SeqAIJ_SeqAIJ_Scalable_fast(Mat, Mat, PetscReal, Mat);
312 PETSC_INTERN PetscErrorCode MatMatMultSymbolic_SeqAIJ_SeqAIJ_Heap(Mat, Mat, PetscReal, Mat);
313 PETSC_INTERN PetscErrorCode MatMatMultSymbolic_SeqAIJ_SeqAIJ_BTHeap(Mat, Mat, PetscReal, Mat);
314 PETSC_INTERN PetscErrorCode MatMatMultSymbolic_SeqAIJ_SeqAIJ_RowMerge(Mat, Mat, PetscReal, Mat);
315 PETSC_INTERN PetscErrorCode MatMatMultSymbolic_SeqAIJ_SeqAIJ_LLCondensed(Mat, Mat, PetscReal, Mat);
316 #if defined(PETSC_HAVE_HYPRE)
317 PETSC_INTERN PetscErrorCode MatMatMultSymbolic_AIJ_AIJ_wHYPRE(Mat, Mat, PetscReal, Mat);
318 #endif
319 
320 PETSC_INTERN PetscErrorCode MatMatMultNumeric_SeqAIJ_SeqAIJ(Mat, Mat, Mat);
321 PETSC_INTERN PetscErrorCode MatMatMultNumeric_SeqAIJ_SeqAIJ_Sorted(Mat, Mat, Mat);
322 
323 PETSC_INTERN PetscErrorCode MatMatMultNumeric_SeqDense_SeqAIJ(Mat, Mat, Mat);
324 PETSC_INTERN PetscErrorCode MatMatMultNumeric_SeqAIJ_SeqAIJ_Scalable(Mat, Mat, Mat);
325 
326 PETSC_INTERN PetscErrorCode MatPtAPSymbolic_SeqAIJ_SeqAIJ_SparseAxpy(Mat, Mat, PetscReal, Mat);
327 PETSC_INTERN PetscErrorCode MatPtAPNumeric_SeqAIJ_SeqAIJ(Mat, Mat, Mat);
328 PETSC_INTERN PetscErrorCode MatPtAPNumeric_SeqAIJ_SeqAIJ_SparseAxpy(Mat, Mat, Mat);
329 
330 PETSC_INTERN PetscErrorCode MatRARtSymbolic_SeqAIJ_SeqAIJ(Mat, Mat, PetscReal, Mat);
331 PETSC_INTERN PetscErrorCode MatRARtSymbolic_SeqAIJ_SeqAIJ_matmattransposemult(Mat, Mat, PetscReal, Mat);
332 PETSC_INTERN PetscErrorCode MatRARtSymbolic_SeqAIJ_SeqAIJ_colorrart(Mat, Mat, PetscReal, Mat);
333 PETSC_INTERN PetscErrorCode MatRARtNumeric_SeqAIJ_SeqAIJ(Mat, Mat, Mat);
334 PETSC_INTERN PetscErrorCode MatRARtNumeric_SeqAIJ_SeqAIJ_matmattransposemult(Mat, Mat, Mat);
335 PETSC_INTERN PetscErrorCode MatRARtNumeric_SeqAIJ_SeqAIJ_colorrart(Mat, Mat, Mat);
336 
337 PETSC_INTERN PetscErrorCode MatTransposeMatMultSymbolic_SeqAIJ_SeqAIJ(Mat, Mat, PetscReal, Mat);
338 PETSC_INTERN PetscErrorCode MatTransposeMatMultNumeric_SeqAIJ_SeqAIJ(Mat, Mat, Mat);
339 PETSC_INTERN PetscErrorCode MatDestroy_SeqAIJ_MatTransMatMult(void *);
340 
341 PETSC_INTERN PetscErrorCode MatMatTransposeMultSymbolic_SeqAIJ_SeqAIJ(Mat, Mat, PetscReal, Mat);
342 PETSC_INTERN PetscErrorCode MatMatTransposeMultNumeric_SeqAIJ_SeqAIJ(Mat, Mat, Mat);
343 PETSC_INTERN PetscErrorCode MatTransposeColoringCreate_SeqAIJ(Mat, ISColoring, MatTransposeColoring);
344 PETSC_INTERN PetscErrorCode MatTransColoringApplySpToDen_SeqAIJ(MatTransposeColoring, Mat, Mat);
345 PETSC_INTERN PetscErrorCode MatTransColoringApplyDenToSp_SeqAIJ(MatTransposeColoring, Mat, Mat);
346 
347 PETSC_INTERN PetscErrorCode MatMatMatMultSymbolic_SeqAIJ_SeqAIJ_SeqAIJ(Mat, Mat, Mat, PetscReal, Mat);
348 PETSC_INTERN PetscErrorCode MatMatMatMultNumeric_SeqAIJ_SeqAIJ_SeqAIJ(Mat, Mat, Mat, Mat);
349 
350 PETSC_INTERN PetscErrorCode MatSetRandomSkipColumnRange_SeqAIJ_Private(Mat, PetscInt, PetscInt, PetscRandom);
351 PETSC_INTERN PetscErrorCode MatSetValues_SeqAIJ(Mat, PetscInt, const PetscInt[], PetscInt, const PetscInt[], const PetscScalar[], InsertMode);
352 PETSC_INTERN PetscErrorCode MatGetRow_SeqAIJ(Mat, PetscInt, PetscInt *, PetscInt **, PetscScalar **);
353 PETSC_INTERN PetscErrorCode MatRestoreRow_SeqAIJ(Mat, PetscInt, PetscInt *, PetscInt **, PetscScalar **);
354 PETSC_INTERN PetscErrorCode MatScale_SeqAIJ(Mat, PetscScalar);
355 PETSC_INTERN PetscErrorCode MatDiagonalScale_SeqAIJ(Mat, Vec, Vec);
356 PETSC_INTERN PetscErrorCode MatDiagonalSet_SeqAIJ(Mat, Vec, InsertMode);
357 PETSC_INTERN PetscErrorCode MatAXPY_SeqAIJ(Mat, PetscScalar, Mat, MatStructure);
358 PETSC_INTERN PetscErrorCode MatGetRowIJ_SeqAIJ(Mat, PetscInt, PetscBool, PetscBool, PetscInt *, const PetscInt *[], const PetscInt *[], PetscBool *);
359 PETSC_INTERN PetscErrorCode MatRestoreRowIJ_SeqAIJ(Mat, PetscInt, PetscBool, PetscBool, PetscInt *, const PetscInt *[], const PetscInt *[], PetscBool *);
360 PETSC_INTERN PetscErrorCode MatGetColumnIJ_SeqAIJ(Mat, PetscInt, PetscBool, PetscBool, PetscInt *, const PetscInt *[], const PetscInt *[], PetscBool *);
361 PETSC_INTERN PetscErrorCode MatRestoreColumnIJ_SeqAIJ(Mat, PetscInt, PetscBool, PetscBool, PetscInt *, const PetscInt *[], const PetscInt *[], PetscBool *);
362 PETSC_INTERN PetscErrorCode MatGetColumnIJ_SeqAIJ_Color(Mat, PetscInt, PetscBool, PetscBool, PetscInt *, const PetscInt *[], const PetscInt *[], PetscInt *[], PetscBool *);
363 PETSC_INTERN PetscErrorCode MatRestoreColumnIJ_SeqAIJ_Color(Mat, PetscInt, PetscBool, PetscBool, PetscInt *, const PetscInt *[], const PetscInt *[], PetscInt *[], PetscBool *);
364 PETSC_INTERN PetscErrorCode MatDestroy_SeqAIJ(Mat);
365 PETSC_INTERN PetscErrorCode MatSetUp_SeqAIJ(Mat);
366 PETSC_INTERN PetscErrorCode MatView_SeqAIJ(Mat, PetscViewer);
367 
368 PETSC_INTERN PetscErrorCode MatSeqAIJInvalidateDiagonal(Mat);
369 PETSC_INTERN PetscErrorCode MatSeqAIJInvalidateDiagonal_Inode(Mat);
370 PETSC_INTERN PetscErrorCode MatSeqAIJCheckInode(Mat);
371 PETSC_INTERN PetscErrorCode MatSeqAIJCheckInode_FactorLU(Mat);
372 
373 PETSC_INTERN PetscErrorCode MatAXPYGetPreallocation_SeqAIJ(Mat, Mat, PetscInt *);
374 
375 #if defined(PETSC_HAVE_MATLAB)
376 PETSC_EXTERN PetscErrorCode MatlabEnginePut_SeqAIJ(PetscObject, void *);
377 PETSC_EXTERN PetscErrorCode MatlabEngineGet_SeqAIJ(PetscObject, void *);
378 #endif
379 PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqSBAIJ(Mat, MatType, MatReuse, Mat *);
380 PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqBAIJ(Mat, MatType, MatReuse, Mat *);
381 PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqDense(Mat, MatType, MatReuse, Mat *);
382 PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJCRL(Mat, MatType, MatReuse, Mat *);
383 PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_Elemental(Mat, MatType, MatReuse, Mat *);
384 #if defined(PETSC_HAVE_SCALAPACK)
385 PETSC_INTERN PetscErrorCode MatConvert_AIJ_ScaLAPACK(Mat, MatType, MatReuse, Mat *);
386 #endif
387 PETSC_INTERN PetscErrorCode MatConvert_AIJ_HYPRE(Mat, MatType, MatReuse, Mat *);
388 PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJPERM(Mat, MatType, MatReuse, Mat *);
389 PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJSELL(Mat, MatType, MatReuse, Mat *);
390 PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJMKL(Mat, MatType, MatReuse, Mat *);
391 PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJViennaCL(Mat, MatType, MatReuse, Mat *);
392 PETSC_INTERN PetscErrorCode MatReorderForNonzeroDiagonal_SeqAIJ(Mat, PetscReal, IS, IS);
393 PETSC_INTERN PetscErrorCode MatRARt_SeqAIJ_SeqAIJ(Mat, Mat, MatReuse, PetscReal, Mat *);
394 PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJ(Mat);
395 PETSC_INTERN PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat, MatAssemblyType);
396 PETSC_EXTERN PetscErrorCode MatZeroEntries_SeqAIJ(Mat);
397 
398 PETSC_INTERN PetscErrorCode MatAXPYGetPreallocation_SeqX_private(PetscInt, const PetscInt *, const PetscInt *, const PetscInt *, const PetscInt *, PetscInt *);
399 PETSC_INTERN PetscErrorCode MatCreateMPIMatConcatenateSeqMat_SeqAIJ(MPI_Comm, Mat, PetscInt, MatReuse, Mat *);
400 PETSC_INTERN PetscErrorCode MatCreateMPIMatConcatenateSeqMat_MPIAIJ(MPI_Comm, Mat, PetscInt, MatReuse, Mat *);
401 
402 PETSC_INTERN PetscErrorCode MatSetSeqMat_SeqAIJ(Mat, IS, IS, MatStructure, Mat);
403 PETSC_INTERN PetscErrorCode MatEliminateZeros_SeqAIJ(Mat A);
404 PETSC_INTERN PetscErrorCode MatDestroySubMatrix_Private(Mat_SubSppt *);
405 PETSC_INTERN PetscErrorCode MatDestroySubMatrix_SeqAIJ(Mat);
406 PETSC_INTERN PetscErrorCode MatDestroySubMatrix_Dummy(Mat);
407 PETSC_INTERN PetscErrorCode MatDestroySubMatrices_Dummy(PetscInt, Mat *[]);
408 PETSC_INTERN PetscErrorCode MatCreateSubMatrix_SeqAIJ(Mat, IS, IS, PetscInt, MatReuse, Mat *);
409 
410 PETSC_INTERN PetscErrorCode MatSeqAIJCompactOutExtraColumns_SeqAIJ(Mat, ISLocalToGlobalMapping *);
411 PETSC_INTERN PetscErrorCode MatSetSeqAIJWithArrays_private(MPI_Comm, PetscInt, PetscInt, PetscInt[], PetscInt[], PetscScalar[], MatType, Mat);
412 
413 /*
414     PetscSparseDenseMinusDot - The inner kernel of triangular solves and Gauss-Siedel smoothing. \sum_i xv[i] * r[xi[i]] for CSR storage
415 
416   Input Parameters:
417 +  nnz - the number of entries
418 .  r - the array of vector values
419 .  xv - the matrix values for the row
420 -  xi - the column indices of the nonzeros in the row
421 
422   Output Parameter:
423 .  sum - negative the sum of results
424 
425   PETSc compile flags:
426 +   PETSC_KERNEL_USE_UNROLL_4
427 -   PETSC_KERNEL_USE_UNROLL_2
428 
429   Developer Note:
430     The macro changes sum but not other parameters
431 
432 .seealso: `PetscSparseDensePlusDot()`
433 */
434 #if defined(PETSC_KERNEL_USE_UNROLL_4)
435   #define PetscSparseDenseMinusDot(sum, r, xv, xi, nnz) \
436     { \
437       if (nnz > 0) { \
438         PetscInt nnz2 = nnz, rem = nnz & 0x3; \
439         switch (rem) { \
440         case 3: \
441           sum -= *xv++ * r[*xi++]; \
442         case 2: \
443           sum -= *xv++ * r[*xi++]; \
444         case 1: \
445           sum -= *xv++ * r[*xi++]; \
446           nnz2 -= rem; \
447         } \
448         while (nnz2 > 0) { \
449           sum -= xv[0] * r[xi[0]] + xv[1] * r[xi[1]] + xv[2] * r[xi[2]] + xv[3] * r[xi[3]]; \
450           xv += 4; \
451           xi += 4; \
452           nnz2 -= 4; \
453         } \
454         xv -= nnz; \
455         xi -= nnz; \
456       } \
457     }
458 
459 #elif defined(PETSC_KERNEL_USE_UNROLL_2)
460   #define PetscSparseDenseMinusDot(sum, r, xv, xi, nnz) \
461     { \
462       PetscInt __i, __i1, __i2; \
463       for (__i = 0; __i < nnz - 1; __i += 2) { \
464         __i1 = xi[__i]; \
465         __i2 = xi[__i + 1]; \
466         sum -= (xv[__i] * r[__i1] + xv[__i + 1] * r[__i2]); \
467       } \
468       if (nnz & 0x1) sum -= xv[__i] * r[xi[__i]]; \
469     }
470 
471 #else
472   #define PetscSparseDenseMinusDot(sum, r, xv, xi, nnz) \
473     { \
474       PetscInt __i; \
475       for (__i = 0; __i < nnz; __i++) sum -= xv[__i] * r[xi[__i]]; \
476     }
477 #endif
478 
479 /*
480     PetscSparseDensePlusDot - The inner kernel of matrix-vector product \sum_i xv[i] * r[xi[i]] for CSR storage
481 
482   Input Parameters:
483 +  nnz - the number of entries
484 .  r - the array of vector values
485 .  xv - the matrix values for the row
486 -  xi - the column indices of the nonzeros in the row
487 
488   Output Parameter:
489 .  sum - the sum of results
490 
491   PETSc compile flags:
492 +   PETSC_KERNEL_USE_UNROLL_4
493 -   PETSC_KERNEL_USE_UNROLL_2
494 
495   Developer Note:
496     The macro changes sum but not other parameters
497 
498 .seealso: `PetscSparseDenseMinusDot()`
499 */
500 #if defined(PETSC_KERNEL_USE_UNROLL_4)
501   #define PetscSparseDensePlusDot(sum, r, xv, xi, nnz) \
502     { \
503       if (nnz > 0) { \
504         PetscInt nnz2 = nnz, rem = nnz & 0x3; \
505         switch (rem) { \
506         case 3: \
507           sum += *xv++ * r[*xi++]; \
508         case 2: \
509           sum += *xv++ * r[*xi++]; \
510         case 1: \
511           sum += *xv++ * r[*xi++]; \
512           nnz2 -= rem; \
513         } \
514         while (nnz2 > 0) { \
515           sum += xv[0] * r[xi[0]] + xv[1] * r[xi[1]] + xv[2] * r[xi[2]] + xv[3] * r[xi[3]]; \
516           xv += 4; \
517           xi += 4; \
518           nnz2 -= 4; \
519         } \
520         xv -= nnz; \
521         xi -= nnz; \
522       } \
523     }
524 
525 #elif defined(PETSC_KERNEL_USE_UNROLL_2)
526   #define PetscSparseDensePlusDot(sum, r, xv, xi, nnz) \
527     { \
528       PetscInt __i, __i1, __i2; \
529       for (__i = 0; __i < nnz - 1; __i += 2) { \
530         __i1 = xi[__i]; \
531         __i2 = xi[__i + 1]; \
532         sum += (xv[__i] * r[__i1] + xv[__i + 1] * r[__i2]); \
533       } \
534       if (nnz & 0x1) sum += xv[__i] * r[xi[__i]]; \
535     }
536 
537 #elif defined(PETSC_USE_AVX512_KERNELS) && defined(PETSC_HAVE_IMMINTRIN_H) && defined(__AVX512F__) && defined(PETSC_USE_REAL_DOUBLE) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_64BIT_INDICES) && !defined(PETSC_SKIP_IMMINTRIN_H_CUDAWORKAROUND)
538   #define PetscSparseDensePlusDot(sum, r, xv, xi, nnz) PetscSparseDensePlusDot_AVX512_Private(&(sum), (r), (xv), (xi), (nnz))
539 
540 #else
541   #define PetscSparseDensePlusDot(sum, r, xv, xi, nnz) \
542     { \
543       PetscInt __i; \
544       for (__i = 0; __i < nnz; __i++) sum += xv[__i] * r[xi[__i]]; \
545     }
546 #endif
547 
548 #if defined(PETSC_USE_AVX512_KERNELS) && defined(PETSC_HAVE_IMMINTRIN_H) && defined(__AVX512F__) && defined(PETSC_USE_REAL_DOUBLE) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_64BIT_INDICES) && !defined(PETSC_SKIP_IMMINTRIN_H_CUDAWORKAROUND)
549   #include <immintrin.h>
550   #if !defined(_MM_SCALE_8)
551     #define _MM_SCALE_8 8
552   #endif
553 
554 static inline void PetscSparseDensePlusDot_AVX512_Private(PetscScalar *sum, const PetscScalar *x, const MatScalar *aa, const PetscInt *aj, PetscInt n)
555 {
556   __m512d  vec_x, vec_y, vec_vals;
557   __m256i  vec_idx;
558   __mmask8 mask;
559   PetscInt j;
560 
561   vec_y = _mm512_setzero_pd();
562   for (j = 0; j < (n >> 3); j++) {
563     vec_idx  = _mm256_loadu_si256((__m256i const *)aj);
564     vec_vals = _mm512_loadu_pd(aa);
565     vec_x    = _mm512_i32gather_pd(vec_idx, x, _MM_SCALE_8);
566     vec_y    = _mm512_fmadd_pd(vec_x, vec_vals, vec_y);
567     aj += 8;
568     aa += 8;
569   }
570   /* masked load does not work on KNL, it requires avx512vl */
571   if ((n & 0x07) > 2) {
572     mask     = (__mmask8)(0xff >> (8 - (n & 0x07)));
573     vec_idx  = _mm256_loadu_si256((__m256i const *)aj);
574     vec_vals = _mm512_loadu_pd(aa);
575     vec_x    = _mm512_mask_i32gather_pd(vec_x, mask, vec_idx, x, _MM_SCALE_8);
576     vec_y    = _mm512_mask3_fmadd_pd(vec_x, vec_vals, vec_y, mask);
577   } else if ((n & 0x07) == 2) {
578     *sum += aa[0] * x[aj[0]];
579     *sum += aa[1] * x[aj[1]];
580   } else if ((n & 0x07) == 1) {
581     *sum += aa[0] * x[aj[0]];
582   }
583   if (n > 2) *sum += _mm512_reduce_add_pd(vec_y);
584   /*
585   for (j=0;j<(n&0x07);j++) *sum += aa[j]*x[aj[j]];
586 */
587 }
588 #endif
589 
590 /*
591     PetscSparseDenseMaxDot - The inner kernel of a modified matrix-vector product \max_i xv[i] * r[xi[i]] for CSR storage
592 
593   Input Parameters:
594 +  nnz - the number of entries
595 .  r - the array of vector values
596 .  xv - the matrix values for the row
597 -  xi - the column indices of the nonzeros in the row
598 
599   Output Parameter:
600 .  max - the max of results
601 
602 .seealso: `PetscSparseDensePlusDot()`, `PetscSparseDenseMinusDot()`
603 */
604 #define PetscSparseDenseMaxDot(max, r, xv, xi, nnz) \
605   do { \
606     for (PetscInt __i = 0; __i < (nnz); __i++) { max = PetscMax(PetscRealPart(max), PetscRealPart((xv)[__i] * (r)[(xi)[__i]])); } \
607   } while (0)
608 
609 /*
610  Add column indices into table for counting the max nonzeros of merged rows
611  */
612 #define MatRowMergeMax_SeqAIJ(mat, nrows, ta) \
613   do { \
614     if ((mat)) { \
615       for (PetscInt _row = 0; _row < (nrows); _row++) { \
616         const PetscInt _nz = (mat)->i[_row + 1] - (mat)->i[_row]; \
617         for (PetscInt _j = 0; _j < _nz; _j++) { \
618           PetscInt *_col = _j + (mat)->j + (mat)->i[_row]; \
619           PetscCall(PetscHMapISet((ta), *_col + 1, 1)); \
620         } \
621       } \
622     } \
623   } while (0)
624 
625 /*
626  Add column indices into table for counting the nonzeros of merged rows
627  */
628 #define MatMergeRows_SeqAIJ(mat, nrows, rows, ta) \
629   do { \
630     for (PetscInt _i = 0; _i < (nrows); _i++) { \
631       const PetscInt _row = (rows)[_i]; \
632       const PetscInt _nz  = (mat)->i[_row + 1] - (mat)->i[_row]; \
633       for (PetscInt _j = 0; _j < _nz; _j++) { \
634         PetscInt *_col = _j + (mat)->j + (mat)->i[_row]; \
635         PetscCall(PetscHMapISetWithMode((ta), *_col + 1, 1, INSERT_VALUES)); \
636       } \
637     } \
638   } while (0)
639 
640 #endif
641