8.3
general documentation
cs_matrix_priv.h
Go to the documentation of this file.
1#ifndef __CS_MATRIX_PRIV_H__
2#define __CS_MATRIX_PRIV_H__
3
4/*============================================================================
5 * Private types for sparse matrix representation and operations.
6 *============================================================================*/
7
8/*
9 This file is part of code_saturne, a general-purpose CFD tool.
10
11 Copyright (C) 1998-2024 EDF S.A.
12
13 This program is free software; you can redistribute it and/or modify it under
14 the terms of the GNU General Public License as published by the Free Software
15 Foundation; either version 2 of the License, or (at your option) any later
16 version.
17
18 This program is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
21 details.
22
23 You should have received a copy of the GNU General Public License along with
24 this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
25 Street, Fifth Floor, Boston, MA 02110-1301, USA.
26*/
27
28/*----------------------------------------------------------------------------*/
29
30/*----------------------------------------------------------------------------
31 * Local headers
32 *----------------------------------------------------------------------------*/
33
34#include "cs_defs.h"
35
36#include "cs_matrix.h"
37
38/*----------------------------------------------------------------------------*/
39
41
44/*============================================================================
45 * Macro definitions
46 *============================================================================*/
47
48/*============================================================================
49 * Type definitions
50 *============================================================================*/
51
52/* Formats currently supported:
53 *
54 * - Native
55 * - Compressed Sparse Row (CSR)
56 * - Modified Compressed Sparse Row (MSR), with separate diagonal
57 */
58
59/*----------------------------------------------------------------------------
60 * Function pointer types
61 *----------------------------------------------------------------------------*/
62
63typedef void
64(cs_matrix_set_coeffs_t) (cs_matrix_t *matrix,
65 bool symmetric,
66 bool copy,
67 cs_lnum_t n_edges,
68 const cs_lnum_2_t *edges,
69 const cs_real_t *da,
70 const cs_real_t *xa);
71
72typedef void
73(cs_matrix_release_coeffs_t) (cs_matrix_t *matrix);
74
75typedef void
76(cs_matrix_destroy_struct_t) (void **ms);
77
78typedef void
79(cs_matrix_destroy_coeffs_t) (cs_matrix_t *matrix);
80
81typedef void
82(cs_matrix_destroy_adaptor_t) (cs_matrix_t *matrix);
83
84typedef void
85(cs_matrix_copy_diagonal_t) (const cs_matrix_t *matrix,
86 cs_real_t *da);
87
88typedef const cs_real_t *
89(cs_matrix_get_diagonal_t)(const cs_matrix_t *matrix);
90
92(cs_matrix_assembler_values_create_t) (cs_matrix_t *matrix,
93 cs_lnum_t diag_block_size,
94 cs_lnum_t extra_diag_block_size);
95
96/*----------------------------------------------------------------------------
97 * Function pointer for matrix-veector product (y = A.x).
98 *
99 * parameters:
100 * matrix <-> pointer to matrix structure
101 * exclude_diag <-- if true, compute (A-D).x instead of A.x
102 * sync <-- if true, synchronize ghost values
103 * x <-- x input vector (may be synchronized by this function)
104 * y <-- y output vector
105 *----------------------------------------------------------------------------*/
106
107typedef void
108(cs_matrix_vector_product_t) (cs_matrix_t *matrix,
109 bool exclude_diag,
110 bool sync,
111 cs_real_t *x,
112 cs_real_t *y);
113
114/*----------------------------------------------------------------------------
115 * Matrix types
116 *----------------------------------------------------------------------------*/
117
118/* Native matrix structure representation */
119/*----------------------------------------*/
120
121/* Note: the members of this structure are already available through the top
122 * matrix structure, but are replicated here in case of future removal
123 * from the top structure (which would require computation/assignment of
124 * matrix coefficients in another form) */
125
126typedef struct _cs_matrix_struct_native_t {
127
128 cs_lnum_t n_rows; /* Local number of rows */
129 cs_lnum_t n_cols_ext; /* Local number of columns + ghosts */
130 cs_lnum_t n_edges; /* Local number of graph edges
131 (for extra-diagonal terms) */
132
133 /* Pointers to shared arrays */
134
135 const cs_lnum_2_t *edges; /* Edges (symmetric row <-> column)
136 connectivity */
137
138} cs_matrix_struct_native_t;
139
140/* CSR (Compressed Sparse Row) matrix structure representation */
141/*-------------------------------------------------------------*/
142
143typedef struct _cs_matrix_struct_csr_t {
144
145 cs_lnum_t n_rows; /* Local number of rows */
146 cs_lnum_t n_cols_ext; /* Local number of columns + ghosts */
147
148 /* Pointers to structure arrays and info (row_index, col_id) */
149
150 bool have_diag; /* Has non-zero diagonal */
151 bool direct_assembly; /* True if each value corresponds to
152 a unique face; false if multiple
153 faces contribute to the same
154 value (i.e. we have split faces) */
155
156 const cs_lnum_t *row_index; /* Pointer to row index (0 to n-1) */
157 const cs_lnum_t *col_id; /* Pointer to column id (0 to n-1) */
158
159 cs_lnum_t *_row_index; /* Row index (0 to n-1), if owner */
160 cs_lnum_t *_col_id; /* Column id (0 to n-1), if owner */
161
162} cs_matrix_struct_csr_t;
163
164/* Distributed matrix structure representation */
165/*---------------------------------------------*/
166
167/* This storage assumes a representation in the following form:
168
169 - D+E+H
170
171 Where D is the diagonal, E the local extra-diagonal, and H is the part
172 of the matrix referencing halo values (only the upper-part of the
173 coefficients are needed on a given rank).
174
175 In cases where a partial block structure is used for the diagonal
176 (usually at boundary cells), the diagonal values may be indexed.
177
178 Sub-matrices are stored in CSR format, to allow easy indexing
179 or access to a given row's elements (which is useful for multigrid
180 coarsening).
181
182 Since the H portion of the matrix may be very sparse, with most rows
183 empty, the matching row ids can be stored in addition to the column ids,
184 defining a simple coordinate-type structure as a COO-type SpMv product
185 can be expected to be cheaper than a CSR-based one in this case.
186 */
187
188typedef struct _cs_matrix_struct_dist_t {
189
190 cs_lnum_t n_rows; /* Local number of rows */
191 cs_lnum_t n_cols_ext; /* Local number of columns + ghosts */
192
193 /* Pointers to structure arrays and info */
194
195 cs_matrix_struct_csr_t e; /* E (local off-diagonal) CSR structure */
196 cs_matrix_struct_csr_t h; /* H (halo) CSR structure */
197
198 cs_lnum_t *h_row_id; /* Optional row id for coordinates
199 format (col_id in h structure) */
200
201} cs_matrix_struct_dist_t;
202
203/* Commmon matrix coefficients representation */
204/*--------------------------------------------*/
205
206/* Used for matrix formats except third-party */
207
208typedef struct _cs_matrix_coeff_t {
209
210 bool symmetric; /* Symmetry indicator */
211
212 int db_size; /* Diagonal block size */
213 int eb_size; /* Extra-diagonal block size */
214
215 /* Pointers to possibly shared arrays.
216 For distributed matrices, where the h_val (halo-only) coefficient array
217 is used, the e_val (extra-diagonal) array should contain only local
218 values. For other matrix types, it can contain a mix of local and
219 distant values. */
220
221 const cs_real_t *val; /* All coefficients (CSR) */
222 const cs_real_t *d_val; /* D (diagonal-only) coefficients */
223 const cs_real_t *e_val; /* E (extra-diagonal) coefficients */
224 const cs_real_t *h_val; /* H (halo-only) coefficients */
225
226 /* Pointers to private arrays.
227 nullptr if shared:
228 * If non-null, d_val, e_val, and h_val should point
229 to matching private array.
230 * In the case of CSR storage, where diagonal values can be stored in
231 the a_val array, d_val will be null but _d_val may be used to store
232 (cache) diagonal values, if queried. */
233
234 cs_real_t *_val; /* All coefficients (CSR) */
235 cs_real_t *_d_val; /* D (diagonal) coefficients */
236 cs_real_t *_e_val; /* E (extra-diagonal) coefficients */
237 cs_real_t *_h_val; /* H (halo-only) coefficients */
238
239 /* Pointers to auxiliary matrix structure elements */
240
241 cs_lnum_t *d_idx; /* Index for diagonal matrix coefficients
242 in case of multiple block sizes */
243
244} cs_matrix_coeff_t;
245
246/* Matrix structure (representation-independent part) */
247/*----------------------------------------------------*/
248
249struct _cs_matrix_structure_t {
250
251 cs_matrix_type_t type; /* Matrix storage and definition type */
252
253 cs_lnum_t n_rows; /* Local number of rows */
254 cs_lnum_t n_cols_ext; /* Local number of columns + ghosts */
255
256 cs_alloc_mode_t alloc_mode; /* Preferred allocation mode */
257
258 void *structure; /* Matrix structure */
259
260 /* Pointers to shared arrays from mesh structure
261 (face->cell connectivity for coefficient assignment,
262 local->local cell numbering for future info or renumbering,
263 and halo) */
264
265 const cs_halo_t *halo; /* Parallel or periodic halo */
266 const cs_numbering_t *numbering; /* Vectorization or thread-related
267 numbering information */
268
269 const cs_matrix_assembler_t *assembler; /* Associated matrix assembler */
270};
271
272/* Structure associated with Matrix (representation-independent part) */
273/*--------------------------------------------------------------------*/
274
275struct _cs_matrix_t {
276
277 cs_matrix_type_t type; /* Matrix storage and definition type */
278
279 const char *type_name; /* Pointer to matrix type name string */
280 const char *type_fname; /* Pointer to matrix type
281 full name string */
282
283 cs_lnum_t n_rows; /* Local number of rows */
284 cs_lnum_t n_cols_ext; /* Local number of columns + ghosts */
285
286 cs_matrix_fill_type_t fill_type; /* Matrix fill type */
287
288 bool symmetric; /* true if coefficients are symmetric */
289
290 cs_lnum_t db_size; /* Diagonal block size */
291
292 cs_lnum_t eb_size; /* Extradiag block size */
293
294 cs_alloc_mode_t alloc_mode; /* Preferred allocation mode */
295
296 unsigned int need_xa : 1; /* Need face-based xa array */
297
298 /* Pointer to shared structure */
299
300 const void *structure; /* Possibly shared matrix structure */
301 void *_structure; /* Private matrix structure */
302
303 /* Pointers to arrays possibly shared from mesh structure
304 (graph edges: face->cell connectivity for coefficient assignment,
305 rows: local->local cell numbering for future info or renumbering,
306 and halo) */
307
308 const cs_halo_t *halo; /* Parallel or periodic halo */
309 const cs_numbering_t *numbering; /* Vectorization or thread-related
310 numbering information */
311
312 const cs_matrix_assembler_t *assembler; /* Associated matrix assembler */
313
314 /* Pointer to shared arrays from coefficient assignment from
315 "native" type. This should be removed in the future, but requires
316 removing the dependency to the native structure in the multigrid
317 code first. */
318
319 const cs_real_t *xa; /* Extra-diagonal terms */
320 cs_real_t *_xa; /* Extra-diagonal terms, private copy */
321
322 /* Pointer to associated connectivity and geometric data needed by
323 multigrid smoothing. At least cell centers and volumes are needed for
324 relaxation, and face normals are needed for the "classical" option.
325 Cells and faces here do not need to be primary mesh elements,
326 but could be dual mesh elements of some sort */
327
328 const cs_lnum_t *c2f_idx; /* Cell to faces index (shared) */
329 const cs_lnum_t *c2f; /* Cell to faces adjacency (shared) */
330 const short int *c2f_sgn; /* Cell to faces orientation (shared) */
331
332 const cs_real_3_t *cell_cen; /* Cell center (shared) */
333 const cs_real_t *cell_vol; /* Cell volume (shared) */
334 const cs_real_3_t *face_normal; /* Face normal (shared) */
335
336 /* Pointer to private data */
337
338 void *coeffs; /* Matrix coefficients */
339
340 void *ext_lib_map; /* Mapping of structure and
341 coefficients to external
342 library, if needed */
343
344 /* Function pointers */
345
346 cs_matrix_set_coeffs_t *set_coefficients;
347 cs_matrix_release_coeffs_t *release_coefficients;
348 cs_matrix_copy_diagonal_t *copy_diagonal;
349 cs_matrix_get_diagonal_t *get_diagonal;
350
351 cs_matrix_destroy_struct_t *destroy_structure;
352 cs_matrix_destroy_coeffs_t *destroy_coefficients;
353 cs_matrix_destroy_adaptor_t *destroy_adaptor;
354
355 cs_matrix_assembler_values_create_t *assembler_values_create;
356
357 /* Function pointer arrays, with CS_MATRIX_N_FILL_TYPES variants:
358 fill_type*4 + full, extra-diagonal, lower, upper parts */
359
360 cs_matrix_vector_product_t *vector_multiply[CS_MATRIX_N_FILL_TYPES]
362
363#if defined(HAVE_ACCEL)
364
365 /* Function pointer arrays, with CS_MATRIX_N_FILL_TYPES variants, for host
366 or device only: fill_type*4 + full, extra-diagonal, lower, upper parts */
367
368 cs_matrix_vector_product_t *vector_multiply_h[CS_MATRIX_N_FILL_TYPES]
370 cs_matrix_vector_product_t *vector_multiply_d[CS_MATRIX_N_FILL_TYPES]
372
373#endif /* defined(HAVE_ACCEL) */
374
375};
376
377/* Structure used for tuning variants */
378/*------------------------------------*/
379
380struct _cs_matrix_variant_t {
381
382 char name[2][32]; /* Variant names */
383
384 cs_matrix_type_t type; /* Matrix storage and definition type */
385 cs_matrix_fill_type_t fill_type; /* Matrix storage and definition type */
386
387 /* Function pointer arrays */
388
389 cs_matrix_vector_product_t *vector_multiply[CS_MATRIX_SPMV_N_TYPES];
390
391 /* Associated vector host/device locations (h/d/g for host/device/both) */
392
393 char vector_multiply_xy_hd[CS_MATRIX_SPMV_N_TYPES];
394};
395
398/*=============================================================================
399 * Semi-private function prototypes
400 *============================================================================*/
401
402/*----------------------------------------------------------------------------*/
403
405
406#endif /* __CS_MATRIX_PRIV_H__ */
#define BEGIN_C_DECLS
Definition: cs_defs.h:542
double cs_real_t
Floating-point value.
Definition: cs_defs.h:342
cs_lnum_t cs_lnum_2_t[2]
vector of 2 local mesh-entity ids
Definition: cs_defs.h:352
cs_real_t cs_real_3_t[3]
vector of 3 floating-point values
Definition: cs_defs.h:359
#define END_C_DECLS
Definition: cs_defs.h:543
int cs_lnum_t
local mesh entity id
Definition: cs_defs.h:335
@ h
Definition: cs_field_pointer.h:93
@ CS_MATRIX_SPMV_N_TYPES
Definition: cs_matrix.h:100
struct _cs_matrix_t cs_matrix_t
Definition: cs_matrix.h:110
cs_matrix_fill_type_t
Definition: cs_matrix.h:72
@ CS_MATRIX_N_FILL_TYPES
Definition: cs_matrix.h:85
cs_matrix_type_t
Definition: cs_matrix.h:54
struct _cs_matrix_assembler_t cs_matrix_assembler_t
Definition: cs_matrix_assembler.h:61
struct _cs_matrix_assembler_values_t cs_matrix_assembler_values_t
Definition: cs_matrix_assembler.h:65
cs_alloc_mode_t
Definition: cs_mem.h:50
Definition: cs_halo.h:77
Definition: cs_numbering.h:87