9.2
general documentation
cs_sles_pc.h
Go to the documentation of this file.
1#ifndef CS_SLES_PC_H
2#define CS_SLES_PC_H
3
4/*============================================================================
5 * Sparse Linear Equation Solver Preconditioner driver
6 *============================================================================*/
7
8/*
9 This file is part of code_saturne, a general-purpose CFD tool.
10
11 Copyright (C) 1998-2026 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 "base/cs_base.h"
35#include "base/cs_log.h"
36#include "base/cs_halo_perio.h"
37#include "alge/cs_matrix.h"
38
39/*============================================================================
40 * Macro definitions
41 *============================================================================*/
42
43/*============================================================================
44 * Type definitions
45 *============================================================================*/
46
47/*----------------------------------------------------------------------------
48 * Convergence status
49 *----------------------------------------------------------------------------*/
50
52typedef enum {
53
62
63/* General linear solver context (opaque) */
64
65typedef struct _cs_sles_pc_t cs_sles_pc_t;
66
67/*----------------------------------------------------------------------------
68 * Function pointer returning the type name of a preconditioner context.
69 *
70 * The context structure depends on the type of preconditioner used,
71 * which may in turn be determined by the string returned by
72 * cs_sles_pc_get_type() and cs_sles_pc_get_type_name().
73 * If may be used by appropriate functions specific to that type.
74 *
75 * parameters:
76 * context <-- pointer to preconditioner-specific context
77 * logging <-- if true, a name appropritate to logging
78 * (possibly translated) is returned; if false,
79 * a canonical name is returned.
80 *----------------------------------------------------------------------------*/
81
82typedef const char *
83(cs_sles_pc_get_type_t) (const void *context,
84 bool logging);
85
86/*----------------------------------------------------------------------------
87 * Function pointer for pre-resolution setup of a preconditioner context.
88 *
89 * This setup may include building a multigrid hierarchy, for example.
90 *
91 * parameters:
92 * context <-> pointer to preconditioner context
93 * name <-- pointer to name of associated linear system
94 * a <-- matrix
95 * accel <-- use accelerator version ?
96 * verbosity <-- associated verbosity
97 *----------------------------------------------------------------------------*/
98
99typedef void
100(cs_sles_pc_setup_t) (void *context,
101 const char *name,
102 const cs_matrix_t *a,
103 bool accel,
104 int verbosity);
105
106/*----------------------------------------------------------------------------
107 * Function pointer for setting of the required tolerance for preconditioners
108 * involving an iterative solver.
109 *
110 * This will usually not be relevant to non-iterative preconditioners,
111 * for which this type of function does not need to be defined.
112 *
113 * The preconditioner is considered to have converged when
114 * residual/r_norm <= precision, residual being the L2 norm of a.vx-rhs.
115 *
116 * parameters:
117 * context <-> pointer to preconditioner context
118 * precision <-- preconditioner precision
119 * r_norm <-- residual normalization
120 *----------------------------------------------------------------------------*/
121
122typedef void
123(cs_sles_pc_tolerance_t) (void *context,
124 double precision,
125 double r_norm);
126
127/*----------------------------------------------------------------------------
128 * Function pointer for application of a preconditioner.
129 *
130 * In cases where it is desired that the preconditioner modify a vector
131 * "in place", x_in should be set to NULL, and x_out contain the vector to
132 * be modified (\f$x_{out} \leftarrow M^{-1}x_{out})\f$).
133 *
134 * parameters:
135 * context <-> pointer to preconditioner context
136 * x_in <-- input vector
137 * x_out <-> input/output vector
138 *
139 * returns:
140 * preconditioner application status
141 *----------------------------------------------------------------------------*/
142
144(cs_sles_pc_apply_t) (void *context,
145 const cs_real_t *x_in,
146 cs_real_t *x_out);
147
148/*----------------------------------------------------------------------------
149 * Function pointer for freeing of a preconditioner's context data.
150 *
151 * Note that this function should free resolution-related data, such as
152 * multigrid hierarchy and any other temporary arrays or
153 * objects required for application, but should not free the whole context,
154 * as info used for logging (especially performance data) should be
155 * maintained.
156 *
157 * parameters:
158 * context <-> pointer to preconditioner context
159 *----------------------------------------------------------------------------*/
160
161typedef void
162(cs_sles_pc_free_t) (void *context);
163
164/*----------------------------------------------------------------------------
165 * Function pointer for logging of linear preconditioner setup,
166 * history and performance data.
167 *
168 * This function will indirectly be called for each preconditioner when
169 * cs_sles_finalize() is called.
170 *
171 * parameters:
172 * context <-- pointer to preconditioner context
173 * log_type <-- log type
174 *----------------------------------------------------------------------------*/
175
176typedef void
177(cs_sles_pc_log_t) (const void *context,
178 cs_log_t log_type);
179
180/*----------------------------------------------------------------------------
181 * Function pointer for creation of a preconditioner context based on the
182 * copy of another.
183 *
184 * The new context copies the settings of the copied context, but not
185 * its setup data and logged info, such as performance data.
186 *
187 * This type of function is optional, but enables associating different
188 * preconditioners to related systems (to differentiate logging) while using
189 * the same settings by default.
190 *
191 * parameters:
192 * context <-- context to clone
193 *
194 * returns:
195 * pointer to newly created context
196 *----------------------------------------------------------------------------*/
197
198typedef void *
199(cs_sles_pc_clone_t) (const void *context);
200
201/*----------------------------------------------------------------------------
202 * Function pointer for destruction of a preconditioner context.
203 *
204 * This function should free all context data.
205 *
206 * parameters:
207 * context <-> pointer to preconditioner context
208 *----------------------------------------------------------------------------*/
209
210typedef void
211(cs_sles_pc_destroy_t) (void **context);
212
213/*============================================================================
214 * Global variables
215 *============================================================================*/
216
217/*=============================================================================
218 * Public function prototypes for Fortran API
219 *============================================================================*/
220
221/*=============================================================================
222 * Public function prototypes
223 *============================================================================*/
224
225/*----------------------------------------------------------------------------*/
226/*
227 * \brief Log preconditioner setup, history and performance data.
228 *
229 * This function frees resolution-related data, such as multigrid hierarchy,
230 * preconditioning, and any other temporary arrays or objects required for
231 * resolution, but maintains context information such as that used for
232 * logging (especially performance data).
233 *
234 * \param[in, out] pc pointer to preconditioner object
235 * \param[in] log_type log type
236 */
237/*----------------------------------------------------------------------------*/
238
239extern "C" void
241 cs_log_t log_type);
242
243/*----------------------------------------------------------------------------*/
244/*
245 * \brief Define sparse linear equation preconditioner.
246 *
247 * The context pointer is used to point to a structure adapted to the function
248 * pointers given here, and combined with those functions, allows using
249 * both built-in, external, or user-defined preconditioners.
250 *
251 * \param[in, out] context pointer to preconditioner context structure
252 * (cs_sles_pc subsystem becomes owner)
253 * \param[in] get_type_func pointer to function returning type name
254 * \param[in] setup_func pointer to preconditioner setup function
255 * \param[in] tolerance_func pointer to tolerance setting functio
256 * \param[in] apply_func pointer to preconditioner application
257 * function (also calls setup_func if not done
258 * yet or free_func called since last apply)
259 * \param[in] free_func pointer function freeing system setup
260 * \param[in] log_func pointer to system info logging function
261 (optional, but recommended)
262 * \param[in] clone_func pointer to settings clone function
263 * \param[in] destroy_func pointer to function destroying
264 * preconditioner context
265 *
266 * \return pointer to associated preconditioner object
267 */
268/*----------------------------------------------------------------------------*/
269
270extern "C" cs_sles_pc_t *
271cs_sles_pc_define(void *context,
272 cs_sles_pc_get_type_t *get_type_func,
273 cs_sles_pc_setup_t *setup_func,
274 cs_sles_pc_tolerance_t *tolerance_func,
275 cs_sles_pc_apply_t *apply_func,
276 cs_sles_pc_free_t *free_func,
277 cs_sles_pc_log_t *log_func,
278 cs_sles_pc_clone_t *clone_func,
279 cs_sles_pc_destroy_t *destroy_func);
280
281/*----------------------------------------------------------------------------*/
282/*
283 * \brief Destroy a sparse linear equation preconditioner.
284 *
285 * \param[in, out] pc pointer to preconditioner context structure
286 */
287/*----------------------------------------------------------------------------*/
288
289extern "C" void
291
292/*----------------------------------------------------------------------------*/
293/*
294 * \brief Create a new preconditioner context based on the copy of another.
295 *
296 * The intended use of this function is to allow associating different
297 * preconditioners to related systems, so as to allow simultaneous setups
298 * and differentiate logging, while using the same settings by default.
299 *
300 * If no preconditioner (i.e. NULL) is passed, it will return NULL.
301 *
302 * \param[in] src pointer to source preconditioner object
303 *
304 * \return pointer to new preconditioner object, or NULL
305 */
306/*----------------------------------------------------------------------------*/
307
308extern "C" cs_sles_pc_t *
310
311/*----------------------------------------------------------------------------*/
312/*
313 * \brief Return type name of preconditioner context.
314 *
315 * The returned string is intended to help determine which type is associated
316 * with the void * pointer returned by \ref cs_sles_pc_get_context for a given
317 * preconditioner definition, so as to be able to call additional specific
318 * functions beyond the generic functions assigned to a cs_sles_pc_t object.
319 *
320 * \param[in] pc pointer to preconditioner object
321 *
322 * \return pointer to linear system preconditioner specific type name
323 */
324/*----------------------------------------------------------------------------*/
325
326extern "C" const char *
328
329/*----------------------------------------------------------------------------*/
330/*
331 * \brief Return type name of preconditioner context.
332 *
333 * The returned string is intended mainly for logging.
334 *
335 * \param[in] pc pointer to preconditioner object
336 */
337/*----------------------------------------------------------------------------*/
338
339extern "C" const char *
341
342/*----------------------------------------------------------------------------*/
343/*
344 * \brief Return pointer to preconditioner context structure pointer.
345 *
346 * The context structure depends on the type of preconditioner used, which may
347 * in turn be determined by the string returned by cs_sles_pc_get_type().
348 * If may be used by appropriate functions specific to that type.
349 *
350 * \param[in] pc pointer to preconditioner object
351 *
352 * \return pointer to preconditioner-specific info and context
353 */
354/*----------------------------------------------------------------------------*/
355
356extern "C" void *
358
359/*----------------------------------------------------------------------------*/
360/*
361 * \brief Return a pointer to the function used to apply a preconditioner.
362 *
363 * This allows calling the preconditioner with one less level of indirection.
364 *
365 * \param[in] pc pointer to preconditioner object
366 *
367 * \return preconditioner apply function
368 */
369/*----------------------------------------------------------------------------*/
370
371extern "C" cs_sles_pc_apply_t *
373
374/*----------------------------------------------------------------------------*/
375/*
376 * \brief Set the required tolerance for preconditioners involving an
377 * iterative solver.
378 *
379 * This will usually not be relevant to non-iterative preconditioners,
380 * in which case this is a no-op.
381 *
382 * If no options were previously provided for the matching system,
383 * default options will be used.
384 *
385 * The system is considered to have converged when
386 * residual/r_norm <= precision, residual being the L2 norm of a.vx-rhs.
387 *
388 * \param[in, out] pc pointer to preconditioner object
389 * \param[in] precision preconditioner precision
390 * \param[in] r_norm residual normalization
391 */
392/*----------------------------------------------------------------------------*/
393
394extern "C" void
396 double precision,
397 double r_norm);
398
399/*----------------------------------------------------------------------------*/
400/*
401 * \brief Setup sparse linear equation preconditioner.
402 *
403 * Use of this function is optional: if a \ref cs_sles_solve is called
404 * for the same system before this function is called, the latter will be
405 * called automatically.
406 *
407 * If no options were previously provided for the matching system,
408 * default options will be used.
409 *
410 * \param[in, out] pc pointer to preconditioner object
411 * \param[in] name linear system name
412 * \param[in] a matrix
413 * \param[in] accel use accelerator version ?
414 * \param[in] verbosity verbosity level
415 */
416/*----------------------------------------------------------------------------*/
417
418extern "C" void
420 const char *name,
421 const cs_matrix_t *a,
422 bool accel,
423 int verbosity);
424
425/*----------------------------------------------------------------------------*/
426/*
427 * \brief Apply a preconditioner.
428 *
429 * If no options were previously provided for the matching system,
430 * default options will be used.
431 *
432 * In cases where it is desired that the preconditioner modify a vector
433 * "in place", x_in should be set to NULL, and x_out contain the vector to
434 * be modified (\f$x_{out} \leftarrow M^{-1}x_{out})\f$).
435 *
436 * \param[in, out] pc pointer to preconditioner object
437 * \param[in] x_in input vector
438 * \param[in, out] x_out input/output vector
439 *
440 * \return preconditioner application status
441 */
442/*----------------------------------------------------------------------------*/
443
444extern "C" cs_sles_pc_state_t
446 cs_real_t *x_in,
447 cs_real_t *x_out);
448
449/*----------------------------------------------------------------------------*/
450/*
451 * \brief Free preconditioner setup.
452 *
453 * This function frees resolution-related data, such as multigrid hierarchy,
454 * preconditioning, and any other temporary arrays or objects required for
455 * resolution, but maintains context information such as that used for
456 * logging (especially performance data).
457 *
458 * \param[in, out] pc pointer to preconditioner object
459 */
460/*----------------------------------------------------------------------------*/
461
462extern "C" void
464
465/*----------------------------------------------------------------------------*/
466/*
467 * \brief Create an "identity" (or null) preconditioner.
468 *
469 * \return pointer to newly created preconditioner object.
470 */
471/*----------------------------------------------------------------------------*/
472
473extern "C" cs_sles_pc_t *
475
476/*----------------------------------------------------------------------------*/
477/*
478 * \brief Create a Jacobi preconditioner.
479 *
480 * \return pointer to newly created preconditioner object.
481 */
482/*----------------------------------------------------------------------------*/
483
484extern "C" cs_sles_pc_t *
486
487/*----------------------------------------------------------------------------*/
488/*
489 * \brief Create a polynomial preconditioner of degree 1.
490 *
491 * \return pointer to newly created preconditioner object.
492 */
493/*----------------------------------------------------------------------------*/
494
495extern "C" cs_sles_pc_t *
497
498/*----------------------------------------------------------------------------*/
499/*
500 * \brief Create a polynomial preconditioner of degree 2.
501 *
502 * \return pointer to newly created preconditioner object.
503 */
504/*----------------------------------------------------------------------------*/
505
506extern "C" cs_sles_pc_t *
508
509/*----------------------------------------------------------------------------
510 * Direct access to the diagonal inverse values for a jacobi or
511 * polynomial preconditioner.
512 *
513 * This may be useful for fusing specific kernels while staying in the general
514 * framework so as to correctly handle setp/free phases.
515 *
516 * parameters:
517 * context <-> pointer to preconditioner context
518 *
519 * returns:
520 * pointer to diagonal inverse if present, null otherwise
521 *----------------------------------------------------------------------------*/
522
523extern "C" const cs_real_t *
524cs_sles_pc_get_ad_inv(void *context);
525
526/*----------------------------------------------------------------------------*/
527
528#endif /* CS_SLES_PC_H */
double cs_real_t
Floating-point value.
Definition: cs_defs.h:332
cs_log_t
Definition: cs_log.h:44
struct _cs_matrix_t cs_matrix_t
Definition: cs_matrix.h:108
cs_sles_pc_state_t() cs_sles_pc_apply_t(void *context, const cs_real_t *x_in, cs_real_t *x_out)
Function pointer for application of a preconditioner.
Definition: cs_sles_pc.h:144
cs_sles_pc_t * cs_sles_pc_jacobi_create(void)
Create a Jacobi preconditioner.
Definition: cs_sles_pc.cpp:1064
void() cs_sles_pc_free_t(void *context)
Function pointer for freeing of a preconditioner's context data.
Definition: cs_sles_pc.h:162
const char * cs_sles_pc_get_type_name(cs_sles_pc_t *pc)
Return type name of preconditioner context.
Definition: cs_sles_pc.cpp:839
void() cs_sles_pc_tolerance_t(void *context, double precision, double r_norm)
Function pointer for setting of the required tolerance for preconditioners involving an iterative sol...
Definition: cs_sles_pc.h:123
cs_sles_pc_state_t
Definition: cs_sles_pc.h:52
@ CS_SLES_PC_MAX_ITERATION
Definition: cs_sles_pc.h:57
@ CS_SLES_PC_CONVERGED
Definition: cs_sles_pc.h:59
@ CS_SLES_PC_DIVERGED
Definition: cs_sles_pc.h:54
@ CS_SLES_PC_BREAKDOWN
Definition: cs_sles_pc.h:55
struct _cs_sles_pc_t cs_sles_pc_t
Definition: cs_sles_pc.h:65
cs_sles_pc_t * cs_sles_pc_poly_2_create(void)
Create a Polynomial preconditioner of degree 2.
Definition: cs_sles_pc.cpp:1120
void cs_sles_pc_setup(cs_sles_pc_t *pc, const char *name, const cs_matrix_t *a, bool accel, int verbosity)
Setup sparse linear equation preconditioner.
Definition: cs_sles_pc.cpp:944
cs_sles_pc_t * cs_sles_pc_none_create(void)
Create an "identity" (or null) preconditioner.
Definition: cs_sles_pc.cpp:1036
void() cs_sles_pc_destroy_t(void **context)
Definition: cs_sles_pc.h:211
const char * cs_sles_pc_get_type(cs_sles_pc_t *pc)
Return type name of preconditioner context.
Definition: cs_sles_pc.cpp:818
void cs_sles_pc_log(cs_sles_pc_t *pc, cs_log_t log_type)
Log preconditioner setup, history and performance data.
Definition: cs_sles_pc.cpp:1020
const char *() cs_sles_pc_get_type_t(const void *context, bool logging)
Function pointer returning the type name of a preconditioner context.
Definition: cs_sles_pc.h:83
void *() cs_sles_pc_clone_t(const void *context)
Function pointer for creation of a preconditioner context based on the copy of another.
Definition: cs_sles_pc.h:199
cs_sles_pc_apply_t * cs_sles_pc_get_apply_func(const cs_sles_pc_t *pc)
Return a pointer to the function used to apply a preconditioner.
Definition: cs_sles_pc.cpp:887
cs_sles_pc_t * cs_sles_pc_define(void *context, cs_sles_pc_get_type_t *get_type_func, cs_sles_pc_setup_t *setup_func, cs_sles_pc_tolerance_t *tolerance_func, cs_sles_pc_apply_t *apply_func, cs_sles_pc_free_t *free_func, cs_sles_pc_log_t *log_func, cs_sles_pc_clone_t *clone_func, cs_sles_pc_destroy_t *destroy_func)
Define sparse linear equation preconditioner.
Definition: cs_sles_pc.cpp:712
cs_sles_pc_t * cs_sles_pc_clone(const cs_sles_pc_t *src)
Create a new preconditioner context based on the copy of another.
Definition: cs_sles_pc.cpp:778
cs_sles_pc_state_t cs_sles_pc_apply(cs_sles_pc_t *pc, cs_real_t *x_in, cs_real_t *x_out)
Apply a preconditioner.
Definition: cs_sles_pc.cpp:976
cs_sles_pc_t * cs_sles_pc_poly_1_create(void)
Create a Polynomial preconditioner of degree 1.
Definition: cs_sles_pc.cpp:1092
void cs_sles_pc_free(cs_sles_pc_t *pc)
Free preconditioner setup.
Definition: cs_sles_pc.cpp:997
void * cs_sles_pc_get_context(cs_sles_pc_t *pc)
Return pointer to preconditioner context structure pointer.
Definition: cs_sles_pc.cpp:864
void() cs_sles_pc_log_t(const void *context, cs_log_t log_type)
Function pointer for logging of preconditioner history and performance data.
Definition: cs_sles_pc.h:177
void() cs_sles_pc_setup_t(void *context, const char *name, const cs_matrix_t *a, bool accel, int verbosity)
Function pointer for pre-resolution setup of a preconditioner context.
Definition: cs_sles_pc.h:100
void cs_sles_pc_set_tolerance(cs_sles_pc_t *pc, double precision, double r_norm)
Set the required tolerance for preconditioners involving an iterative solver.
Definition: cs_sles_pc.cpp:913
const cs_real_t * cs_sles_pc_get_ad_inv(void *context)
Definition: cs_sles_pc.cpp:1154
void cs_sles_pc_destroy(cs_sles_pc_t **pc)
Destroy a sparse linear equation preconditioner.
Definition: cs_sles_pc.cpp:750