-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathround.hpp
More file actions
241 lines (209 loc) · 8.13 KB
/
round.hpp
File metadata and controls
241 lines (209 loc) · 8.13 KB
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
235
236
237
238
239
240
241
//*****************************************************************************
// Copyright (c) 2026, Intel Corporation
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// - Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// - Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// - Neither the name of the copyright holder nor the names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
//*****************************************************************************
//
//===---------------------------------------------------------------------===//
///
/// \file
/// This file defines kernels for elementwise evaluation of ROUND(x) function.
//===---------------------------------------------------------------------===//
#pragma once
#include <complex>
#include <cstddef>
#include <cstdint>
#include <type_traits>
#include <vector>
#include <sycl/sycl.hpp>
#include "vec_size_util.hpp"
#include "kernels/dpctl_tensor_types.hpp"
#include "kernels/elementwise_functions/common.hpp"
#include "utils/type_dispatch_building.hpp"
#include "utils/type_utils.hpp"
namespace dpctl::tensor::kernels::round
{
using dpctl::tensor::ssize_t;
namespace td_ns = dpctl::tensor::type_dispatch;
using dpctl::tensor::type_utils::is_complex;
template <typename argT, typename resT>
struct RoundFunctor
{
// is function constant for given argT
using is_constant = typename std::false_type;
// constant value, if constant
// constexpr resT constant_value = resT{};
// is function defined for sycl::vec
using supports_vec = typename std::false_type;
// do both argTy and resTy support sugroup store/load operation
using supports_sg_loadstore = typename std::negation<
std::disjunction<is_complex<resT>, is_complex<argT>>>;
resT operator()(const argT &in) const
{
if constexpr (std::is_integral_v<argT>) {
return in;
}
else if constexpr (is_complex<argT>::value) {
using realT = typename argT::value_type;
return resT{round_func<realT>(std::real(in)),
round_func<realT>(std::imag(in))};
}
else {
return round_func<argT>(in);
}
}
private:
template <typename T>
T round_func(const T &input) const
{
return sycl::rint(input);
}
};
template <typename argTy,
typename resTy = argTy,
std::uint8_t vec_sz = 4u,
std::uint8_t n_vecs = 2u,
bool enable_sg_loadstore = true>
using RoundContigFunctor =
elementwise_common::UnaryContigFunctor<argTy,
resTy,
RoundFunctor<argTy, resTy>,
vec_sz,
n_vecs,
enable_sg_loadstore>;
template <typename argTy, typename resTy, typename IndexerT>
using RoundStridedFunctor = elementwise_common::
UnaryStridedFunctor<argTy, resTy, IndexerT, RoundFunctor<argTy, resTy>>;
template <typename T>
struct RoundOutputType
{
using value_type = typename std::disjunction<
td_ns::TypeMapResultEntry<T, std::uint8_t>,
td_ns::TypeMapResultEntry<T, std::uint16_t>,
td_ns::TypeMapResultEntry<T, std::uint32_t>,
td_ns::TypeMapResultEntry<T, std::uint64_t>,
td_ns::TypeMapResultEntry<T, std::int8_t>,
td_ns::TypeMapResultEntry<T, std::int16_t>,
td_ns::TypeMapResultEntry<T, std::int32_t>,
td_ns::TypeMapResultEntry<T, std::int64_t>,
td_ns::TypeMapResultEntry<T, sycl::half>,
td_ns::TypeMapResultEntry<T, float>,
td_ns::TypeMapResultEntry<T, double>,
td_ns::TypeMapResultEntry<T, std::complex<float>>,
td_ns::TypeMapResultEntry<T, std::complex<double>>,
td_ns::DefaultResultEntry<void>>::result_type;
static constexpr bool is_defined = !std::is_same_v<value_type, void>;
};
namespace hyperparam_detail
{
namespace vsu_ns = dpctl::tensor::kernels::vec_size_utils;
using vsu_ns::ContigHyperparameterSetDefault;
using vsu_ns::UnaryContigHyperparameterSetEntry;
template <typename argTy>
struct RoundContigHyperparameterSet
{
using value_type =
typename std::disjunction<ContigHyperparameterSetDefault<4u, 2u>>;
constexpr static auto vec_sz = value_type::vec_sz;
constexpr static auto n_vecs = value_type::n_vecs;
};
} // end of namespace hyperparam_detail
template <typename T1, typename T2, std::uint8_t vec_sz, std::uint8_t n_vecs>
class round_contig_kernel;
template <typename argTy>
sycl::event round_contig_impl(sycl::queue &exec_q,
std::size_t nelems,
const char *arg_p,
char *res_p,
const std::vector<sycl::event> &depends = {})
{
using RoundHS = hyperparam_detail::RoundContigHyperparameterSet<argTy>;
static constexpr std::uint8_t vec_sz = RoundHS::vec_sz;
static constexpr std::uint8_t n_vecs = RoundHS::n_vecs;
return elementwise_common::unary_contig_impl<
argTy, RoundOutputType, RoundContigFunctor, round_contig_kernel, vec_sz,
n_vecs>(exec_q, nelems, arg_p, res_p, depends);
}
template <typename fnT, typename T>
struct RoundContigFactory
{
fnT get()
{
if constexpr (!RoundOutputType<T>::is_defined) {
fnT fn = nullptr;
return fn;
}
else {
fnT fn = round_contig_impl<T>;
return fn;
}
}
};
template <typename fnT, typename T>
struct RoundTypeMapFactory
{
/*! @brief get typeid for output type of sycl::round(T x) */
std::enable_if_t<std::is_same<fnT, int>::value, int> get()
{
using rT = typename RoundOutputType<T>::value_type;
return td_ns::GetTypeid<rT>{}.get();
}
};
template <typename T1, typename T2, typename T3>
class round_strided_kernel;
template <typename argTy>
sycl::event
round_strided_impl(sycl::queue &exec_q,
std::size_t nelems,
int nd,
const ssize_t *shape_and_strides,
const char *arg_p,
ssize_t arg_offset,
char *res_p,
ssize_t res_offset,
const std::vector<sycl::event> &depends,
const std::vector<sycl::event> &additional_depends)
{
return elementwise_common::unary_strided_impl<
argTy, RoundOutputType, RoundStridedFunctor, round_strided_kernel>(
exec_q, nelems, nd, shape_and_strides, arg_p, arg_offset, res_p,
res_offset, depends, additional_depends);
}
template <typename fnT, typename T>
struct RoundStridedFactory
{
fnT get()
{
if constexpr (!RoundOutputType<T>::is_defined) {
fnT fn = nullptr;
return fn;
}
else {
fnT fn = round_strided_impl<T>;
return fn;
}
}
};
} // namespace dpctl::tensor::kernels::round