-
Notifications
You must be signed in to change notification settings - Fork 13
/
hpc_numeric.hpp
198 lines (174 loc) · 5.53 KB
/
hpc_numeric.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
#pragma once
#include <hpc_algorithm.hpp>
#include <hpc_functional.hpp>
#include <hpc_range.hpp>
#ifdef HPC_CUDA
#include <thrust/execution_policy.h>
#include <thrust/transform_scan.h>
#endif
namespace hpc {
namespace impl {
template <class T>
class pi;
template <>
class pi<float>
{
public:
static constexpr float value = 3.14159265f;
};
template <>
class pi<double>
{
public:
static constexpr double value = 3.141592653589793238;
};
} // namespace impl
template <class T>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr T
pi() noexcept
{
return ::hpc::impl::pi<T>::value;
}
template <class Range, class T>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE T
reduce(local_policy policy, Range const& range, T init) noexcept
{
using input_value_type = typename Range::value_type;
auto const unop = [](input_value_type const i) { return T(i); };
return transform_reduce(policy, range, init, plus<T>(), unop);
}
template <class Range, class T>
HPC_NOINLINE T
reduce(serial_policy policy, Range const& range, T init)
{
using input_value_type = typename Range::value_type;
auto const unop = [](input_value_type const i) { return T(i); };
return transform_reduce(policy, range, init, plus<T>(), unop);
}
#ifdef HPC_CUDA
template <class Range, class T>
HPC_NOINLINE T
reduce(cuda_policy policy, Range const& range, T init)
{
using input_value_type = typename Range::value_type;
auto const unop = [] HPC_DEVICE(input_value_type const i) { return T(i); };
return hpc::transform_reduce(policy, range, init, hpc::plus<T>(), unop);
}
#endif
template <class InputRange, class OutputRange, class BinaryOp, class UnaryOp>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE void
transform_inclusive_scan(
local_policy,
InputRange const& input,
OutputRange& output,
BinaryOp binary_op,
UnaryOp unary_op) noexcept
{
auto first = input.begin();
auto last = input.end();
auto d_first = output.begin();
if (first == last) return;
auto sum = unary_op(*first);
*d_first = sum;
while (++first != last) {
sum = binary_op(std::move(sum), unary_op(*first));
*(++d_first) = sum;
}
}
template <class InputRange, class OutputRange, class BinaryOp, class UnaryOp>
HPC_NOINLINE void
transform_inclusive_scan(
serial_policy,
InputRange const& input,
OutputRange& output,
BinaryOp binary_op,
UnaryOp unary_op)
{
auto first = input.begin();
auto last = input.end();
auto d_first = output.begin();
if (first == last) return;
auto sum = unary_op(*first);
*d_first = sum;
while (++first != last) {
sum = binary_op(std::move(sum), unary_op(*first));
*(++d_first) = sum;
}
}
#ifdef HPC_CUDA
namespace impl {
template <class InputIterator, class OutputIterator, class BinaryOp, class UnaryOp>
HPC_NOINLINE void
transform_inclusive_scan(
cuda_policy,
InputIterator first,
InputIterator last,
OutputIterator d_first,
BinaryOp binary_op,
UnaryOp unary_op)
{
thrust::transform_inclusive_scan(thrust::device, first, last, d_first, unary_op, binary_op);
}
template <class T, class Index, class UnaryOp>
HPC_NOINLINE void
transform_inclusive_scan(
cuda_policy,
::hpc::counting_iterator<Index> first,
::hpc::counting_iterator<Index> last,
::hpc::pointer_iterator<T, Index> d_first,
::hpc::plus<T>,
UnaryOp unary_op)
{
::hpc::counting_iterator<Index> old_zero(0);
::thrust::counting_iterator<int> new_first(int(first - old_zero));
::thrust::counting_iterator<int> new_last(int(last - old_zero));
T* new_d_first = &(*d_first);
thrust::transform_inclusive_scan(thrust::device, new_first, new_last, new_d_first, unary_op, thrust::plus<T>());
}
template <class TStored, class TResult, class Index, class UnaryOp>
HPC_NOINLINE void
transform_inclusive_scan(
cuda_policy,
::hpc::pointer_iterator<TStored, Index> first,
::hpc::pointer_iterator<TStored, Index> last,
::hpc::pointer_iterator<TResult, Index> d_first,
::hpc::plus<TResult>,
UnaryOp unary_op)
{
auto const size = std::ptrdiff_t(last - first);
TStored* const new_first = &(*first);
TStored* const new_last = new_first + size;
TResult* const new_d_first = &(*d_first);
thrust::transform_inclusive_scan(thrust::device, new_first, new_last, new_d_first, unary_op, thrust::plus<TResult>());
}
} // namespace impl
template <class InputRange, class OutputRange, class BinaryOp, class UnaryOp>
HPC_NOINLINE void
transform_inclusive_scan(
cuda_policy policy,
InputRange const& input,
OutputRange& output,
BinaryOp binary_op,
UnaryOp unary_op)
{
::hpc::impl::transform_inclusive_scan(policy, input.begin(), input.end(), output.begin(), binary_op, unary_op);
}
#endif
template <class ExecutionPolicy, class InputRange, class OutputRange>
void
offset_scan(ExecutionPolicy policy, InputRange const& input, OutputRange& output)
{
auto it = output.begin();
auto const first = it;
++it;
auto const second = it;
auto const first_range = ::hpc::iterator_range<decltype(it)>(first, second);
using input_value_type = typename InputRange::value_type;
using output_value_type = typename OutputRange::value_type;
::hpc::fill(policy, first_range, output_value_type(0));
auto const end = output.end();
auto const rest = iterator_range<decltype(it)>(second, end);
::hpc::transform_inclusive_scan(
policy, input, rest, ::hpc::plus<output_value_type>(), ::hpc::cast<output_value_type, input_value_type>());
}
} // namespace hpc