SimiLie
Loading...
Searching...
No Matches
clamp_to_domain.hpp
1// SPDX-FileCopyrightText: 2026 Baptiste Legouix
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4#pragma once
5
6#include <algorithm>
7
8#include <ddc/ddc.hpp>
9
10#include "select_from_type_seq.hpp"
11#include "signed_discrete_element.hpp"
12#include "specialization.hpp"
13#include "type_seq_conversion.hpp"
14#include "type_seq_ext.hpp"
15
16namespace sil {
17
18namespace misc {
19
20namespace detail {
21
22template <class CommonSeq>
23struct ClampToDomain;
24
25template <class... CommonDDim>
26struct ClampToDomain<ddc::TypeSeq<CommonDDim...>>
27{
28 template <class BatchDomain, class Elem>
29 KOKKOS_FUNCTION static Elem operator()(BatchDomain const& batch_domain, Elem elem)
30 {
31 ddc::DiscreteDomain<CommonDDim...> const common_domain
32 = select_from_type_seq<ddc::TypeSeq<CommonDDim...>>(batch_domain);
33 ddc::DiscreteElement<CommonDDim...> const front = common_domain.front();
34 ddc::DiscreteElement<CommonDDim...> const back = common_domain.back();
35
36 if constexpr (misc::Specialization<Elem, SignedDiscreteElement>) {
37 ((elem.template uid<CommonDDim>() = std::
38 max(static_cast<std::ptrdiff_t>(front.template uid<CommonDDim>()),
39 std::
40 min(elem.template uid<CommonDDim>(),
41 static_cast<std::ptrdiff_t>(
42 back.template uid<CommonDDim>())))),
43 ...);
44 } else {
45 // Signed offsets distinguish a sample below zero from a large upper index.
46 ((elem.template uid<CommonDDim>()
47 = (ddc::DiscreteElement<CommonDDim>(elem) - ddc::DiscreteElement<CommonDDim>(front))
48 .template get<CommonDDim>()
49 < 0
50 ? front.template uid<CommonDDim>()
51 : std::
52 min(elem.template uid<CommonDDim>(),
53 back.template uid<CommonDDim>())),
54 ...);
55 }
56 return elem;
57 }
58};
59
60} // namespace detail
61
62template <class BatchDomain, class Elem>
63KOKKOS_FUNCTION Elem clamp_to_domain(BatchDomain const& batch_domain, Elem const& elem)
64{
65 using CommonSeq = misc::
66 type_seq_intersect_t<misc::to_type_seq_t<Elem>, ddc::to_type_seq_t<BatchDomain>>;
67 return detail::ClampToDomain<CommonSeq>::operator()(batch_domain, elem);
68}
69
70} // namespace misc
71
72} // namespace sil
KOKKOS_FUNCTION Elem clamp_to_domain(BatchDomain const &batch_domain, Elem const &elem)
KOKKOS_FUNCTION auto select_from_type_seq(T t)
The top-level namespace of SimiLie.
Definition csr.hpp:15