|
| 1 | +// This file is part of https://github.com/KurtBoehm/linealia. |
| 2 | +// |
| 3 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 4 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 | +// file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 6 | + |
| 7 | +#ifndef INCLUDE_LINEALIA_LINEALIA_HPP |
| 8 | +#define INCLUDE_LINEALIA_LINEALIA_HPP |
| 9 | + |
| 10 | +#include <cstddef> |
| 11 | +#include <cstdint> |
| 12 | + |
| 13 | +extern "C" { |
| 14 | + |
| 15 | +enum LinealiaIterativeStopReason { |
| 16 | + LINEALIA_STOP_RESIDUAL_NORM, |
| 17 | + LINEALIA_STOP_ITERATIONS, |
| 18 | +}; |
| 19 | + |
| 20 | +struct LinealiaIterativeResult { |
| 21 | + LinealiaIterativeStopReason reason; |
| 22 | + size_t iteration; |
| 23 | + // ||Ax−b||₂ |
| 24 | + double residual_norm; |
| 25 | + // ||Ax−b||₂/||b||₂ |
| 26 | + double relative_residual_norm; |
| 27 | +}; |
| 28 | + |
| 29 | +struct LinealiaMatrix { |
| 30 | + uint32_t num_rows = 0; |
| 31 | + uint8_t max_columns = 11; |
| 32 | + uint8_t* num_columns = nullptr; |
| 33 | + uint32_t** column_indices = nullptr; |
| 34 | + double** values = nullptr; |
| 35 | +}; |
| 36 | + |
| 37 | +struct LinealiaVector { |
| 38 | + uint32_t num_elements = 0; |
| 39 | + double* values = nullptr; |
| 40 | +}; |
| 41 | + |
| 42 | +struct LinealExecutionParams { |
| 43 | + // The number of threads to use |
| 44 | + // If the value is 0 (the default), the number of threads is determined automatically |
| 45 | + size_t num_threads = 0; |
| 46 | +}; |
| 47 | + |
| 48 | +struct LinealiaIterativeSolverParams { |
| 49 | + // Maximum number of iterations |
| 50 | + size_t max_iterations = 256; |
| 51 | + // Maximum relative residual norm, i.e. ||Ax−b||₂/||b||₂ |
| 52 | + double max_relative_residual_norm = 1e-10; |
| 53 | +}; |
| 54 | + |
| 55 | +struct LinealiaRelaxedParams { |
| 56 | + // The relaxation factor, commonly called ω (1 by default, i.e. SOR→Gauß-Seidel) |
| 57 | + double relax = 1.0; |
| 58 | +}; |
| 59 | + |
| 60 | +LinealiaIterativeResult linealia_solve_sor(LinealiaMatrix lhs, LinealiaVector sol, |
| 61 | + LinealiaVector rhs, LinealExecutionParams eparams, |
| 62 | + LinealiaIterativeSolverParams iparams, |
| 63 | + LinealiaRelaxedParams sparams); |
| 64 | +LinealiaIterativeResult linealia_solve_ssor(LinealiaMatrix lhs, LinealiaVector sol, |
| 65 | + LinealiaVector rhs, LinealExecutionParams eparams, |
| 66 | + LinealiaIterativeSolverParams iparams, |
| 67 | + LinealiaRelaxedParams sparams); |
| 68 | + |
| 69 | +LinealiaIterativeResult linealia_solve_cg(LinealiaMatrix lhs, LinealiaVector sol, |
| 70 | + LinealiaVector rhs, LinealExecutionParams eparams, |
| 71 | + LinealiaIterativeSolverParams iparams); |
| 72 | + |
| 73 | +struct LinealiaRelaxedPreconditionerParams { |
| 74 | + // The relaxation factor, commonly called ω (1 by default, i.e. Gauß-Seidel) |
| 75 | + double relax = 1.0; |
| 76 | + // The number of SOR iterations to apply per CG iteration |
| 77 | + size_t iterations = 2; |
| 78 | +}; |
| 79 | + |
| 80 | +LinealiaIterativeResult linealia_solve_pcg_sor(LinealiaMatrix lhs, LinealiaVector sol, LinealiaVector rhs, |
| 81 | + LinealExecutionParams eparams, LinealiaIterativeSolverParams iparams, |
| 82 | + LinealiaRelaxedPreconditionerParams preconditioner_params); |
| 83 | + |
| 84 | +LinealiaIterativeResult linealia_solve_pcg_ssor(LinealiaMatrix lhs, LinealiaVector sol, LinealiaVector rhs, |
| 85 | + LinealExecutionParams eparams, LinealiaIterativeSolverParams iparams, |
| 86 | + LinealiaRelaxedPreconditionerParams preconditioner_params); |
| 87 | + |
| 88 | +struct AmgAggregateSizeRange { |
| 89 | + size_t min; |
| 90 | + size_t max; |
| 91 | +}; |
| 92 | + |
| 93 | +struct LinealiaPcgAmgParams { |
| 94 | + // The AMG relaxation factor |
| 95 | + double amg_relax = 0.6; |
| 96 | + // The aggregate size ranges |
| 97 | + // If these are null/0, uses [8, 12] on the finest level and [6, 9] on other levels |
| 98 | + AmgAggregateSizeRange* amg_aggregate_size_ranges = nullptr; |
| 99 | + size_t amg_aggregate_size_ranges_size = 0; |
| 100 | + // The maximum number of levels in the AMG hierarchy |
| 101 | + size_t amg_max_level_num = 64; |
| 102 | + // A linear system must have at least this size to continue coarsening |
| 103 | + uint32_t amg_min_coarsen_size = 1024; |
| 104 | + // The decrease in the size of the linear system must be at least this factor |
| 105 | + // to continue coarsening |
| 106 | + double amg_min_coarsen_factor = 1.2; |
| 107 | + // The minimum number of unknowns per thread |
| 108 | + // If there are fewer, the number of threads is reduced accordingly |
| 109 | + double amg_aggregation_min_per_thread = 32768; |
| 110 | + // The SOR smoother relaxation factor, commonly called ω (1 by default) |
| 111 | + double smoother_relax = 1.0; |
| 112 | + // The number of SOR smoother iterations to apply |
| 113 | + size_t smoother_iterations = 2; |
| 114 | +}; |
| 115 | + |
| 116 | +LinealiaIterativeResult linealia_solve_pcg_amg_sor(LinealiaMatrix lhs, LinealiaVector sol, |
| 117 | + LinealiaVector rhs, |
| 118 | + LinealExecutionParams eparams, |
| 119 | + LinealiaIterativeSolverParams iparams, |
| 120 | + LinealiaPcgAmgParams sparams); |
| 121 | +LinealiaIterativeResult linealia_solve_pcg_amg_ssor(LinealiaMatrix lhs, LinealiaVector sol, |
| 122 | + LinealiaVector rhs, |
| 123 | + LinealExecutionParams eparams, |
| 124 | + LinealiaIterativeSolverParams iparams, |
| 125 | + LinealiaPcgAmgParams sparams); |
| 126 | +} |
| 127 | + |
| 128 | +#endif // INCLUDE_LINEALIA_LINEALIA_HPP |
0 commit comments