-
Notifications
You must be signed in to change notification settings - Fork 13
/
hpc_range_sum.hpp
234 lines (226 loc) · 6.87 KB
/
hpc_range_sum.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#pragma once
#include <cassert>
#include <hpc_numeric.hpp>
#include <hpc_range.hpp>
#include <hpc_vector.hpp>
#include <iterator>
namespace hpc {
template <class TargetIndex, class SourceIndex>
class range_sum_iterator
{
TargetIndex const* m_ptr;
public:
using value_type = counting_range<TargetIndex>;
using difference_type = SourceIndex;
using reference = value_type;
using pointer = value_type const*;
using iterator_category = std::random_access_iterator_tag;
HPC_ALWAYS_INLINE HPC_HOST_DEVICE explicit constexpr range_sum_iterator(TargetIndex const* ptr_in) noexcept
: m_ptr(ptr_in)
{
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr bool
operator==(range_sum_iterator const& other) const noexcept
{
return m_ptr == other.m_ptr;
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr bool
operator!=(range_sum_iterator const& other) const noexcept
{
return m_ptr != other.m_ptr;
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr reference
operator*() const noexcept
{
return value_type(m_ptr[0], m_ptr[1]);
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE range_sum_iterator&
operator++() noexcept
{
++m_ptr;
return *this;
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE range_sum_iterator
operator++(int) noexcept
{
auto const ret = *this;
++m_ptr;
return ret;
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE range_sum_iterator&
operator--() noexcept
{
--m_ptr;
return *this;
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE range_sum_iterator
operator--(int) noexcept
{
auto ret = *this;
--m_ptr;
return ret;
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE range_sum_iterator&
operator+=(difference_type const n) noexcept
{
m_ptr = m_ptr + n;
return *this;
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE range_sum_iterator&
operator-=(difference_type const n) noexcept
{
m_ptr = m_ptr - n;
return *this;
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr range_sum_iterator
operator+(difference_type const n) const noexcept
{
return range_sum_iterator(m_ptr + n);
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr range_sum_iterator
operator-(difference_type const n) const noexcept
{
return range_sum_iterator(m_ptr - n);
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr difference_type
operator-(range_sum_iterator const& other) const noexcept
{
return difference_type(m_ptr - other.m_ptr);
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr reference
operator[](difference_type const n) const noexcept
{
return *((*this) + n);
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr bool
operator<(range_sum_iterator const& other) const noexcept
{
return m_ptr < other.m_ptr;
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr bool
operator>(range_sum_iterator const& other) const noexcept
{
return m_ptr > other.m_ptr;
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr bool
operator<=(range_sum_iterator const& other) const noexcept
{
return m_ptr <= other.m_ptr;
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr bool
operator>=(range_sum_iterator const& other) const noexcept
{
return m_ptr >= other.m_ptr;
}
};
template <class TargetIndex, class Allocator, class ExecutionPolicy, class SourceIndex>
class range_sum
{
using vector_type = ::hpc::vector<TargetIndex, Allocator, ExecutionPolicy, SourceIndex>;
vector_type m_vector;
public:
using value_type = ::hpc::counting_range<TargetIndex>;
using allocator_type = typename vector_type::allocator_type;
using execution_policy = typename vector_type::execution_policy;
using size_type = typename vector_type::size_type;
using difference_type = typename vector_type::difference_type;
using reference = value_type;
using const_reference = value_type;
using pointer = TargetIndex*;
using const_pointer = TargetIndex const*;
using iterator = ::hpc::range_sum_iterator<TargetIndex, SourceIndex>;
using const_iterator = iterator;
constexpr range_sum() noexcept : m_vector()
{
}
template <class RangeSizes>
explicit range_sum(RangeSizes const& range_sizes)
{
assign_sizes(range_sizes);
}
constexpr range_sum(allocator_type const& allocator_in, execution_policy const& policy_in) noexcept
: m_vector(allocator_in, policy_in)
{
}
template <class RangeSizes>
range_sum(RangeSizes const& range_sizes, allocator_type const& allocator_in, execution_policy const& policy_in)
: m_vector(allocator_in, policy_in)
{
assign_sizes(range_sizes);
}
range_sum(range_sum&&) noexcept = default;
range_sum&
operator=(range_sum&&) = default;
range_sum(range_sum const&) = delete;
range_sum&
operator=(range_sum const&) = delete;
const_iterator
begin() const noexcept
{
return iterator(m_vector.data());
}
const_iterator
cbegin() const noexcept
{
return iterator(m_vector.data());
}
const_iterator
end() const noexcept
{
return iterator(m_vector.data() + size());
}
const_iterator
cend() const noexcept
{
return iterator(m_vector.data() + size());
}
const_reference
operator[](size_type i) const noexcept
{
return begin()[i];
}
bool
empty() const noexcept
{
return m_vector.size() <= 1;
}
size_type
size() const noexcept
{
return ::hpc::max(size_type(1), m_vector.size()) - size_type(1);
}
template <class RangeSizes>
void
assign_sizes(RangeSizes const& sizes)
{
m_vector.resize(size_type(sizes.size() + size_type(1)));
auto it = m_vector.begin();
auto const first = it;
++it;
auto const second = it;
auto const first_range = ::hpc::iterator_range<decltype(it)>(first, second);
using subrange_size_type = typename RangeSizes::value_type;
::hpc::fill(get_execution_policy(), first_range, TargetIndex(0));
auto const end = m_vector.end();
auto const rest = iterator_range<decltype(it)>(second, end);
auto const unop = [] HPC_HOST_DEVICE(subrange_size_type const i) { return TargetIndex(std::ptrdiff_t(i)); };
::hpc::transform_inclusive_scan(get_execution_policy(), sizes, rest, ::hpc::plus<TargetIndex>(), unop);
}
allocator_type
get_allocator() const noexcept
{
return m_vector.get_allocator();
}
execution_policy
get_execution_policy() const noexcept
{
return m_vector.get_execution_policy();
}
};
template <class T = std::ptrdiff_t, class S = std::ptrdiff_t>
using host_range_sum = range_sum<T, ::hpc::host_allocator<T>, ::hpc::host_policy, S>;
template <class T = std::ptrdiff_t, class S = std::ptrdiff_t>
using device_range_sum = range_sum<T, ::hpc::device_allocator<T>, ::hpc::device_policy, S>;
template <class T = std::ptrdiff_t, class S = std::ptrdiff_t>
using pinned_range_sum = range_sum<T, ::hpc::pinned_allocator<T>, ::hpc::host_policy, S>;
} // namespace hpc