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 By default, an appropriate preconditioner type is selected 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 /**
288 * @brief [Experimental] @ref PreconditionerType::PerActor preconditioner that can switch the
289 * preconditioner type of each actor during the simulation to speed up the linear solves.
290 *
291 * @warning Some scenes can run slower than with @ref PreconditionerType::PerActor, and some steps
292 * can take longer than usual, causing latency spikes.
293 * @warning This is an experimental feature. It may be changed or removed in the future. Use at
294 * your own risk.
295 */
297
298 /** @brief Number of preconditioner type enum values. */
300
301 /** @brief Default preconditioner type. */
303};
304} // namespace superdex
305
307MOCHI_ENUM_ITEM(None)
308MOCHI_ENUM_ITEM(Jacobi)
309MOCHI_ENUM_ITEM(BlockJacobi)
310MOCHI_ENUM_ITEM(SSOR)
311MOCHI_ENUM_ITEM(BlockSSOR)
314MOCHI_ENUM_ITEM(SymInverse)
315MOCHI_ENUM_ITEM(LDLT)
316MOCHI_ENUM_ITEM(ILU0)
318MOCHI_ENUM_ITEM(PerActor)
319MOCHI_ENUM_ITEM(ColoredSSOR)
320MOCHI_ENUM_ITEM(AdaptivePerActor)
321MOCHI_ENUM_COUNT(Count)
323
324namespace superdex {
325/**
326 * @brief Norm types for the stopping criteria of the linear solver.
327 *
328 * @note @ref LinearSolverType::CG, @ref LinearSolverType::ParallelCG and @ref
329 * LinearSolverType::AsyncCG support every norm. @ref LinearSolverType::CudaCG supports every norm
330 * except @ref LinearSolverConvergenceNorm::ResidualPerActorWeighted, for which it uses @ref
331 * LinearSolverConvergenceNorm::PreconditionedResidualL2. @ref LinearSolverType::GMRES, @ref
332 * LinearSolverType::CudaGMRES, and @ref LinearSolverType::AugmentedCG always use the L2-norm of the
333 * residual regardless of this setting. @ref LinearSolverType::MINRES always uses the
334 * preconditioner-induced norm of the residual. Direct solvers do not have stop criteria.
335 */
337 ResidualL2, ///< L2-norm of the residual: \f$\|r\|_2\f$.
338 PreconditionedResidualL2, ///< L2-norm of the preconditioned residual: \f$\|z\|_2\f$ where
339 ///< \f$z = M^{-1}r\f$.
340 ResidualPreconditionerInduced, ///< Preconditioner-induced norm of the residual:
341 ///< \f$\sqrt{\langle r, z \rangle}\f$ where \f$z = M^{-1}r\f$.
342 /**
343 * @brief Per-actor weighted L2-norm of the residual, each actor normalized by its own non-linear
344 * residual, so actors have comparable influence regardless of the magnitude of their residuals.
345 *
346 * @details \f$\|r\| = \sqrt{\sum_a \|r_a\|_W^2 / s_a^2}\f$, where
347 * \f$\|r_a\|_W = \sqrt{\sum_i w_i r_i^2}\f$ uses actor \f$a\f$'s weights from @ref
348 * NonLinearSolverConvergenceMode::PerActorWeighted, and \f$s_a\f$ is the larger of actor
349 * \f$a\f$'s non-linear residual in that norm and @ref NonLinearSolverParams::absTol, or, if both
350 * are zero, the largest actor's residual.
351 *
352 * @note In @ref NonLinearSolverConvergenceMode::Global mode, the solvers that support it use @ref
353 * LinearSolverConvergenceNorm::ResidualL2 instead.
354 */
356 Count, ///< Number of linear solver convergence norm enum values.
357 Default = PreconditionedResidualL2 ///< Default linear solver convergence norm.
358};
359} // namespace superdex
360
362MOCHI_ENUM_ITEM(ResidualL2)
363MOCHI_ENUM_ITEM(PreconditionedResidualL2)
364MOCHI_ENUM_ITEM(ResidualPreconditionerInduced)
365MOCHI_ENUM_ITEM(ResidualPerActorWeighted)
366MOCHI_ENUM_COUNT(Count)
368
369namespace superdex {
370
371/** @brief Sentinel value for @ref LinearSolverParams::maxIter that lets Mochi automatically select
372 * the maximum number of linear solver iterations. */
373constexpr int kAutoLinearSolverMaxIter = -1;
374
375/** @brief Parameters for the linear solve performed in each iteration of the non-linear solver. */
377 /** @brief Linear solver type. */
379
380 /**
381 * @brief Preconditioner type.
382 *
383 * @note Applies only to iterative solvers.
384 */
386
387 /**
388 * @brief Norm used for the stopping criteria.
389 *
390 * @note Applies only to the solvers documented in @ref LinearSolverConvergenceNorm.
391 */
393
394 /**
395 * @brief Absolute residual norm tolerance for convergence.
396 *
397 * @note Applies only to iterative solvers.
398 * @note When the solver stops on @ref LinearSolverConvergenceNorm::ResidualPerActorWeighted, the
399 * absolute stop ends the solve only once every actor's weighted residual \f$\|r_a\|_W\f$ is at
400 * most @ref absTol. Otherwise, it bounds the solver's stopping norm.
401 * @note 0 leaves only the relative test.
402 * @note A negative value lets Mochi select it. When the solver stops on @ref
403 * LinearSolverConvergenceNorm::ResidualPerActorWeighted, Mochi selects 1% of the residual at
404 * which the non-linear solver accepts each actor: @ref NonLinearSolverParams::relTol times the
405 * actor's initial residual, or @ref NonLinearSolverParams::absTol if larger. When it stops on
406 * @ref LinearSolverConvergenceNorm::ResidualL2 in @ref NonLinearSolverConvergenceMode::Global
407 * mode, Mochi selects 1% of the residual at which the non-linear solver accepts the island.
408 * Otherwise, it selects 1e-9. The selection is 0 when @ref NonLinearSolverParams::absTol is 0 and
409 * either @ref NonLinearSolverParams::relTol or the initial residual of an actor or the island is
410 * 0, which leaves only the relative test.
411 */
412 real absTol = 1e-9_r;
413
414 /**
415 * @brief Relative residual norm tolerance for convergence, relative to the same norm of the
416 * right-hand side.
417 *
418 * @note Applies only to iterative solvers.
419 * @note Ignored when using @ref LinearToleranceStrategy::EisenstatWalker1 or @ref
420 * LinearToleranceStrategy::EisenstatWalker2 in the non-linear solver.
421 */
422 real relTol = 1e-5_r;
423
424 /**
425 * @brief Relative residual norm tolerance for divergence, relative to the same norm of the
426 * right-hand side.
427 *
428 * @note Applies only to iterative solvers.
429 */
430 real relDivTol = 1e10_r;
431
432 /**
433 * @brief Maximum number of iterations for iterative solvers.
434 *
435 * @note Applies only to iterative solvers.
436 * @note Must be positive or @ref kAutoLinearSolverMaxIter.
437 * @note @ref kAutoLinearSolverMaxIter lets Mochi select the maximum number of iterations based
438 * on the problem.
439 */
441
442 /**
443 * @brief Krylov subspace dimension before restarting.
444 *
445 * @note Applies only to GMRES solvers (e.g., @ref LinearSolverType::GMRES, @ref
446 * LinearSolverType::CudaGMRES).
447 */
448 int restartSize = 1000;
449
450 /**
451 * @brief Abort solve if matrix is not SPD.
452 *
453 * @note Applies only to iterative SPD solvers (e.g., @ref LinearSolverType::CG, @ref
454 * LinearSolverType::CudaCG, @ref LinearSolverType::AugmentedCG).
455 */
456 bool abortIfNotSpd = false;
457
458 /** @brief Verbosity level for logging output. */
460
461#if MOCHI_LANGUAGE_CPP20
462 bool operator==(LinearSolverParams const&) const = default;
463#endif
464
477};
478
479} // 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.
@ AdaptivePerActor
[Experimental] PreconditionerType::PerActor preconditioner that can switch the preconditioner type of...
@ 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.
@ ResidualPerActorWeighted
Per-actor weighted L2-norm of the residual, each actor normalized by its own non-linear residual,...
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:329
#define MOCHI_ENUM_END()
Definition reflection.h:330
#define MOCHI_STRUCT_END()
Definition reflection.h:333
#define MOCHI_ENUM_BEGIN(name)
Definition reflection.h:327
#define MOCHI_FIELD(name)
Definition reflection.h:351
#define MOCHI_STRUCT_BEGIN(name)
Definition reflection.h:332
#define MOCHI_ENUM_ITEM(name)
Definition reflection.h:328
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 same norm of the right-hand side.
real relDivTol
Relative residual norm tolerance for divergence, relative to the same norm of the right-hand side.
int maxIter
Maximum number of iterations for iterative solvers.
int restartSize
Krylov subspace dimension before restarting.
bool operator==(LinearSolverParams const &) const =default