SuperDex Physics C++ API
Loading...
Searching...
No Matches
linear_solver_params.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19// PLEASE DO NOT ADD OTHER INCLUDES HERE. This header is included in the mochi_physics public API.
23
24namespace superdex {
25/** @brief Linear solver types. */
26enum struct LinearSolverType {
27 /**
28 * @brief Conjugate Gradient (CG) solver.
29 *
30 * @note Only valid for symmetric positive-definite systems.
31 * @note Unlike @ref LinearSolverType::ParallelCG, only matrix-vector products are parallelized.
32 * CG orthogonalization and preconditioner solves are not.
33 */
35
36 /** @brief Generalized Minimal Residual (GMRES) solver. */
38
39 /**
40 * @brief CUDA CG solver.
41 *
42 * @note Only valid for symmetric positive-definite systems.
43 * @note Requires building with CUDA and a preconditioner with CUDA support.
44 *
45 * @see PreconditionerType
46 */
48
49 /**
50 * @brief CUDA GMRES solver.
51 *
52 * @note Requires building with CUDA and a preconditioner with CUDA support.
53 *
54 * @see PreconditionerType
55 */
57
58 /**
59 * @brief [Experimental] Augmented CG solver with Krylov subspace recycling.
60 *
61 * @warning This is an experimental feature. It may be changed or removed in the future. Use at
62 * your own risk.
63 *
64 * @note Only valid for symmetric positive-definite systems.
65 * @note Requires building with Eigen.
66 */
68
69 /**
70 * @brief Direct solver based on the LDLt factorization without pivoting.
71 *
72 * @note Only valid for symmetric systems.
73 * @note May be unstable for indefinite or ill-conditioned systems.
74 * @note Uses a dense or a sparse factorization depending on the size and sparsity of the system.
75 */
77
78 /**
79 * @brief Direct solver based on the dense LU factorization without pivoting.
80 *
81 * @note May be unstable for ill-conditioned systems.
82 * @note The cost of the factorization is O(n^3). Will be slow for large systems.
83 * @note Consider using @ref LinearSolverType::LDLT if the system is symmetric.
84 */
86
87 /**
88 * @brief [Experimental] Asynchronous CG solver.
89 *
90 * @warning This is an experimental feature. It may be changed or removed in the future. Use at
91 * your own risk.
92 *
93 * @note Only valid for symmetric positive-definite systems.
94 * @note Async CG is a reformulation of the classical CG algorithm to compute the matrix-vector
95 * products in parallel to the orthogonalization and convergence check.
96 * @note Equivalent to the classical CG algorithm in exact-precision arithmetic, but has inferior
97 * stability properties in finite-precision arithmetic.
98 */
100
101 /**
102 * @brief [Experimental] Parallel CG solver.
103 *
104 * @warning Not deterministic.
105 * @warning This is an experimental feature. It may be changed or removed in the future. Use at
106 * your own risk.
107 *
108 * @note Only valid for symmetric positive-definite systems.
109 * @note Unlike @ref LinearSolverType::CG, all operations (matrix-vector products, preconditioner
110 * solves, CG orthogonalization) are parallelized.
111 * @note Requires a preconditioner with a parallel solve.
112 */
114
115 /**
116 * @brief Minimal Residual (MINRES) solver.
117 *
118 * @note Only valid for symmetric systems.
119 */
121
122 /**
123 * @brief [Experimental] CUDA sparse Cholesky factorization.
124 *
125 * @warning This is an experimental feature. It may be changed or removed in the future. Use at
126 * your own risk.
127 *
128 * @note Only valid for sparse or block-sparse symmetric positive-definite systems.
129 * @note Requires building with CUDA.
130 */
132
133 /**
134 * @brief [Experimental] CUDA sparse LDLt factorization.
135 *
136 * @warning This is an experimental feature. It may be changed or removed in the future. Use at
137 * your own risk.
138 *
139 * @note Only valid for sparse or block-sparse symmetric systems.
140 * @note Requires building with CUDA and cuDSS.
141 */
143
144 /**
145 * @brief [Experimental] CUDA sparse LU factorization.
146 *
147 * @warning This is an experimental feature. It may be changed or removed in the future. Use at
148 * your own risk.
149 *
150 * @note Only valid for sparse or block-sparse systems.
151 * @note Requires building with CUDA.
152 */
154
155 /** @brief Let Mochi select the solver type based on the problem. */
157
158 /** @brief Number of solver type enum values. */
160
161 /** @brief Default solver type. */
163};
164} // namespace superdex
165
168MOCHI_ENUM_ITEM(GMRES)
169MOCHI_ENUM_ITEM(CudaCG)
170MOCHI_ENUM_ITEM(CudaGMRES)
171MOCHI_ENUM_ITEM(AugmentedCG)
172MOCHI_ENUM_ITEM(LDLT)
174MOCHI_ENUM_ITEM(AsyncCG)
175MOCHI_ENUM_ITEM(ParallelCG)
176MOCHI_ENUM_ITEM(MINRES)
177MOCHI_ENUM_ITEM(ExperimentalCudaSparseCholesky)
178MOCHI_ENUM_ITEM(ExperimentalCudaSparseLDLT)
179MOCHI_ENUM_ITEM(ExperimentalCudaSparseLU)
180MOCHI_ENUM_ITEM(Auto)
181MOCHI_ENUM_COUNT(Count)
183
184namespace superdex {
185/**
186 * @brief Preconditioner types for the linear solver.
187 *
188 * @note Only used for iterative solvers.
189 * @note CUDA iterative solvers only support @ref PreconditionerType::None, @ref
190 * PreconditionerType::Jacobi and @ref PreconditionerType::BlockJacobi preconditioners.
191 */
193 /** @brief No preconditioner. */
195
196 /**
197 * @brief Jacobi preconditioner.
198 *
199 * @note Only valid for matrices with non-zero diagonal entries.
200 */
202
203 /**
204 * @brief Block Jacobi preconditioner with block size of 3.
205 *
206 * @note Only valid for matrices whose size is a multiple of 3 and whose 3x3 diagonal blocks are
207 * non-singular.
208 */
210
211 /**
212 * @brief Symmetric successive over-relaxation (SSOR) preconditioner.
213 *
214 * @note Only valid for symmetric positive-definite matrices.
215 */
217
218 /**
219 * @brief Block symmetric successive over-relaxation (SSOR) preconditioner with block size of 3.
220 *
221 * @note Only valid for symmetric positive-definite matrices whose size is a multiple of 3 and
222 * whose 3x3 diagonal blocks are non-singular.
223 */
225
226 /**
227 * @brief Dense LU factorization without pivoting.
228 *
229 * @note May be unstable for ill-conditioned matrices.
230 * @note The iterative solver should converge in 1 iteration.
231 */
233
234 /**
235 * @brief Algebraic multigrid (AMG).
236 *
237 * @note Only valid for 3x3 block sparse symmetric positive-definite matrices with non-singular
238 * 3x3 diagonal blocks.
239 */
241
242 /**
243 * @brief Symmetric inverse preconditioner based on the dense LDLt factorization without pivoting.
244 *
245 * @note Only valid for symmetric matrices.
246 * @note May be unstable for indefinite or ill-conditioned matrices.
247 * @note The iterative solver should converge in 1 iteration.
248 */
250
251 /**
252 * @brief Dense LDLt factorization without pivoting.
253 *
254 * @note Only valid for symmetric matrices.
255 * @note May be unstable for indefinite or ill-conditioned matrices.
256 * @note The iterative solver should converge in 1 iteration.
257 */
259
260 /** @brief Incomplete LU factorization with zero fill-in. */
262
263 /**
264 * @brief Incomplete Cholesky factorization with zero fill-in.
265 *
266 * @note Only valid for symmetric positive-definite matrices.
267 */
269
270 /**
271 * @brief Domain decomposition preconditioner, where each actor is a subdomain.
272 *
273 * @note The most appropriate preconditioner type is used for each actor.
274 */
276
277 /**
278 * @brief [Experimental] Colored symmetric successive over-relaxation (SSOR) preconditioner.
279 *
280 * @warning This is an experimental feature. It may be changed or removed in the future. Use at
281 * your own risk.
282 *
283 * @note Only valid for symmetric positive-definite matrices.
284 */
286
287 /** @brief Number of preconditioner type enum values. */
289
290 /** @brief Default preconditioner type. */
292};
293} // namespace superdex
294
296MOCHI_ENUM_ITEM(None)
297MOCHI_ENUM_ITEM(Jacobi)
298MOCHI_ENUM_ITEM(BlockJacobi)
299MOCHI_ENUM_ITEM(SSOR)
300MOCHI_ENUM_ITEM(BlockSSOR)
303MOCHI_ENUM_ITEM(SymInverse)
304MOCHI_ENUM_ITEM(LDLT)
305MOCHI_ENUM_ITEM(ILU0)
307MOCHI_ENUM_ITEM(PerActor)
308MOCHI_ENUM_ITEM(ColoredSSOR)
309MOCHI_ENUM_COUNT(Count)
311
312namespace superdex {
313/**
314 * @brief Norm types for the stopping criteria of the linear solver.
315 *
316 * @note This setting only applies to @ref LinearSolverType::CG, @ref LinearSolverType::CudaCG, @ref
317 * LinearSolverType::ParallelCG and @ref LinearSolverType::AsyncCG. @ref LinearSolverType::GMRES,
318 * @ref LinearSolverType::CudaGMRES, @ref LinearSolverType::MINRES, and @ref
319 * LinearSolverType::AugmentedCG always use the L2-norm of the residual regardless of this setting.
320 * Direct solvers do not have stop criteria.
321 */
323 ResidualL2, ///< L2-norm of the residual: \f$\|r\|_2\f$.
324 PreconditionedResidualL2, ///< L2-norm of the preconditioned residual: \f$\|z\|_2\f$ where
325 ///< \f$z = M^{-1}r\f$.
326 ResidualPreconditionerInduced, ///< Preconditioner-induced norm of the residual:
327 ///< \f$\sqrt{\langle r, z \rangle}\f$ where \f$z = M^{-1}r\f$.
328 Count, ///< Number of linear solver convergence norm enum values.
329 Default = PreconditionedResidualL2 ///< Default linear solver convergence norm.
330};
331} // namespace superdex
332
334MOCHI_ENUM_ITEM(ResidualL2)
335MOCHI_ENUM_ITEM(PreconditionedResidualL2)
336MOCHI_ENUM_ITEM(ResidualPreconditionerInduced)
337MOCHI_ENUM_COUNT(Count)
339
340namespace superdex {
341
342/** @brief Sentinel value for @ref LinearSolverParams::maxIter that lets Mochi automatically select
343 * the maximum number of linear solver iterations. */
344constexpr int kAutoLinearSolverMaxIter = -1;
345
346/** @brief Parameters for the linear solve performed in each iteration of the non-linear solver. */
348 /** @brief Linear solver type. */
350
351 /**
352 * @brief Preconditioner type.
353 *
354 * @note Applies only to iterative solvers.
355 */
357
358 /**
359 * @brief Norm used for the stopping criteria.
360 *
361 * @note Applies only to the solvers documented in @ref LinearSolverConvergenceNorm.
362 */
364
365 /**
366 * @brief Absolute residual norm tolerance for convergence.
367 *
368 * @note Applies only to iterative solvers.
369 */
370 real absTol = 1e-9_r;
371
372 /**
373 * @brief Relative residual norm tolerance for convergence, relative to the initial residual.
374 *
375 * @note Applies only to iterative solvers.
376 * @note Ignored when using @ref LinearToleranceStrategy::EisenstatWalker1 or @ref
377 * LinearToleranceStrategy::EisenstatWalker2 in the non-linear solver.
378 * @note For @ref LinearToleranceStrategy::EisenstatWalker3, this is used as a lower bound on the
379 * adaptive tolerance in every non-linear iteration.
380 */
381 real relTol = 1e-5_r;
382
383 /**
384 * @brief Relative residual norm tolerance for divergence, relative to the initial residual.
385 *
386 * @note Applies only to iterative solvers.
387 */
388 real relDivTol = 1e10_r;
389
390 /**
391 * @brief Maximum number of linear solver iterations.
392 *
393 * @note Applies only to iterative solvers.
394 * @note Must be non-negative or @ref kAutoLinearSolverMaxIter.
395 * @note @ref kAutoLinearSolverMaxIter lets Mochi select the maximum number of iterations based
396 * on the problem.
397 */
399
400 /**
401 * @brief Krylov subspace dimension before restarting.
402 *
403 * @note Applies only to GMRES solvers (e.g., @ref LinearSolverType::GMRES, @ref
404 * LinearSolverType::CudaGMRES).
405 */
406 int restartSize = 1000;
407
408 /**
409 * @brief Abort solve if matrix is not SPD.
410 *
411 * @note Applies only to iterative SPD solvers (e.g., @ref LinearSolverType::CG, @ref
412 * LinearSolverType::CudaCG, @ref LinearSolverType::AugmentedCG).
413 */
414 bool abortIfNotSpd = false;
415
416 /** @brief Verbosity level for logging output. */
418
419#if MOCHI_LANGUAGE_CPP20
420 bool operator==(LinearSolverParams const&) const = default;
421#endif
422
435};
436
437} // namespace superdex
VerbosityLevel
Verbosity levels for solvers and optimizers.
@ Warning
Errors and warnings.
PreconditionerType
Preconditioner types for the linear solver.
@ ILU0
Incomplete LU factorization with zero fill-in.
@ ColoredSSOR
[Experimental] Colored symmetric successive over-relaxation (SSOR) preconditioner.
@ AMG
Algebraic multigrid (AMG).
@ SSOR
Symmetric successive over-relaxation (SSOR) preconditioner.
@ Default
Default preconditioner type.
@ IC0
Incomplete Cholesky factorization with zero fill-in.
@ SymInverse
Symmetric inverse preconditioner based on the dense LDLt factorization without pivoting.
@ PerActor
Domain decomposition preconditioner, where each actor is a subdomain.
@ BlockJacobi
Block Jacobi preconditioner with block size of 3.
@ BlockSSOR
Block symmetric successive over-relaxation (SSOR) preconditioner with block size of 3.
LinearSolverConvergenceNorm
Norm types for the stopping criteria of the linear solver.
@ ResidualPreconditionerInduced
Preconditioner-induced norm of the residual: where .
@ PreconditionedResidualL2
L2-norm of the preconditioned residual: where .
@ Default
Default linear solver convergence norm.
LinearSolverType
Linear solver types.
@ ExperimentalCudaSparseCholesky
[Experimental] CUDA sparse Cholesky factorization.
@ CG
Conjugate Gradient (CG) solver.
@ AugmentedCG
[Experimental] Augmented CG solver with Krylov subspace recycling.
@ LU
Direct solver based on the dense LU factorization without pivoting.
@ AsyncCG
[Experimental] Asynchronous CG solver.
@ LDLT
Direct solver based on the LDLt factorization without pivoting.
@ GMRES
Generalized Minimal Residual (GMRES) solver.
@ ExperimentalCudaSparseLDLT
[Experimental] CUDA sparse LDLt factorization.
@ MINRES
Minimal Residual (MINRES) solver.
@ ExperimentalCudaSparseLU
[Experimental] CUDA sparse LU factorization.
@ ParallelCG
[Experimental] Parallel CG solver.
constexpr int kAutoLinearSolverMaxIter
Sentinel value for LinearSolverParams::maxIter that lets Mochi automatically select the maximum numbe...
@ Auto
Automatic collider type selection based on the actor's shape.
@ None
Invalid actor type.
Definition mochi_enums.h:26
@ Count
Number of actor type enum values.
Definition mochi_enums.h:38
#define MOCHI_ENUM_COUNT(name)
Definition reflection.h:275
#define MOCHI_ENUM_END()
Definition reflection.h:276
#define MOCHI_STRUCT_END()
Definition reflection.h:279
#define MOCHI_ENUM_BEGIN(name)
Definition reflection.h:273
#define MOCHI_FIELD(name)
Definition reflection.h:293
#define MOCHI_STRUCT_BEGIN(name)
Definition reflection.h:278
#define MOCHI_ENUM_ITEM(name)
Definition reflection.h:274
Parameters for the linear solve performed in each iteration of the non-linear solver.
real absTol
Absolute residual norm tolerance for convergence.
LinearSolverType solverType
Linear solver type.
VerbosityLevel verbosity
Verbosity level for logging output.
LinearSolverConvergenceNorm normType
Norm used for the stopping criteria.
bool abortIfNotSpd
Abort solve if matrix is not SPD.
PreconditionerType preconditionerType
Preconditioner type.
real relTol
Relative residual norm tolerance for convergence, relative to the initial residual.
real relDivTol
Relative residual norm tolerance for divergence, relative to the initial residual.
int maxIter
Maximum number of linear solver iterations.
int restartSize
Krylov subspace dimension before restarting.
bool operator==(LinearSolverParams const &) const =default