numerics
Loading...
Searching...
No Matches
policy.hpp
Go to the documentation of this file.
1/// @file policy.hpp
2/// @brief Backend enum for linear algebra operations.
3///
4/// Each module defines its own backend enum for the choices relevant to it.
5/// This enum covers linalg (vector, matrix, solvers, factorization, eigen, svd).
6/// Other modules define their own -- e.g. spectral/ uses FFTBackend.
7///
8/// num::seq -- naive serial C++ (always available)
9/// num::blocked -- cache-blocked, no intrinsics (compiler auto-vectorizes)
10/// num::simd -- hand-written SIMD (AVX2 on x86, NEON on AArch64)
11/// num::blas -- cblas / LAPACKE (optional, NUMERICS_HAS_BLAS)
12/// num::omp -- OpenMP parallel (optional, NUMERICS_HAS_OMP)
13/// num::gpu -- CUDA (optional, NUMERICS_HAS_CUDA)
14#pragma once
15
16namespace num {
17
18/// @brief Selects which backend handles a linalg operation.
19enum class Backend {
20 seq, ///< Naive textbook loops -- always available
21 blocked, ///< Cache-blocked; compiler auto-vectorizes inner loops
22 simd, ///< Hand-written SIMD intrinsics (AVX2 or NEON)
23 blas, ///< cblas/LAPACKE -- OpenBLAS, MKL, Apple Accelerate
24 omp, ///< OpenMP parallel blocked loops
25 gpu, ///< CUDA -- custom kernels or cuBLAS
26};
27
28// Convenience constants -- use these at call sites:
29// matmul(A, B, C, num::blas);
30inline constexpr Backend seq = Backend::seq;
31inline constexpr Backend blocked = Backend::blocked;
32inline constexpr Backend simd = Backend::simd;
33inline constexpr Backend blas = Backend::blas;
34inline constexpr Backend omp = Backend::omp;
35inline constexpr Backend gpu = Backend::gpu;
36
37// -- Compile-time capability flags --------------------------------------------
38/// True when a BLAS/cblas library was found at configure time.
39inline constexpr bool has_blas =
40#if defined(NUMERICS_HAS_BLAS)
41 true;
42#else
43 false;
44#endif
45
46/// True when OpenMP was found at configure time.
47inline constexpr bool has_omp =
48#if defined(NUMERICS_HAS_OMP)
49 true;
50#else
51 false;
52#endif
53
54// -- Default and best backends -------------------------------------------------
55/// Automatically selected at configure time: blas > blocked.
56/// Used as the default parameter for all operations.
57inline constexpr Backend default_backend =
58#if defined(NUMERICS_HAS_BLAS)
60#else
62#endif
63
64/// Best backend for memory-bound vector ops: blas > omp > blocked.
65inline constexpr Backend best_backend =
66#if defined(NUMERICS_HAS_BLAS)
68#elif defined(NUMERICS_HAS_OMP)
70#else
72#endif
73
74} // namespace num
constexpr Backend simd
Definition policy.hpp:32
Backend
Selects which backend handles a linalg operation.
Definition policy.hpp:19
@ gpu
CUDA – custom kernels or cuBLAS.
@ omp
OpenMP parallel blocked loops.
@ blocked
Cache-blocked; compiler auto-vectorizes inner loops.
@ simd
Hand-written SIMD intrinsics (AVX2 or NEON)
@ blas
cblas/LAPACKE – OpenBLAS, MKL, Apple Accelerate
@ seq
Naive textbook loops – always available.
constexpr Backend best_backend
Best backend for memory-bound vector ops: blas > omp > blocked.
Definition policy.hpp:65
constexpr Backend gpu
Definition policy.hpp:35
constexpr bool has_blas
True when a BLAS/cblas library was found at configure time.
Definition policy.hpp:39
constexpr Backend default_backend
Definition policy.hpp:57
constexpr bool has_omp
True when OpenMP was found at configure time.
Definition policy.hpp:47
constexpr Backend blas
Definition policy.hpp:33
constexpr Backend omp
Definition policy.hpp:34
constexpr Backend seq
Definition policy.hpp:30
constexpr Backend blocked
Definition policy.hpp:31