numerics 0.1.0
Loading...
Searching...
No Matches
thomas.cpp
Go to the documentation of this file.
1/// @file linalg/factorization/thomas.cpp
2/// @brief Thomas tridiagonal solver dispatcher.
3///
4/// Backend routing:
5/// Backend::lapack -> backends::lapack::thomas (LAPACKE_dgtsv with
6/// pivoting) Backend::gpu -> CUDA batched Thomas kernel everything else
7/// -> backends::seq::thomas (forward elimination + back sub)
8
11#include "backends/seq/impl.hpp"
12#include "backends/lapack/impl.hpp"
13#include <stdexcept>
14
15namespace num {
16
17void thomas(const Vector& a,
18 const Vector& b,
19 const Vector& c,
20 const Vector& d,
21 Vector& x,
22 Backend backend) {
23 idx n = b.size();
24 if (a.size() != n - 1 || c.size() != n - 1 || d.size() != n
25 || x.size() != n)
26 throw std::invalid_argument("Dimension mismatch in Thomas solver");
27
28 switch (backend) {
29 case Backend::lapack:
30 backends::lapack::thomas(a, b, c, d, x);
31 return;
32 case Backend::gpu:
33#ifdef NUMERICS_HAS_CUDA
34 {
35 Vector ag = a;
36 ag.to_gpu();
37 Vector bg = b;
38 bg.to_gpu();
39 Vector cg = c;
40 cg.to_gpu();
41 Vector dg = d;
42 dg.to_gpu();
43 x = Vector(n);
44 x.to_gpu();
46 bg.gpu_data(),
47 cg.gpu_data(),
48 dg.gpu_data(),
49 x.gpu_data(),
50 n,
51 1);
52 x.to_cpu();
53 return;
54 }
55#endif
56 [[fallthrough]];
57 default:
58 backends::seq::thomas(a, b, c, d, x);
59 return;
60 }
61}
62
63} // namespace num
real * gpu_data()
Definition vector.hpp:118
constexpr idx size() const noexcept
Definition vector.hpp:80
CUDA kernel wrappers.
void thomas(const Vector &a, const Vector &b, const Vector &c, const Vector &d, Vector &x)
Definition thomas.cpp:15
void thomas(const Vector &a, const Vector &b, const Vector &c, const Vector &d, Vector &x)
Definition thomas.cpp:8
void thomas_batched(const real *a, const real *b, const real *c, const real *d, real *x, idx n, idx batch_size)
Batched Thomas algorithm for tridiagonal systems.
Backend
Selects which backend handles a linalg operation.
Definition policy.hpp:19
@ gpu
CUDA – custom kernels or cuBLAS.
@ lapack
LAPACKE – industry-standard factorizations, SVD, eigen.
std::size_t idx
Definition types.hpp:11
void thomas(const Vector &a, const Vector &b, const Vector &c, const Vector &d, Vector &x, Backend backend=lapack_backend)
Thomas algorithm (LU for tridiagonal systems), O(n).
Definition thomas.cpp:17
BasicVector< real > Vector
Real-valued dense vector with full backend dispatch (CPU + GPU)
Definition vector.hpp:130
SolverResult cg(const Matrix &A, const Vector &b, Vector &x, real tol=1e-10, idx max_iter=1000, Backend backend=default_backend)
Conjugate gradient solver for Ax = b.
Definition cg.cpp:8
Thomas algorithm – direct O(n) tridiagonal solver.