numerics
Loading...
Searching...
No Matches
thomas.cpp
Go to the documentation of this file.
3#include <stdexcept>
4
5namespace num {
6
7void thomas(const Vector& a, const Vector& b, const Vector& c,
8 const Vector& d, Vector& x, Backend backend) {
9 idx n = b.size();
10 if (a.size() != n - 1 || c.size() != n - 1 || d.size() != n || x.size() != n)
11 throw std::invalid_argument("Dimension mismatch in Thomas solver");
12
13 if (backend == Backend::gpu) {
14#ifdef NUMERICS_HAS_CUDA
15 Vector ag = a; ag.to_gpu();
16 Vector bg = b; bg.to_gpu();
17 Vector cg = c; cg.to_gpu();
18 Vector dg = d; dg.to_gpu();
19 x = Vector(n);
20 x.to_gpu();
21 cuda::thomas_batched(ag.gpu_data(), bg.gpu_data(), cg.gpu_data(),
22 dg.gpu_data(), x.gpu_data(), n, 1);
23 x.to_cpu();
24 return;
25#endif
26 }
27
28 Vector b_work = b;
29 Vector d_work = d;
30
31 for (idx i = 1; i < n; ++i) {
32 real w = a[i - 1] / b_work[i - 1];
33 b_work[i] -= w * c[i - 1];
34 d_work[i] -= w * d_work[i - 1];
35 }
36
37 x[n - 1] = d_work[n - 1] / b_work[n - 1];
38 for (idx i = n - 1; i > 0; --i)
39 x[i - 1] = (d_work[i - 1] - c[i - 1] * x[i]) / b_work[i - 1];
40}
41
42} // namespace num
real * gpu_data()
Definition vector.hpp:111
constexpr idx size() const noexcept
Definition vector.hpp:77
CUDA kernel wrappers.
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.
double real
Definition types.hpp:10
Backend
Selects which backend handles a linalg operation.
Definition policy.hpp:19
@ gpu
CUDA – custom kernels or cuBLAS.
constexpr T ipow(T x) noexcept
Compute x^N at compile time via repeated squaring.
std::size_t idx
Definition types.hpp:11
BasicVector< real > Vector
Real-valued dense vector with full backend dispatch (CPU + GPU)
Definition vector.hpp:122
void thomas(const Vector &a, const Vector &b, const Vector &c, const Vector &d, Vector &x, Backend backend=Backend::seq)
Thomas algorithm (LU for tridiagonal systems), O(n).
Definition thomas.cpp:7
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.