DDC 0.10.0
Loading...
Searching...
No Matches
splines_linear_problem_maker.hpp
1// Copyright (C) The DDC development team, see COPYRIGHT.md file
2//
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
7#include <algorithm>
8#include <cassert>
9#include <cstddef>
10#include <memory>
11#include <optional>
12#include <utility>
13
14#include "splines_linear_problem.hpp"
15#include "splines_linear_problem_2x2_blocks.hpp"
16#include "splines_linear_problem_3x3_blocks.hpp"
17#include "splines_linear_problem_band.hpp"
18#include "splines_linear_problem_dense.hpp"
19#include "splines_linear_problem_pds_band.hpp"
20#include "splines_linear_problem_pds_tridiag.hpp"
21#include "splines_linear_problem_sparse.hpp"
22
23namespace ddc::detail {
24
25/**
26 * @brief A static factory for SplinesLinearProblem classes.
27 */
28class SplinesLinearProblemMaker
29{
30public:
31 /**
32 * @brief Construct a dense matrix
33 *
34 * @tparam the Kokkos::ExecutionSpace on which matrix-related operation will be performed.
35 * @param n The size of one of the dimensions of the square matrix.
36 *
37 * @return The SplinesLinearProblem instance.
38 */
39 template <typename ExecSpace>
40 static std::unique_ptr<SplinesLinearProblem<ExecSpace>> make_new_dense(int const n)
41 {
42 return std::make_unique<SplinesLinearProblemDense<ExecSpace>>(n);
43 }
44
45 /**
46 * @brief Construct a band matrix
47 *
48 * @tparam the Kokkos::ExecutionSpace on which matrix-related operation will be performed.
49 * @param n The size of one of the dimensions of the square matrix.
50 * @param kl The number of subdiagonals.
51 * @param ku The number of superdiagonals.
52 * @param pds A boolean indicating if the matrix is positive-definite symmetric or not.
53 *
54 * @return The SplinesLinearProblem instance.
55 */
56 template <typename ExecSpace>
57 static std::unique_ptr<SplinesLinearProblem<ExecSpace>> make_new_band(
58 int const n,
59 int const kl,
60 int const ku,
61 bool const pds)
62 {
63 if (kl == ku && kl == 1 && pds) {
64 return std::make_unique<SplinesLinearProblemPDSTridiag<ExecSpace>>(n);
65 }
66
67 if (kl == ku && pds) {
68 return std::make_unique<SplinesLinearProblemPDSBand<ExecSpace>>(n, kl);
69 }
70
71 if (2 * kl + ku + 1 >= n) {
72 return std::make_unique<SplinesLinearProblemDense<ExecSpace>>(n);
73 }
74
75 return std::make_unique<SplinesLinearProblemBand<ExecSpace>>(n, kl, ku);
76 }
77
78 /**
79 * @brief Construct a 2x2-blocks or 3x3-blocks linear problem with band "main" block (the one called
80 * Q in SplinesLinearProblem2x2Blocks and SplinesLinearProblem3x3Blocks).
81 *
82 * @tparam the Kokkos::ExecutionSpace on which matrix-related operation will be performed.
83 * @param n The size of one of the dimensions of the whole square matrix.
84 * @param kl The number of subdiagonals in the band block.
85 * @param ku The number of superdiagonals in the band block.
86 * @param pds A boolean indicating if the band block is positive-definite symmetric or not.
87 * @param bottom_right_size The size of one of the dimensions of the bottom-right square block.
88 * @param top_left_size The size of one of the dimensions of the top-left square block.
89 *
90 * @return The SplinesLinearProblem instance.
91 */
92 template <typename ExecSpace>
93 static std::unique_ptr<SplinesLinearProblem<ExecSpace>>
94 make_new_block_matrix_with_band_main_block(
95 int const n,
96 int const kl,
97 int const ku,
98 bool const pds,
99 int const bottom_right_size,
100 int const top_left_size = 0)
101 {
102 int const main_size = n - top_left_size - bottom_right_size;
103 std::unique_ptr<SplinesLinearProblem<ExecSpace>> main_block
104 = make_new_band<ExecSpace>(main_size, kl, ku, pds);
105 if (top_left_size == 0) {
106 return std::make_unique<
107 SplinesLinearProblem2x2Blocks<ExecSpace>>(n, std::move(main_block));
108 }
109 return std::make_unique<
110 SplinesLinearProblem3x3Blocks<ExecSpace>>(n, top_left_size, std::move(main_block));
111 }
112
113 /**
114 * @brief Construct a 2x2-blocks linear problem with band "main" block (the one called
115 * Q in SplinesLinearProblem2x2Blocks) and other blocks containing the "periodic parts" of
116 * a periodic band matrix.
117 *
118 * It simply calls make_new_block_matrix_with_band_main_block with bottom_size being
119 * max(kl, ku) (except if the allocation would be higher than instantiating a SplinesLinearProblemDense).
120 *
121 * @tparam the Kokkos::ExecutionSpace on which matrix-related operation will be performed.
122 * @param n The size of one of the dimensions of the whole square matrix.
123 * @param kl The number of subdiagonals in the band block.
124 * @param ku The number of superdiagonals in the band block.
125 * @param pds A boolean indicating if the band block is positive-definite symmetric or not.
126 *
127 * @return The SplinesLinearProblem instance.
128 */
129 template <typename ExecSpace>
130 static std::unique_ptr<SplinesLinearProblem<ExecSpace>> make_new_periodic_band_matrix(
131 int const n,
132 int const kl,
133 int const ku,
134 bool const pds)
135 {
136 assert(kl < n);
137 assert(ku < n);
138 int const bottom_size = std::max(kl, ku);
139 int const top_size = n - bottom_size;
140
141 if (bottom_size * (n + top_size) + (2 * kl + ku + 1) * top_size >= n * n) {
142 return std::make_unique<SplinesLinearProblemDense<ExecSpace>>(n);
143 }
144
145 return make_new_block_matrix_with_band_main_block<ExecSpace>(n, kl, ku, pds, bottom_size);
146 }
147
148 /**
149 * @brief Construct a sparse matrix
150 *
151 * @tparam the Kokkos::ExecutionSpace on which matrix-related operation will be performed.
152 * @param n The size of one of the dimensions of the square matrix.
153 * @param cols_per_chunk A parameter used by the slicer (internal to the solver) to define the size
154 * of a chunk of right-hand sides of the linear problem to be computed in parallel (chunks are treated
155 * by the linear solver one-after-the-other).
156 * This value is optional. If no value is provided then the default value is chosen by the requested solver.
157 *
158 * @param preconditioner_max_block_size A parameter used by the slicer (internal to the solver) to
159 * define the size of a block used by the Block-Jacobi preconditioner.
160 * This value is optional. If no value is provided then the default value is chosen by the requested solver.
161 *
162 * @return The SplinesLinearProblem instance.
163 */
164 template <typename ExecSpace>
165 static std::unique_ptr<SplinesLinearProblem<ExecSpace>> make_new_sparse(
166 int const n,
167 std::optional<std::size_t> cols_per_chunk = std::nullopt,
168 std::optional<unsigned int> preconditioner_max_block_size = std::nullopt)
169 {
170 return std::make_unique<SplinesLinearProblemSparse<
171 ExecSpace>>(n, cols_per_chunk, preconditioner_max_block_size);
172 }
173};
174
175} // namespace ddc::detail
The top-level namespace of DDC.