numerics 0.1.0
Loading...
Searching...
No Matches
reduce.cpp
Go to the documentation of this file.
1/// @file kernel/reduce.cpp
2/// @brief Implementations for num::kernel::reduce (seq_t and par_t overloads).
3///
4/// seq_t paths delegate to raw::, which internally calls cblas_dasum /
5/// cblas_idamax when NUMERICS_HAS_BLAS is defined.
6///
7/// par_t paths use OpenMP reductions. When NUMERICS_HAS_OMP is not defined
8/// par_t falls through to the seq_t implementation.
9
10#include "kernel/reduce.hpp"
11#include "kernel/raw.hpp"
12#include <cmath>
13
14namespace num::kernel::reduce {
15
16// ---------------------------------------------------------------------------
17// l1_norm
18// ---------------------------------------------------------------------------
19
20real l1_norm(const Vector& x, seq_t) noexcept {
21 return raw::l1_norm(x.data(), x.size());
22}
23
25#ifdef NUMERICS_HAS_OMP
26 const idx n = x.size();
27 const real* xd = x.data();
28 real s = real(0);
29 #pragma omp parallel for reduction(+:s) schedule(static)
30 for (idx i = 0; i < n; ++i) {
31 s += std::abs(xd[i]);
32 }
33 return s;
34#else
35 return l1_norm(x, seq_t{});
36#endif
37}
38
39// ---------------------------------------------------------------------------
40// linf_norm
41// ---------------------------------------------------------------------------
42
43real linf_norm(const Vector& x, seq_t) noexcept {
44 return raw::linf_norm(x.data(), x.size());
45}
46
48#ifdef NUMERICS_HAS_OMP
49 const idx n = x.size();
50 const real* xd = x.data();
51 real mx = real(0);
52 #pragma omp parallel for reduction(max:mx) schedule(static)
53 for (idx i = 0; i < n; ++i) {
54 const real v = std::abs(xd[i]);
55 if (v > mx) {
56 mx = v;
57 }
58 }
59 return mx;
60#else
61 return linf_norm(x, seq_t{});
62#endif
63}
64
65// ---------------------------------------------------------------------------
66// sum
67// ---------------------------------------------------------------------------
68
69real sum(const Vector& x, seq_t) noexcept {
70 return raw::sum(x.data(), x.size());
71}
72
73real sum(const Vector& x, par_t) {
74#ifdef NUMERICS_HAS_OMP
75 const idx n = x.size();
76 const real* xd = x.data();
77 real s = real(0);
78 #pragma omp parallel for reduction(+:s) schedule(static)
79 for (idx i = 0; i < n; ++i) {
80 s += xd[i];
81 }
82 return s;
83#else
84 return sum(x, seq_t{});
85#endif
86}
87
88} // namespace num::kernel::reduce
constexpr idx size() const noexcept
Definition vector.hpp:80
NUM_K_AINLINE real l1_norm(const real *NUM_K_RESTRICT x, idx n) noexcept
L1 norm: sum |x[i]|.
Definition raw.hpp:174
NUM_K_AINLINE real linf_norm(const real *NUM_K_RESTRICT x, idx n) noexcept
L-infinity norm: max |x[i]|.
Definition raw.hpp:189
NUM_K_AINLINE real sum(const real *NUM_K_RESTRICT x, idx n) noexcept
Scalar sum: return sum x[i].
Definition raw.hpp:210
real l1_norm(const Vector &x, seq_t) noexcept
Sequential: calls raw::l1_norm (routes to cblas_dasum when BLAS available; otherwise auto-vectorizabl...
Definition reduce.cpp:20
real sum(const Vector &x, seq_t) noexcept
Sequential: auto-vectorizable summation loop (no BLAS equivalent).
Definition reduce.cpp:69
real linf_norm(const Vector &x, seq_t) noexcept
Sequential: calls raw::linf_norm (routes to cblas_idamax when BLAS available; otherwise plain max loo...
Definition reduce.cpp:43
double real
Definition types.hpp:10
std::size_t idx
Definition types.hpp:11
Tier-1 kernel: raw-pointer, inline, zero-overhead inner loops.
Scalar reduction kernels (namespace num::kernel::reduce)
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