alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
DataPitches.hpp
Go to the documentation of this file.
1/* Copyright 2024 René Widera
2 * SPDX-License-Identifier: MPL-2.0
3 */
4
5#pragma once
6
7#include "alpaka/CVec.hpp"
8#include "alpaka/Vec.hpp"
11
12#include <type_traits>
13
14namespace alpaka
15{
16 //! Calculate the pitches purely from the extents.
17 template<typename T_Elem, alpaka::concepts::Vector T_Vec>
18 constexpr auto calculatePitchesFromExtents(T_Vec const& extent)
19 {
20 constexpr auto dim = T_Vec::dim();
21 using type = typename T_Vec::type;
22 auto pitchBytes = typename T_Vec::UniVec{};
23 if constexpr(dim > 0)
24 pitchBytes.back() = static_cast<type>(sizeof(T_Elem));
25 if constexpr(dim > 1)
26 for(type i = dim - 1; i > 0; i--)
27 pitchBytes[i - 1] = extent[i] * pitchBytes[i];
28 return pitchBytes;
29 }
30
31 //! Calculate the pitches purely from the extents.
32 template<typename T_Elem, alpaka::concepts::Vector T_Vec>
33 requires(T_Vec::dim() >= 2)
34 constexpr auto calculatePitches(T_Vec const& extent, typename T_Vec::type const& rowPitchBytes)
35 {
36 constexpr auto dim = T_Vec::dim();
37 using type = typename T_Vec::type;
38 auto pitchBytes = typename T_Vec::UniVec{};
39 pitchBytes.back() = static_cast<type>(sizeof(T_Elem));
40 if constexpr(dim > 1)
41 pitchBytes[dim - 2u] = rowPitchBytes;
42 if constexpr(dim > 2)
43 for(type i = dim - 2; i > 0; i--)
44 pitchBytes[i - 1] = extent[i] * pitchBytes[i];
45 return pitchBytes;
46 }
47
48 template<typename T_Type, concepts::Vector T_Pitches>
50 {
51 using value_type = T_Type;
52 using index_type = typename T_Pitches::type;
53
54 static consteval uint32_t dim()
55 {
56 return T_Pitches::dim();
57 }
58
59 constexpr DataPitches(T_Pitches const& pitchBytes) : m_pitch(pitchBytes.eraseBack())
60 {
61 assert(pitchBytes.back() == sizeof(value_type));
62 }
63
64 /*Object must init by copy a valid instance*/
65 constexpr DataPitches() = default;
66
67 constexpr auto getPitches() const
68 {
69 Vec<index_type, dim()> result;
70 for(uint32_t d = 0u; d < dim() - 1u; ++d)
71 {
72 result[d] = m_pitch[d];
73 }
74 result.back() = static_cast<index_type>(sizeof(value_type));
75 return result;
76 }
77
78 constexpr index_type operator[](std::integral auto idx) const
79 {
80 return getPitches()[idx];
81 }
82
83 private:
84 decltype(std::declval<T_Pitches>().eraseBack()) m_pitch;
85 };
86
87 template<typename T_Type, typename T_IndexType, typename T_Storage>
88 struct DataPitches<T_Type, Vec<T_IndexType, 1u, T_Storage>>
89 {
90 using value_type = T_Type;
91 using index_type = T_IndexType;
92
93 static consteval uint32_t dim()
94 {
95 return 1u;
96 }
97
98 constexpr DataPitches([[maybe_unused]] Vec<T_IndexType, 1u> const& pitchBytes)
99 {
100 assert(pitchBytes.back() == sizeof(value_type));
101 }
102
103 /*Object must init by copy a valid instance*/
104 constexpr DataPitches() = default;
105
106 constexpr auto getPitches() const
107 {
108 return Vec{static_cast<index_type>(sizeof(value_type))};
109 }
110 };
111} // namespace alpaka
main alpaka namespace.
Definition alpaka.hpp:76
constexpr auto calculatePitchesFromExtents(T_Vec const &extent)
Calculate the pitches purely from the extents.
constexpr auto calculatePitches(T_Vec const &extent, typename T_Vec::type const &rowPitchBytes)
Calculate the pitches purely from the extents.
constexpr DataPitches(Vec< T_IndexType, 1u > const &pitchBytes)
constexpr auto getPitches() const
constexpr index_type operator[](std::integral auto idx) const
constexpr DataPitches()=default
constexpr DataPitches(T_Pitches const &pitchBytes)
static consteval uint32_t dim()
typename T_Pitches::type index_type
constexpr decltype(auto) back()
Definition Vec.hpp:392