DDC 0.1.0
Loading...
Searching...
No Matches
view.hpp
1// Copyright (C) The DDC development team, see COPYRIGHT.md file
2//
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
7#include <cstddef>
8#include <ostream>
9#include <utility>
10
11#include <Kokkos_Core.hpp>
12
13namespace ddc::detail {
14
15template <std::size_t N, class ElementType, bool CONTIGUOUS = true>
16struct ViewNDMaker;
17
18template <std::size_t N, class ElementType>
19struct ViewNDMaker<N, ElementType, true>
20{
21 using type
22 = Kokkos::mdspan<ElementType, Kokkos::dextents<std::size_t, N>, Kokkos::layout_right>;
23};
24
25template <std::size_t N, class ElementType>
26struct ViewNDMaker<N, ElementType, false>
27{
28 using type
29 = Kokkos::mdspan<ElementType, Kokkos::dextents<std::size_t, N>, Kokkos::layout_stride>;
30};
31
32/// Note: We use the comma operator to fill the input parameters
33///
34/// If Is=[1, 2], `subspan(s, i0, (Is, all)...)` will be expanded as
35/// `subspan(s, i0, (1, all), (2, all))` which is equivalent to
36/// `subspan(s, i0, all, all)`
37template <
38 class ElementType,
39 class Extents,
40 class Layout,
41 class Accessor,
42 std::size_t I0,
43 std::size_t... Is>
44std::ostream& stream_impl(
45 std::ostream& os,
46 Kokkos::mdspan<ElementType, Extents, Layout, Accessor> const& s,
47 std::index_sequence<I0, Is...>)
48{
49 if constexpr (sizeof...(Is) > 0) {
50 os << '[';
51 for (std::size_t i0 = 0; i0 < s.extent(I0); ++i0) {
52 stream_impl(
53 os,
54 Kokkos::submdspan(s, i0, ((void)Is, Kokkos::full_extent)...),
55 std::make_index_sequence<sizeof...(Is)>());
56 }
57 os << ']';
58 } else {
59 os << '[';
60 for (std::size_t i0 = 0; i0 < s.extent(I0) - 1; ++i0) {
61 os << s(i0) << ',';
62 }
63 os << s(s.extent(I0) - 1) << ']';
64 }
65 return os;
66}
67
68/// Convenient function to dump a mdspan, it recursively prints all dimensions.
69/// Disclaimer: use with caution for large arrays
70template <class ElementType, class Extents, class Layout, class Accessor>
71std::ostream& operator<<(
72 std::ostream& os,
73 Kokkos::mdspan<ElementType, Extents, Layout, Accessor> const& s)
74{
75 return ddc::detail::stream_impl(os, s, std::make_index_sequence<Extents::rank()>());
76}
77
78} // namespace ddc::detail
79
80namespace ddc {
81
82template <std::size_t N, class ElementType>
83using SpanND = Kokkos::mdspan<ElementType, Kokkos::dextents<std::size_t, N>>;
84
85template <std::size_t N, class ElementType>
86using ViewND = SpanND<N, ElementType const>;
87
88template <class ElementType>
89using Span1D = SpanND<1, ElementType>;
90
91template <class ElementType>
92using Span2D = SpanND<2, ElementType>;
93
94template <class ElementType>
95using View1D = ViewND<1, ElementType>;
96
97template <class ElementType>
98using View2D = ViewND<2, ElementType>;
99
100using DSpan1D = ddc::Span1D<double>;
101
102using DSpan2D = ddc::Span2D<double>;
103
104using CDSpan1D = ddc::Span1D<double const>;
105
106using CDSpan2D = ddc::Span2D<double const>;
107
108using DView1D = View1D<double>;
109
110using DView2D = View2D<double>;
111
112} // namespace ddc
The top-level namespace of DDC.