SimiLie
Loading...
Searching...
No Matches
bilinear_operator_storage.hpp
1// SPDX-FileCopyrightText: 2026 Baptiste Legouix
2// SPDX-License-Identifier: AGPL-3.0-or-later
3// AI-GENERATED
4
5#pragma once
6
7#include <tuple>
8
9#include <similie/exterior/stencil.hpp>
10
11namespace sil::exterior {
12
21template <
22 class Operator,
23 class Index,
24 class First,
25 class Second,
26 class Target,
27 std::size_t FirstRadius,
28 std::size_t SecondRadius,
29 class Domain,
30 class Memory,
31 class Exec = typename Memory::execution_space>
33{
35 Domain m_domain, m_first_domain, m_second_domain;
36 Kokkos::View<
37 double * [Target::mem_size() * Coefficients::first_size * Coefficients::second_size],
38 Kokkos::LayoutLeft,
39 Memory>
40 m_weights;
41
42public:
43 using execution_space = Exec;
44 template <class OtherExec>
46 Operator,
47 Index,
48 First,
49 Second,
50 Target,
51 FirstRadius,
52 SecondRadius,
53 Domain,
54 Memory,
55 OtherExec>;
56
57 template <class Geometry, class A, class B, class Arguments, std::size_t... I>
58 KOKKOS_FUNCTION static Coefficients evaluate_value(
59 Geometry const& geometry,
60 A const& first,
61 B const& second,
62 typename Domain::discrete_element_type element,
63 Arguments const& arguments,
64 std::index_sequence<I...>)
65 {
66 return Operator::value(geometry, first, second, element, std::get<I>(arguments)...);
67 }
68
69 template <class FirstField, class SecondField, class Geometry, class... Arguments>
70 requires(
71 std::same_as<typename FirstField::indices_domain_t, ddc::DiscreteDomain<First>>
72 && std::same_as<
73 typename SecondField::indices_domain_t,
74 ddc::DiscreteDomain<Second>>)
76 Exec const& exec,
77 FirstField const& first,
78 SecondField const& second,
79 Geometry const& geometry,
80 Arguments const&... arguments)
81 {
83 result.m_domain = first.non_indices_domain();
84 result.m_first_domain = first.non_indices_domain();
85 result.m_second_domain = second.non_indices_domain();
86 // Equal spatial domains keep sampling and cached boundary rules unambiguous.
87 if (result.m_first_domain != result.m_second_domain)
88 throw std::invalid_argument("Bilinear staging requires equal input spatial domains");
89 result.m_weights = decltype(m_weights)(
90 Kokkos::view_alloc(Kokkos::WithoutInitializing, "staged_bilinear_stencils"),
91 result.m_domain.size());
92 auto const weights = result.m_weights;
93 Domain const domain = result.m_domain;
94 std::tuple<Arguments...> const cached_arguments(arguments...);
95 Kokkos::View<int, Memory> incomplete("staged_bilinear_incomplete");
96 Kokkos::parallel_for(
97 "similie_cache_bilinear_operator_stencils",
98 Kokkos::RangePolicy<Exec>(exec, 0, domain.size()),
99 KOKKOS_LAMBDA(std::size_t node) {
100 Coefficients const stencil = evaluate_value(
101 geometry,
102 first,
103 second,
104 detail::stencil_element(domain, node),
105 cached_arguments,
106 std::index_sequence_for<Arguments...>());
107 if (!stencil.complete)
108 Kokkos::atomic_max(&incomplete(), 1);
109 for (std::size_t entry = 0; entry < stencil.weights.size(); ++entry)
110 weights(node, entry) = stencil.weights[entry];
111 });
112 int failed;
113 Kokkos::deep_copy(exec, failed, incomplete);
114 exec.fence();
115 if (failed)
116 throw std::invalid_argument("Bilinear boundary closure exceeds cached stencil support");
117 return result;
118 }
119
120 [[nodiscard]] KOKKOS_FUNCTION Coefficients
121 value(typename Domain::discrete_element_type element) const
122 {
123 Coefficients result;
124 std::size_t const node = detail::stencil_node(m_domain, element);
125 for (std::size_t entry = 0; entry < result.weights.size(); ++entry)
126 result.weights[entry] = m_weights(node, entry);
127 return result;
128 }
129
130 template <class Output, class FirstTensor, class SecondTensor>
131 requires(
132 FirstRadius == 0 && SecondRadius == 0
133 && std::same_as<typename FirstTensor::indices_domain_t, ddc::DiscreteDomain<First>>
134 && std::same_as<
135 typename SecondTensor::indices_domain_t,
136 ddc::DiscreteDomain<Second>>
137 && std::same_as<typename Output::indices_domain_t, ddc::DiscreteDomain<Target>>)
138 KOKKOS_FUNCTION void apply_pointwise(
139 Output const& output,
140 FirstTensor const& first,
141 SecondTensor const& second,
142 typename Domain::discrete_element_type element) const
143 {
144 std::size_t const node = detail::stencil_node(m_domain, element);
145 for (std::size_t target = 0; target < Target::mem_size(); ++target) {
146 double sum = 0.0;
147 for (std::size_t a = 0; a < First::mem_size(); ++a)
148 for (std::size_t b = 0; b < Second::mem_size(); ++b)
149 sum += m_weights(
150 node,
151 (target * First::mem_size() + a) * Second::mem_size() + b)
152 * first.mem(ddc::DiscreteElement<First>(a))
153 * second.mem(ddc::DiscreteElement<Second>(b));
154 output.mem(ddc::DiscreteElement<Target>(target)) = sum;
155 }
156 }
157
158 template <class Output, class FirstField, class SecondField>
159 requires(
160 std::same_as<typename FirstField::indices_domain_t, ddc::DiscreteDomain<First>>
161 && std::same_as<typename SecondField::indices_domain_t, ddc::DiscreteDomain<Second>>
162 && std::same_as<typename Output::indices_domain_t, ddc::DiscreteDomain<Target>>)
163 Output apply(
164 Exec const& exec,
165 Output const& output,
166 FirstField const& first,
167 SecondField const& second) const
168 {
169 if (first.non_indices_domain() != m_first_domain
170 || second.non_indices_domain() != m_second_domain)
171 throw std::invalid_argument("Staged bilinear operator input domain changed");
172 Domain const out_domain = output.non_indices_domain();
173 if (!out_domain.empty()
174 && (!misc::domain_contains(m_domain, out_domain.front())
175 || !misc::domain_contains(m_domain, out_domain.back())))
176 throw std::invalid_argument("Staged bilinear operator output exceeds cached domain");
177 auto const weights = m_weights;
178 Domain const domain = m_domain;
179 Kokkos::parallel_for(
180 "similie_apply_staged_bilinear_stencil",
181 Kokkos::RangePolicy<Exec>(exec, 0, out_domain.size() * Target::mem_size()),
182 KOKKOS_LAMBDA(std::size_t row) {
183 auto const element
184 = detail::stencil_element(out_domain, row / Target::mem_size());
185 std::size_t const node = detail::stencil_node(domain, element),
186 target = row % Target::mem_size();
187 double sum = 0.0;
188 for (std::size_t a = 0; a < Coefficients::first_size; ++a)
189 for (std::size_t b = 0; b < Coefficients::second_size; ++b) {
190 double const coefficient = weights(
191 node,
192 (target * Coefficients::first_size + a)
194 + b);
195 if (coefficient != 0.0)
196 sum += coefficient
197 * first
198 .mem(detail::stencil_source<Index, FirstRadius>(
199 domain,
200 element,
201 a / First::mem_size()),
202 ddc::DiscreteElement<First>(
203 a % First::mem_size()))
204 * second
205 .mem(detail::stencil_source<Index, SecondRadius>(
206 domain,
207 element,
208 b / Second::mem_size()),
209 ddc::DiscreteElement<Second>(
210 b % Second::mem_size()));
211 }
212 output.mem(element, ddc::DiscreteElement<Target>(target)) = sum;
213 });
214 return output;
215 }
216};
217
218// A local bilinear map has no spatial offsets for either operand.
219template <
220 class Operator,
221 class Index,
222 class First,
223 class Second,
224 class Target,
225 class Domain,
226 class Memory,
227 class Exec = typename Memory::execution_space>
229 Operator,
230 Index,
231 First,
232 Second,
233 Target,
234 0,
235 0,
236 Domain,
237 Memory,
238 Exec>;
239
240} // namespace sil::exterior
Cache a bilinear spatial map without freezing either operand.
Output apply(Exec const &exec, Output const &output, FirstField const &first, SecondField const &second) const
static KOKKOS_FUNCTION Coefficients evaluate_value(Geometry const &geometry, A const &first, B const &second, typename Domain::discrete_element_type element, Arguments const &arguments, std::index_sequence< I... >)
static BilinearSpatialOperatorStorage build(Exec const &exec, FirstField const &first, SecondField const &second, Geometry const &geometry, Arguments const &... arguments)
KOKKOS_FUNCTION void apply_pointwise(Output const &output, FirstTensor const &first, SecondTensor const &second, typename Domain::discrete_element_type element) const
KOKKOS_FUNCTION Coefficients value(typename Domain::discrete_element_type element) const
KOKKOS_FUNCTION bool domain_contains(ddc::DiscreteDomain< DDim... > const &dom, ddc::DiscreteElement< ODDim... > elem)
Homogeneous bilinear coefficients, ordered output, first point/component, second point/component (sec...
Definition stencil.hpp:55
static constexpr std::size_t first_size
Definition stencil.hpp:57
std::array< double, Target::mem_size() *first_size *second_size > weights
Definition stencil.hpp:60
static constexpr std::size_t second_size
Definition stencil.hpp:59