PACMAN 0.1.0
Portable Algorithms for Coupling, Mapping, and Adaptive iNterpolation
Loading...
Searching...
No Matches
serial_ops.hpp
Go to the documentation of this file.
1//
2// This file is subject to the terms and conditions defined in
3// file 'LICENSE', which is part of this source code package.
4//
5
6#pragma once
7
8#include <KokkosBlas2_gemv.hpp>
9#include <Kokkos_Core.hpp>
10
11#include "common/concepts.hpp"
12#include "common/types.hpp"
14
15namespace PACMAN {
16namespace RbfPum {
17
28template <KokkosViewRank<1> SourceType, KokkosViewRank<1> OffsType,
29 KokkosArray AxisType, int_t dim = AxisType::size()>
31 const OffsType &offs,
34 Kokkos::Array<fp_t, dim> distances;
35 for (int_t axis = 0; axis < dim; ++axis) {
36 if (!activeAxis[axis]) {
38 continue;
39 }
40 auto min_max_pair = RbfPumMinMax(offs, [=](const int &i, const int &j) {
41 return src(i)[axis] < src(j)[axis];
42 });
43 const fp_t dist = Kokkos::abs(src(min_max_pair.second)[axis] -
44 src(min_max_pair.first)[axis]);
46 }
47 fp_t min = fp_consts::max();
48 int_t min_i = 0;
49 for (int_t axis = 0; axis < dim; ++axis) {
50 if (distances[axis] < min) {
51 min = distances[axis];
52 min_i = axis;
53 }
54 }
55 activeAxis[min_i] = false;
56 return min_i;
57}
58
70template <KokkosViewRank<2> PViewType, KokkosViewRank<1> XViewType,
71 KokkosViewRank<1> OffsViewType, KokkosArray AxisType,
72 int_t dim = AxisType::size()>
74 const OffsViewType &XOffs,
76 const int_t N = P.extent_int(0);
79 for (int_t j = 0; j < dim; ++j) {
80 if (activeAxis[j]) {
82 }
83 }
84 for (int_t i = 0; i < N; ++i) {
85 P(i, 0) = fp_consts::one();
86 const auto x = X(XOffs(i));
87 for (int_t k = 0; k < active_count; ++k) {
88 const int_t j = active_index[k];
89 P(i, 1 + k) = x[j];
90 }
91 }
92}
93
108template <KokkosViewRank<2> QViewType, KokkosViewRank<1> XViewType,
109 KokkosViewRank<1> XOffsType, KokkosViewRank<1> WViewType,
110 KokkosArray AxisType, int_t dim = AxisType::size()>
112 const XOffsType &XOffs,
113 WViewType &W,
115 using ExecSpace = typename QViewType::execution_space;
116 int_t poly_vals = dim + 1;
117
118 Kokkos::Array<fp_t, dim + 1> scratch_data;
119 for (int i = 0; i < dim + 1; ++i) {
121 }
122 Kokkos::View<fp_t *, ExecSpace> scratch(scratch_data.data(), dim + 1);
123
124 do {
126 auto S = Kokkos::subview(scratch, Kokkos::make_pair(0, poly_vals));
127 SerialSVD(Q, S, W);
128 const fp_t condition =
129 S(0) / Kokkos::max(S(poly_vals - 1), fp_consts::epsilon());
130 if (condition > 1.0e5) {
132 --poly_vals;
133 } else {
135 break;
136 }
137 } while (poly_vals > 1);
138 return poly_vals;
139}
140
149template <KokkosViewRank<2> AViewType, KokkosViewRank<1> BViewType,
150 KokkosViewRank<1> OutViewType>
153 namespace KB = KokkosBlas;
154 using Gemv =
155 KB::SerialGemv<KB::Trans::NoTranspose, KB::Algo::Gemv::Unblocked>;
156 Gemv::invoke(fp_consts::one(), A, B, fp_consts::zero(), out);
157}
158
164template <KokkosViewRank<1> UViewType, KokkosViewRank<1> VViewType>
166 const VViewType &V) {
167 const int_t N = U.extent_int(0);
168 for (int_t i = 0; i < N; ++i) {
169 U(i) += V(i);
170 }
171}
172
178template <KokkosViewRank<1> UViewType, KokkosViewRank<1> VViewType>
180 const VViewType &V) {
181 const int_t N = U.extent_int(0);
182 for (int_t i = 0; i < N; ++i) {
183 U(i) -= V(i);
184 }
185}
186
187} // namespace RbfPum
188} // namespace PACMAN
KOKKOS_FORCEINLINE_FUNCTION void SerialVecVecSub(UViewType &U, const VViewType &V)
Perform an inplace difference of 2 vectors such as U = U - V.
KOKKOS_FORCEINLINE_FUNCTION int_t SerialFillQ(QViewType &Q, const XViewType &X, const XOffsType &XOffs, WViewType &W, AxisType &activeAxis)
Fill the polynomial augmentation matrix Q and sets activeAxis accordingly to the deactivated axis in ...
KOKKOS_FORCEINLINE_FUNCTION void SerialSVD(AViewType &A, SViewType &S, WViewType &W)
Perform a partial SVD decomposition, to fetch only the singular values of A.
typename std::remove_cv< typename std::remove_reference< T >::type >::type base_type
Returns a type without reference or const/volatile modifiers.
Definition utils.hpp:42
KOKKOS_FUNCTION void SerialFillPoly(PViewType &P, const XViewType &X, const OffsViewType &XOffs, AxisType &activeAxis)
Fill the polynomial augmentation matrix accordingly to the activated axis in activeAxis
KOKKOS_INLINE_FUNCTION auto RbfPumMinMax(const ViewType &v, const Comparator &op)
A reimplementation of Kokkos::MinMax which works on any backend.
Definition utils.hpp:110
KOKKOS_FORCEINLINE_FUNCTION void SerialMulMatVec(const AViewType &A, const BViewType &B, OutViewType &out)
Perform: out = Ab.
KOKKOS_FORCEINLINE_FUNCTION void SerialVecVecAdd(UViewType &U, const VViewType &V)
Perform an inplace addition of 2 vectors such as U = U + V.
KOKKOS_INLINE_FUNCTION int_t SerialDeactivateOneAxis(const SourceType &src, const OffsType &offs, AxisType &activeAxis)
Deactivate one axis, the one with the smallest range of values and returns the index of this deactiva...
KOKKOS_INLINE_FUNCTION consteval fp_t zero(void)
Definition types.hpp:92
KOKKOS_INLINE_FUNCTION consteval fp_t epsilon(void)
Definition types.hpp:98
KOKKOS_INLINE_FUNCTION fp_t max(void)
Definition types.hpp:89
KOKKOS_INLINE_FUNCTION consteval fp_t one(void)
Definition types.hpp:95