9.2
general documentation
cs_sles_it_priv.h
Go to the documentation of this file.
1#ifndef CS_SLES_IT_PRIV_H
2#define CS_SLES_IT_PRIV_H
3
4/*============================================================================
5 * Sparse Linear Equation Solvers: private elements.
6 *
7 * These elements are shared between iterative solvers and smoother
8 * both for host and device implementations, but are not accessible to
9 * calling code.
10 *============================================================================*/
11
12/*
13 This file is part of code_saturne, a general-purpose CFD tool.
14
15 Copyright (C) 1998-2026 EDF S.A.
16
17 This program is free software; you can redistribute it and/or modify it under
18 the terms of the GNU General Public License as published by the Free Software
19 Foundation; either version 2 of the License, or (at your option) any later
20 version.
21
22 This program is distributed in the hope that it will be useful, but WITHOUT
23 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
24 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
25 details.
26
27 You should have received a copy of the GNU General Public License along with
28 this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
29 Street, Fifth Floor, Boston, MA 02110-1301, USA.
30*/
31
32/*----------------------------------------------------------------------------*/
33
34#include "base/cs_defs.h"
35
36/*----------------------------------------------------------------------------
37 * Standard C library headers
38 *----------------------------------------------------------------------------*/
39
40#include <assert.h>
41#include <math.h>
42
43#if defined(HAVE_MPI)
44#include <mpi.h>
45#endif
46
47#if defined(HAVE_NCCL)
48#include <nccl.h>
49#endif
50
51/*----------------------------------------------------------------------------
52 * Local headers
53 *----------------------------------------------------------------------------*/
54
55#include "bft/bft_error.h"
56#include "bft/bft_printf.h"
57
58#include "base/cs_base.h"
59#include "alge/cs_blas.h"
60#include "base/cs_file.h"
61#include "base/cs_log.h"
62#include "base/cs_halo.h"
63#include "base/cs_mem.h"
64#include "mesh/cs_mesh.h"
65#include "alge/cs_matrix.h"
67#include "alge/cs_matrix_util.h"
68#include "base/cs_post.h"
69#include "base/cs_timer.h"
70#include "base/cs_time_plot.h"
71
72/*----------------------------------------------------------------------------
73 * Header for the current file
74 *----------------------------------------------------------------------------*/
75
76#include "alge/cs_sles.h"
77#include "alge/cs_sles_it.h"
78#include "alge/cs_sles_pc.h"
79
80/*----------------------------------------------------------------------------*/
81
84/*=============================================================================
85 * Local Macro Definitions
86 *============================================================================*/
87
88#if !defined(HUGE_VAL)
89#define HUGE_VAL 1.E+12
90#endif
91
92#define DB_SIZE_MAX 9
93
94/*=============================================================================
95 * Local Structure Definitions
96 *============================================================================*/
97
98/*----------------------------------------------------------------------------
99 * Function pointer for actual resolution of a linear system.
100 *
101 * parameters:
102 * c <-- pointer to solver context info
103 * a <-- linear equation matrix
104 * convergence <-- convergence information structure
105 * rhs <-- right hand side
106 * vx_ini <-- initial system solution
107 * (vx if nonzero, nullptr if zero)
108 * vx <-> system solution
109 * aux_size <-- number of elements in aux_vectors (in bytes)
110 * aux_vectors --- optional working area (allocation otherwise)
111 *
112 * returns:
113 * convergence status
114 *----------------------------------------------------------------------------*/
115
117(cs_sles_it_solve_t) (cs_sles_it_t *c,
118 const cs_matrix_t *a,
119 cs_lnum_t diag_block_size,
120 cs_sles_it_convergence_t *convergence,
121 const cs_real_t *rhs,
122 cs_real_t *vx_ini,
123 cs_real_t *vx,
124 size_t aux_size,
125 void *aux_vectors);
126
127/* Solver setup data */
128/*-------------------*/
129
130typedef struct _cs_sles_it_setup_t {
131
132 double initial_residual; /* last initial residual value */
133
134 cs_lnum_t n_rows; /* number of associated rows */
135
136 const cs_real_t *ad_inv; /* pointer to diagonal inverse */
137 cs_real_t *_ad_inv; /* private pointer to
138 diagonal inverse */
139
140 void *pc_context; /* preconditioner context */
141 cs_sles_pc_apply_t *pc_apply; /* preconditioner apply */
142
143} cs_sles_it_setup_t;
144
145/* Solver additional data */
146/*------------------------*/
147
148typedef struct _cs_sles_it_add_t {
149
150 cs_lnum_t *order; /* ordering */
151
152} cs_sles_it_add_t;
153
154/* Basic per linear system options and logging */
155/*---------------------------------------------*/
156
157struct _cs_sles_it_t {
158
159 /* Base settings */
160
161 cs_sles_it_type_t type; /* Solver type */
162
163 bool on_device; /* SpMV on device ? */
164
165 bool update_stats; /* do stats need to be updated ? */
166 bool ignore_convergence; /* ignore convergence for some
167 solvers used as preconditioners */
168
169 int n_max_iter; /* maximum number of iterations */
170 int restart_interval; /* maximum number of iterations
171 before restarting the algorithm
172 (only applicable for GMRES or GCR
173 algorithm up to now) */
174
175 cs_sles_it_solve_t *solve; /* pointer to solve function */
176
177 cs_sles_pc_t *pc; /* pointer to possibly shared
178 preconditioner object */
179 cs_sles_pc_t *_pc; /* pointer to owned
180 preconditioner object */
181
182 /* Performance data */
183
184 unsigned n_setups; /* Number of times system setup */
185 unsigned n_solves; /* Number of times system solved */
186
187 unsigned n_iterations_last; /* Number of iterations for last
188 system resolution */
189 unsigned n_iterations_min; /* Minimum number ot iterations
190 in system resolution history */
191 unsigned n_iterations_max; /* Maximum number ot iterations
192 in system resolution history */
193 unsigned long long n_iterations_tot; /* Total accumulated number of
194 iterations */
195
196 cs_timer_counter_t t_setup; /* Total setup */
197 cs_timer_counter_t t_solve; /* Total time used */
198
199 /* Plot info */
200
201 int plot_time_stamp; /* Plot time stamp */
202 cs_time_plot_t *plot; /* Pointer to plot structure,
203 which may be owned or shared */
204 cs_time_plot_t *_plot; /* Pointer to own plot structure */
205
206 /* Communicator used for reduction operations
207 (if left at NULL, main communicator will be used) */
208
209# if defined(HAVE_MPI)
210 MPI_Comm comm;
211 MPI_Comm caller_comm;
212 int caller_n_ranks;
213# endif
214
215#if defined(HAVE_NCCL)
216 ncclComm_t nccl_comm;
217#endif
218
219 const struct _cs_sles_it_t *shared; /* pointer to context sharing some
220 setup and preconditioner data,
221 or NULL */
222
223 cs_sles_it_add_t *add_data; /* additional data */
224
225 cs_sles_it_setup_t *setup_data; /* setup data */
226
227 /* Alternative solvers (fallback or heuristics) */
228
229 cs_sles_convergence_state_t fallback_cvg; /* threshold for fallback
230 convergence */
231 int fallback_n_max_iter; /* number of maximum iteration
232 for fallback solver */
233
234 cs_sles_it_t *fallback; /* fallback solver */
235
236
237};
238
239/* Convergence testing and tracking */
240/*----------------------------------*/
241
242struct _cs_sles_it_convergence_t {
243
244 const char *name; /* Pointer to name string */
245
246 int verbosity; /* Verbosity level */
247
248 unsigned n_iterations; /* Current number of iterations */
249 unsigned n_iterations_max; /* Maximum number of iterations */
250
251 double precision; /* Precision limit */
252 double r_norm; /* Residual normalization */
253 double residual; /* Current residual */
254
255};
256
257/*============================================================================
258 * Inline static function definitions
259 *============================================================================*/
260
261/*----------------------------------------------------------------------------
262 * Block Jacobi utilities.
263 * Compute forward and backward to solve an LU 3*3 system.
264 *
265 * parameters:
266 * mat <-- 3*3*dim matrix
267 * x --> solution
268 * b --> 1st part of RHS (c - b)
269 * c --> 2nd part of RHS (c - b)
270 *----------------------------------------------------------------------------*/
271
272inline static void
273_mat_c_m_b_33(const cs_real_t mat[],
275 const cs_real_t *restrict b,
276 const cs_real_t *restrict c)
277{
278 /* c - b */
279 cs_real_t aux[3];
280 for (cs_lnum_t ii = 0; ii < 3; ii++) {
281 aux[ii] = (c[ii] - b[ii]);
282 }
283
284 for (cs_lnum_t ii = 0; ii < 3; ii++) {
285 x[ii] = mat[3*ii + 0]*aux[0]
286 + mat[3*ii + 1]*aux[1]
287 + mat[3*ii + 2]*aux[2];
288 }
289}
290
291/*----------------------------------------------------------------------------
292 * Block Jacobi utilities.
293 * Compute mat.(c-b) product.
294 *
295 * parameters:
296 * mat <-- P*P*dim matrix
297 * db_size <-- matrix size
298 * x --> solution
299 * b --> 1st part of RHS (c - b)
300 * c --> 2nd part of RHS (c - b)
301 *----------------------------------------------------------------------------*/
302
303inline static void
304_mat_c_m_b(const cs_real_t mat[],
305 cs_lnum_t db_size,
307 const cs_real_t *restrict b,
308 const cs_real_t *restrict c)
309{
310 assert(db_size <= DB_SIZE_MAX);
311 cs_real_t aux[DB_SIZE_MAX];
312
313 /* c - b */
314 for (cs_lnum_t ii = 0; ii < db_size; ii++) {
315 aux[ii] = (c[ii] - b[ii]);
316 }
317
318 for (cs_lnum_t ii = 0; ii < db_size; ii++) {
319 x[ii] = 0;
320 for (cs_lnum_t jj = 0; jj < db_size; jj++) {
321 x[ii] += aux[jj]*mat[ii*db_size + jj];
322 }
323 }
324}
325
326/*============================================================================
327 * Semi-private function definitions
328 *============================================================================*/
329
330/*----------------------------------------------------------------------------*/
331/*
332 * \brief Set execution location (host or device) and stream if appplicable.
333 *
334 * \param[in, out] ctx reference to dispatch context
335 * \param[in] a pointer to matrix
336 * \param[out] local_stream do we force a local stream ?
337 * \param[out] stream stream associated with current solve
338 */
339/*----------------------------------------------------------------------------*/
340
341void
342cs_sles_it_set_exec_location
343(
344 [[maybe_unused]] cs_dispatch_context &ctx,
345 const cs_matrix_t *a,
346 bool &local_stream,
347 [[maybe_unused]] cs_stream_t &stream
348);
349
350/*----------------------------------------------------------------------------*/
351/*
352 * \brief Restore stream to previous settings if appplicable.
353 *
354 * \param[in] local_stream do we force a local stream ?
355 */
356/*----------------------------------------------------------------------------*/
357
358void
359cs_sles_it_restore_exec_location([[maybe_unused]] bool &local_stream);
360
361/*----------------------------------------------------------------------------*/
362/*
363 * \brief Initialize or reset convergence info structure.
364 * At this stage, the initial residual is set to HUGE_VAL, as it is
365 * unknown.
366 *
367 * \param[in, out] convergence convergence info structure
368 * \param[in] solver_name solver name
369 * \param[in] verbosity verbosity level
370 * \param[in] n_iter_max maximum number of iterations
371 * \param[in] precision precision limit
372 * \param[in] r_norm residual normalization
373 * \param[in, out] residual initial residual
374 */
375/*----------------------------------------------------------------------------*/
376
377void
378cs_sles_it_convergence_init(cs_sles_it_convergence_t *convergence,
379 const char *solver_name,
380 int verbosity,
381 unsigned n_iter_max,
382 double precision,
383 double r_norm,
384 double *residual);
385
386/*----------------------------------------------------------------------------
387 * Setup context for iterative linear solver.
388 *
389 * This function is common to most solvers
390 *
391 * parameters:
392 * c <-> pointer to solver context info
393 * name <-- pointer to system name
394 * a <-- matrix
395 * verbosity <-- verbosity level
396 * diag_block_size <-- diagonal block size
397 * block_nn_inverse <-- if diagonal block size is 3 or 6, compute inverse of
398 * block if true, inverse of block diagonal otherwise
399 * l1_inv <-- if diagonal block size is 1, compute
400 * inverse of L1 norm instead of diagonal
401 *----------------------------------------------------------------------------*/
402
403void
404cs_sles_it_setup_priv(cs_sles_it_t *c,
405 const char *name,
406 const cs_matrix_t *a,
407 int verbosity,
408 int diag_block_size,
409 bool block_nn_inverse,
410 bool l1_inv);
411
414/*----------------------------------------------------------------------------*/
415
416#endif /* CS_SLES_IT_PRIV_H */
Definition: cs_dispatch.h:2288
#define restrict
Definition: cs_defs.h:148
void * cs_stream_t
Definition: cs_defs.h:135
double cs_real_t
Floating-point value.
Definition: cs_defs.h:332
int cs_lnum_t
local mesh entity id
Definition: cs_defs.h:325
struct _cs_matrix_t cs_matrix_t
Definition: cs_matrix.h:108
cs_sles_convergence_state_t
Definition: cs_sles.h:57
struct _cs_sles_it_t cs_sles_it_t
Definition: cs_sles_it.h:95
struct _cs_sles_it_convergence_t cs_sles_it_convergence_t
Definition: cs_sles_it.h:99
cs_sles_it_type_t
Definition: cs_sles_it.h:55
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
struct _cs_sles_pc_t cs_sles_pc_t
Definition: cs_sles_pc.h:65
struct _cs_time_plot_t cs_time_plot_t
Definition: cs_time_plot.h:44
Definition: cs_timer.h:51