Gyselalib++
 
Loading...
Searching...
No Matches
view.hpp
1#pragma once
2
3#include <array>
4#include <ostream>
5#include <utility>
6
7#include <Kokkos_Core.hpp>
8
9namespace detail {
10
11template <std::size_t N, class ElementType, bool CONTIGUOUS = true>
12struct ViewNDMaker;
13
14template <std::size_t N, class ElementType>
15struct ViewNDMaker<N, ElementType, true>
16{
17 using type
18 = Kokkos::mdspan<ElementType, Kokkos::dextents<std::size_t, N>, Kokkos::layout_right>;
19};
20
21template <std::size_t N, class ElementType>
22struct ViewNDMaker<N, ElementType, false>
23{
24 using type
25 = Kokkos::mdspan<ElementType, Kokkos::dextents<std::size_t, N>, Kokkos::layout_stride>;
26};
27
33template <
34 class ElementType,
35 class Extents,
36 class Layout,
37 class Accessor,
38 std::size_t I0,
39 std::size_t... Is>
40std::ostream& stream_impl(
41 std::ostream& os,
42 Kokkos::mdspan<ElementType, Extents, Layout, Accessor> const& s,
43 std::index_sequence<I0, Is...>)
44{
45 if constexpr (sizeof...(Is) > 0) {
46 os << '[';
47 for (std::size_t i0 = 0; i0 < s.extent(I0); ++i0) {
48 stream_impl(
49 os,
50 Kokkos::submdspan(s, i0, ((void)Is, Kokkos::full_extent)...),
51 std::make_index_sequence<sizeof...(Is)>());
52 }
53 os << ']';
54 } else {
55 os << '[';
56 for (std::size_t i0 = 0; i0 < s.extent(I0) - 1; ++i0) {
57 os << s(i0) << ',';
58 }
59 os << s(s.extent(I0) - 1) << ']';
60 }
61 return os;
62}
63
64
65// template <class>
66// struct IsContiguous;
67// template <>
68// struct IsContiguous<Kokkos::layout_stride>
69// {
70// static constexpr bool val = false;
71// };
72// template <>
73// struct IsContiguous<Kokkos::layout_right>
74// {
75// static constexpr bool val = true;
76// };
77// template <class ElementType, class Extents, class LayoutPolicy, class AccessorPolicy>
78// struct IsContiguous<Kokkos::mdspan<ElementType, Extents, LayoutPolicy, AccessorPolicy>>
79// {
80// static constexpr bool val = IsContiguous<LayoutPolicy>::val;
81// };
82
83} // namespace detail
84
85
86template <std::size_t N, class ElementType>
87using SpanND = Kokkos::mdspan<ElementType, Kokkos::dextents<std::size_t, N>>;
88
89template <std::size_t N, class ElementType>
90using ViewND = SpanND<N, ElementType const>;
91
92template <class ElementType>
93using Span1D = SpanND<1, ElementType>;
94
95template <class ElementType>
96using Span2D = SpanND<2, ElementType>;
97
98template <class ElementType>
99using View1D = ViewND<1, ElementType>;
100
101template <class ElementType>
102using View2D = ViewND<2, ElementType>;
103
104template <std::size_t N>
105using DKokkosView = Kokkos::View<double[N]>;
106
107template <std::size_t N>
108using DKokkosView_h = Kokkos::View<double[N], Kokkos::HostSpace>;
109
110template <class ElementType, std::size_t N>
111KOKKOS_FUNCTION Span1D<ElementType> as_span(std::array<ElementType, N>& arr) noexcept
112{
113 return Span1D<ElementType>(arr.data(), N);
114}
115
116template <class ElementType, std::size_t N>
117KOKKOS_FUNCTION Span1D<const ElementType> as_span(std::array<ElementType, N> const& arr) noexcept
118{
119 return Span1D<const ElementType>(arr.data(), N);
120}
121
122using DSpan1D = Span1D<double>;
123
124using DSpan2D = Span2D<double>;
125
126using CDSpan1D = Span1D<double const>;
127
128using CDSpan2D = Span2D<double const>;
129
130using DView1D = View1D<double>;
131
132using DView2D = View2D<double>;
133
135using Matrix_2x2 = std::array<std::array<double, 2>, 2>;
136
137// template <class C>
138// constexpr bool is_contiguous_v = detail::IsContiguous<C>::val;
139
142template <class ElementType, class Extents, class Layout, class Accessor>
143std::ostream& operator<<(
144 std::ostream& os,
145 Kokkos::mdspan<ElementType, Extents, Layout, Accessor> const& s)
146{
147 return detail::stream_impl(os, s, std::make_index_sequence<Extents::rank()>());
148}