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#include "kernel/reduce.hpp"
5#include "kernel/raw.hpp"
6#include <cmath>
7
8namespace num::kernel::reduce {
9
10real l1_norm(const Vector& x, seq_t) noexcept {
11 return raw::l1_norm(x.data(), x.size());
12}
13
15#ifdef NUMERICS_HAS_OMP
16 const idx n = x.size();
17 const real* xd = x.data();
18 real s = real(0);
19 #pragma omp parallel for reduction(+ : s) schedule(static)
20 for (idx i = 0; i < n; ++i) {
21 s += std::abs(xd[i]);
22 }
23 return s;
24#else
25 return l1_norm(x, seq_t{});
26#endif
27}
28
29real linf_norm(const Vector& x, seq_t) noexcept {
30 return raw::linf_norm(x.data(), x.size());
31}
32
34#ifdef NUMERICS_HAS_OMP
35 const idx n = x.size();
36 const real* xd = x.data();
37 real mx = real(0);
38 #pragma omp parallel for reduction(max : mx) schedule(static)
39 for (idx i = 0; i < n; ++i) {
40 const real v = std::abs(xd[i]);
41 if (v > mx) {
42 mx = v;
43 }
44 }
45 return mx;
46#else
47 return linf_norm(x, seq_t{});
48#endif
49}
50
51real sum(const Vector& x, seq_t) noexcept {
52 return raw::sum(x.data(), x.size());
53}
54
55real sum(const Vector& x, par_t) {
56#ifdef NUMERICS_HAS_OMP
57 const idx n = x.size();
58 const real* xd = x.data();
59 real s = real(0);
60 #pragma omp parallel for reduction(+ : s) schedule(static)
61 for (idx i = 0; i < n; ++i) {
62 s += xd[i];
63 }
64 return s;
65#else
66 return sum(x, seq_t{});
67#endif
68}
69
70} // namespace num::kernel::reduce
constexpr idx size() const noexcept
Definition vector.hpp:83
NUM_K_AINLINE T sum(const T *NUM_K_RESTRICT x, idx n) noexcept
Scalar sum: return sum x[i].
Definition raw.hpp:127
NUM_K_AINLINE T linf_norm(const T *NUM_K_RESTRICT x, idx n) noexcept
L-infinity norm: max |x[i]|.
Definition raw.hpp:114
NUM_K_AINLINE T l1_norm(const T *NUM_K_RESTRICT x, idx n) noexcept
L1 norm: sum |x[i]|.
Definition raw.hpp:103
real l1_norm(const Vector &x, seq_t) noexcept
Sequential l1 norm.
Definition reduce.cpp:10
real sum(const Vector &x, seq_t) noexcept
Sequential sum.
Definition reduce.cpp:51
real linf_norm(const Vector &x, seq_t) noexcept
Sequential infinity norm.
Definition reduce.cpp:29
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.
Definition policy.hpp:13
Sequential execution policy tag.
Definition policy.hpp:10