numerics 0.1.0
Loading...
Searching...
No Matches
policy.hpp
Go to the documentation of this file.
1/// @file kernel/policy.hpp
2/// @brief Compile-time dispatch policy tags for the kernel module.
3///
4/// The kernel layer has one relevant dimension of variation: parallelism.
5/// BLAS dispatch is already handled inside raw:: for ops that have cblas
6/// equivalents. Vectorization (SIMD) is handled by the compiler or by raw::.
7/// The only thing the caller needs to choose is: run sequentially or in
8/// parallel (OMP).
9///
10/// Two tags:
11/// seq_t -- always sequential; safe to call from inside an OMP region
12/// par_t -- OpenMP parallel; falls through to seq_t if OMP not available
13///
14/// Default policy (kdefault / default_policy):
15/// par_t when NUMERICS_HAS_OMP is defined
16/// seq_t otherwise
17///
18/// Usage:
19/// num::kernel::array::axpby(a, x, b, y); // default policy
20/// num::kernel::array::axpby(a, x, b, y, num::kernel::kseq); // force seq
21/// num::kernel::array::axpby(a, x, b, y, num::kernel::kpar); // force par
22///
23/// For templates that cascade policy through multiple kernel calls:
24/// template<typename Policy = num::kernel::default_policy>
25/// void my_kernel(..., Policy p = {}) {
26/// num::kernel::reduce::l1_norm(x, p);
27/// num::kernel::array::axpby(a, x, b, y, p);
28/// }
29#pragma once
30
31#include "core/policy.hpp" // has_omp
32
33namespace num::kernel {
34
35/// @brief Sequential execution policy tag.
36/// Guarantees no OMP parallel regions; safe to call inside an existing
37/// parallel region without causing nested-parallelism overhead.
38struct seq_t {};
39
40/// @brief Parallel execution policy tag.
41/// Activates OMP parallel-for / reduction constructs when NUMERICS_HAS_OMP.
42/// Falls through to seq_t behaviour when OMP is not available.
43struct par_t {};
44
45inline constexpr seq_t kseq{};
46inline constexpr par_t kpar{};
47
48/// @brief Default policy: par_t if OMP is available, seq_t otherwise.
49/// Selected at configure time; zero runtime overhead.
50#if defined(NUMERICS_HAS_OMP)
51using default_policy = par_t;
52#else
54#endif
55
56inline constexpr default_policy kdefault{};
57
58} // namespace num::kernel
Backend enum for linear algebra operations.
constexpr default_policy kdefault
Definition policy.hpp:56
constexpr par_t kpar
Definition policy.hpp:46
constexpr seq_t kseq
Definition policy.hpp:45
seq_t default_policy
Default policy: par_t if OMP is available, seq_t otherwise. Selected at configure time; zero runtime ...
Definition policy.hpp:53
Parallel execution policy tag. Activates OMP parallel-for / reduction constructs when NUMERICS_HAS_OM...
Definition policy.hpp:43
Sequential execution policy tag. Guarantees no OMP parallel regions; safe to call inside an existing ...
Definition policy.hpp:38