alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
Iterator.hpp
Go to the documentation of this file.
1/* Copyright 2022 Benjamin Worpitz, Erik Zenker, Bernhard Manfred Gruber, Jan Stephan
2 * SPDX-License-Identifier: MPL-2.0
3 */
4
5#pragma once
6
7#include "alpaka/alpaka.hpp"
8
9#include <type_traits>
10
11namespace alpaka::test
12{
13 namespace trait
14 {
15 // \tparam T Type to conditionally make const.
16 // \tparam TSource Type to mimic the constness of.
17 template<typename T, typename TSource>
18 using MimicConst = std::conditional_t<std::is_const_v<TSource>, std::add_const_t<T>, std::remove_const_t<T>>;
19
20 template<typename TView, typename TSfinae = void>
22 {
23 using TViewDecayed = std::decay_t<TView>;
24 using Dim = alpaka::Dim<TViewDecayed>;
25 using Idx = alpaka::Idx<TViewDecayed>;
26 using Elem = MimicConst<alpaka::Elem<TViewDecayed>, TView>;
27
28 public:
29 ALPAKA_FN_HOST IteratorView(TView& view, Idx const idx)
30 : m_nativePtr(getPtrNative(view))
31 , m_currentIdx(idx)
32 , m_extents(getExtents(view))
33 , m_pitchBytes(getPitchesInBytes(view))
34 {
35 }
36
37 ALPAKA_FN_HOST explicit IteratorView(TView& view) : IteratorView(view, 0)
38 {
39 }
40
42 {
43 ++m_currentIdx;
44 return *this;
45 }
46
48 {
49 --m_currentIdx;
50 return *this;
51 }
52
54 {
55 IteratorView iterCopy = *this;
56 m_currentIdx++;
57 return iterCopy;
58 }
59
61 {
62 IteratorView iterCopy = *this;
63 m_currentIdx--;
64 return iterCopy;
65 }
66
67 template<typename TIter>
68 ALPAKA_FN_HOST_ACC auto operator==(TIter& other) const -> bool
69 {
70 return m_currentIdx == other.m_currentIdx;
71 }
72
73 template<typename TIter>
74 ALPAKA_FN_HOST_ACC auto operator!=(TIter& other) const -> bool
75 {
76 return m_currentIdx != other.m_currentIdx;
77 }
78
79 ALPAKA_FN_HOST_ACC auto operator*() const -> Elem&
80 {
81 if constexpr(Dim::value == 0)
82 return *m_nativePtr;
83 else
84 {
85 Vec<Dim, Idx> const currentIdxDimx
86 = mapIdx<Dim::value>(Vec<DimInt<1>, Idx>{m_currentIdx}, m_extents);
87 auto const offsetInBytes = (currentIdxDimx * m_pitchBytes).sum();
88 using QualifiedByte = MimicConst<std::byte, Elem>;
89#if BOOST_COMP_GNUC
90# pragma GCC diagnostic push
91 // "cast from 'Byte*' to 'Elem*' increases required alignment of target type"
92# pragma GCC diagnostic ignored "-Wcast-align"
93#endif
94 return *reinterpret_cast<Elem*>(reinterpret_cast<QualifiedByte*>(m_nativePtr) + offsetInBytes);
95#if BOOST_COMP_GNUC
96# pragma GCC diagnostic pop
97#endif
98 }
99 ALPAKA_UNREACHABLE(*m_nativePtr);
100 }
101
102 private:
103 Elem* m_nativePtr;
104 Idx m_currentIdx;
105 Vec<Dim, Idx> m_extents;
106 Vec<Dim, Idx> m_pitchBytes;
107 };
108
109 template<typename TView, typename TSfinae = void>
110 struct Begin
111 {
112 ALPAKA_FN_HOST static auto begin(TView& view) -> IteratorView<TView>
113 {
114 return IteratorView<TView>(view);
115 }
116 };
117
118 template<typename TView, typename TSfinae = void>
119 struct End
120 {
121 ALPAKA_FN_HOST static auto end(TView& view) -> IteratorView<TView>
122 {
123 auto extents = getExtents(view);
124 return IteratorView<TView>(view, extents.prod());
125 }
126 };
127 } // namespace trait
128
129 template<typename TView>
131
132 template<typename TView>
134 {
135 return trait::Begin<TView>::begin(view);
136 }
137
138 template<typename TView>
139 ALPAKA_FN_HOST auto end(TView& view) -> Iterator<TView>
140 {
141 return trait::End<TView>::end(view);
142 }
143} // namespace alpaka::test
#define ALPAKA_UNREACHABLE(...)
Before CUDA 11.5 nvcc is unable to correctly identify return statements in 'if constexpr' branches....
A n-dimensional vector.
Definition Vec.hpp:38
ALPAKA_FN_HOST_ACC auto operator*() const -> Elem &
Definition Iterator.hpp:79
ALPAKA_FN_HOST IteratorView(TView &view)
Definition Iterator.hpp:37
ALPAKA_FN_HOST_ACC auto operator++() -> IteratorView &
Definition Iterator.hpp:41
ALPAKA_FN_HOST IteratorView(TView &view, Idx const idx)
Definition Iterator.hpp:29
ALPAKA_FN_HOST_ACC auto operator++(int) -> IteratorView
Definition Iterator.hpp:53
ALPAKA_FN_HOST_ACC auto operator==(TIter &other) const -> bool
Definition Iterator.hpp:68
ALPAKA_FN_HOST_ACC auto operator!=(TIter &other) const -> bool
Definition Iterator.hpp:74
ALPAKA_FN_HOST_ACC auto operator--(int) -> IteratorView
Definition Iterator.hpp:60
ALPAKA_FN_HOST_ACC auto operator--() -> IteratorView &
Definition Iterator.hpp:47
#define ALPAKA_FN_HOST
Definition Common.hpp:40
#define ALPAKA_FN_HOST_ACC
Definition Common.hpp:39
std::conditional_t< std::is_const_v< TSource >, std::add_const_t< T >, std::remove_const_t< T > > MimicConst
Definition Iterator.hpp:18
The test specifics.
Definition TestAccs.hpp:27
ALPAKA_FN_HOST auto end(TView &view) -> Iterator< TView >
Definition Iterator.hpp:139
ALPAKA_FN_HOST auto begin(TView &view) -> Iterator< TView >
Definition Iterator.hpp:133
typename trait::IdxType< T >::type Idx
Definition Traits.hpp:29
ALPAKA_FN_HOST auto getPitchesInBytes(TView const &view) -> Vec< Dim< TView >, Idx< TView > >
Definition Traits.hpp:196
ALPAKA_FN_HOST auto getPtrNative(TView const &view) -> Elem< TView > const *
Gets the native pointer of the memory view.
Definition Traits.hpp:136
std::remove_volatile_t< typename trait::ElemType< TView >::type > Elem
The element type trait alias template to remove the ::type.
Definition Traits.hpp:21
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto getExtents(T const &object) -> Vec< Dim< T >, Idx< T > >
Definition Traits.hpp:59
std::integral_constant< std::size_t, N > DimInt
typename trait::DimType< T >::type Dim
The dimension type trait alias template to remove the ::type.
Definition Traits.hpp:19
static ALPAKA_FN_HOST auto begin(TView &view) -> IteratorView< TView >
Definition Iterator.hpp:112
static ALPAKA_FN_HOST auto end(TView &view) -> IteratorView< TView >
Definition Iterator.hpp:121