batmat 0.0.18
Batched linear algebra routines
Loading...
Searching...
No Matches
example.cpp
Basic example demonstrating how to use batmat to perform Cholesky factorization of a batch of matrices.
#include <batmat-version.h>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <limits>
#include <print>
#include <random>
using batmat::index_t;
using batmat::real_t;
namespace la = batmat::linalg;
int main() {
std::println("{:+<79}", "");
std::println("batmat {} ({}:{})", BATMAT_VERSION, batmat_commit_hash, batmat_build_time);
std::println("{:+<79}", "");
std::println("Index type: {}", guanaqo::demangled_typename(typeid(batmat::index_t)));
std::println("Default dtype: {}", guanaqo::demangled_typename(typeid(batmat::real_t)));
std::println("Supported dtypes and vector lengths:");
std::println(" - {} [{}]", guanaqo::demangled_typename(typeid(typename T::dtype)), T::vl);
});
std::println("{:+<79}", "");
// Select an appropriate vector length.
static_assert(v != 0, "No suitable vector length for real_t");
using batch_size = std::integral_constant<index_t, 4>;
constexpr auto storage_order = batmat::matrix::StorageOrder::ColMajor;
// Class representing a batch of four matrices.
// Allocate some batches of matrices (initialized to zero).
index_t n = 3, m = n + 5;
Mat C{{.rows = n, .cols = n}}, A{{.rows = n, .cols = m}};
// Fill A with random values.
std::mt19937 rng{12345};
std::uniform_real_distribution<real_t> uni{-1.0, 1.0};
std::ranges::generate(A, [&] { return uni(rng); });
// Compute C = AAᵀ to make it symmetric positive definite (lower triangular part only).
// Allocate L for the Cholesky factors.
Mat L{{.rows = n, .cols = n}, batmat::matrix::uninitialized};
// Compute the Cholesky factors L of C (lower triangular).
// Print the results.
for (index_t l = 0; l < C.depth(); ++l) {
guanaqo::print_python(std::cout << "C[" << l << "] =\n", C(l));
guanaqo::print_python(std::cout << "L[" << l << "] =\n", L(l));
}
// Compute LLᵀ (in-place).
// Check that LLᵀ == C.
int errors = 0;
const auto eps = std::numeric_limits<real_t>::epsilon();
for (index_t l = 0; l < C.depth(); ++l)
for (index_t c = 0; c < C.cols(); ++c)
for (index_t r = c; r < C.rows(); ++r)
errors += std::abs(C(l, r, c) - L(l, r, c)) < 10 * eps ? 0 : 1;
return errors;
}
int main()
Definition example.cpp:20
std::string demangled_typename(const std::type_info &t)
std::ostream & print_python(std::ostream &os, std::span< T, E > x, std::string_view end="\n", bool squeeze=true)
void syrk(Structured< VA, SA > A, Structured< VD, SD > D, Opts... opts)
D = A Aᵀ with D symmetric.
Definition gemm.hpp:310
void potrf(Structured< VC, SC > C, Structured< VD, SC > D, simdified_value_t< VC > regularization=0)
D = chol(C) with C symmetric, D triangular.
Definition potrf.hpp:97
void fill(simdified_value_t< VB > a, VB &&B)
B = a.
Definition copy.hpp:204
constexpr auto triu(M &&m)
Upper-triangular view.
constexpr auto tril(M &&m)
Lower-triangular view.
struct batmat::matrix::uninitialized_t uninitialized
Tag type to indicate that memory should not be initialized.
Class for a batch of matrices that owns its storage.
constexpr auto foreach_dtype_vl(F &&f)
Call a given function f for all supported (dtype, VL) combinations.
Definition dtypes.hpp:177
constexpr index_t vl_at_most
The largest supported vector length for dtype DT that is less than or equal to VL.
Definition dtypes.hpp:148
Owning array of matrices, stored in an efficient batched format.
Definition matrix.hpp:59