DPNP C++ backend kernel library 0.20.0dev4
Data Parallel Extension for NumPy*
Loading...
Searching...
No Matches
isclose.hpp
1//*****************************************************************************
2// Copyright (c) 2025, Intel Corporation
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are met:
7// - Redistributions of source code must retain the above copyright notice,
8// this list of conditions and the following disclaimer.
9// - Redistributions in binary form must reproduce the above copyright notice,
10// this list of conditions and the following disclaimer in the documentation
11// and/or other materials provided with the distribution.
12// - Neither the name of the copyright holder nor the names of its contributors
13// may be used to endorse or promote products derived from this software
14// without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26// THE POSSIBILITY OF SUCH DAMAGE.
27//*****************************************************************************
28
29#pragma once
30
31#include <algorithm>
32#include <complex>
33#include <cstddef>
34#include <vector>
35
36#include <sycl/sycl.hpp>
37// dpctl tensor headers
38#include "kernels/alignment.hpp"
39#include "kernels/dpctl_tensor_types.hpp"
40#include "kernels/elementwise_functions/sycl_complex.hpp"
41#include "utils/offset_utils.hpp"
42#include "utils/sycl_utils.hpp"
43#include "utils/type_utils.hpp"
44
45namespace dpnp::kernels::isclose
46{
47
48template <typename T>
49inline bool isclose(const T a,
50 const T b,
51 const T rtol,
52 const T atol,
53 const bool equal_nan)
54{
55 static_assert(std::is_floating_point_v<T> || std::is_same_v<T, sycl::half>);
56
57 if (sycl::isfinite(a) && sycl::isfinite(b)) {
58 return sycl::fabs(a - b) <= atol + rtol * sycl::fabs(b);
59 }
60
61 if (sycl::isnan(a) && sycl::isnan(b)) {
62 return equal_nan;
63 }
64
65 return a == b;
66}
67
68template <typename T>
69inline bool isclose(const std::complex<T> a,
70 const std::complex<T> b,
71 const T rtol,
72 const T atol,
73 const bool equal_nan)
74{
75 const bool a_finite = sycl::isfinite(a.real()) && sycl::isfinite(a.imag());
76 const bool b_finite = sycl::isfinite(b.real()) && sycl::isfinite(b.imag());
77
78 if (a_finite && b_finite) {
79 return exprm_ns::abs(exprm_ns::complex<T>(a - b)) <=
80 atol + rtol * exprm_ns::abs(exprm_ns::complex<T>(b));
81 }
82
83 if (sycl::isnan(a.real()) && sycl::isnan(a.imag()) &&
84 sycl::isnan(b.real()) && sycl::isnan(b.imag())) {
85 return equal_nan;
86 }
87
88 return a == b;
89}
90
91template <typename T,
92 typename scT,
93 typename resTy,
94 typename ThreeOffsets_IndexerT>
96{
97private:
98 const T *a_ = nullptr;
99 const T *b_ = nullptr;
100 resTy *out_ = nullptr;
101 const ThreeOffsets_IndexerT three_offsets_indexer_;
102 const scT rtol_;
103 const scT atol_;
104 const bool equal_nan_;
105
106public:
108 const T *b,
109 resTy *out,
110 const ThreeOffsets_IndexerT &inps_res_indexer,
111 const scT rtol,
112 const scT atol,
113 const bool equal_nan)
114 : a_(a), b_(b), out_(out), three_offsets_indexer_(inps_res_indexer),
115 rtol_(rtol), atol_(atol), equal_nan_(equal_nan)
116 {
117 }
118
119 void operator()(sycl::id<1> wid) const
120 {
121 const auto &three_offsets_ = three_offsets_indexer_(wid.get(0));
122 const dpctl::tensor::ssize_t &inp1_offset =
123 three_offsets_.get_first_offset();
124 const dpctl::tensor::ssize_t &inp2_offset =
125 three_offsets_.get_second_offset();
126 const dpctl::tensor::ssize_t &out_offset =
127 three_offsets_.get_third_offset();
128
129 out_[out_offset] =
130 isclose(a_[inp1_offset], b_[inp2_offset], rtol_, atol_, equal_nan_);
131 }
132};
133
134template <typename T,
135 typename scT,
136 typename resTy,
137 std::uint8_t vec_sz = 4u,
138 std::uint8_t n_vecs = 2u,
139 bool enable_sg_loadstore = true>
141{
142private:
143 const T *a_ = nullptr;
144 const T *b_ = nullptr;
145 resTy *out_ = nullptr;
146 std::size_t nelems_;
147 const scT rtol_;
148 const scT atol_;
149 const bool equal_nan_;
150
151public:
153 const T *b,
154 resTy *out,
155 const std::size_t n_elems,
156 const scT rtol,
157 const scT atol,
158 const bool equal_nan)
159 : a_(a), b_(b), out_(out), nelems_(n_elems), rtol_(rtol), atol_(atol),
160 equal_nan_(equal_nan)
161 {
162 }
163
164 void operator()(sycl::nd_item<1> ndit) const
165 {
166 constexpr std::uint8_t elems_per_wi = n_vecs * vec_sz;
167 /* Each work-item processes vec_sz elements, contiguous in memory */
168 /* NOTE: work-group size must be divisible by sub-group size */
169
170 using dpctl::tensor::type_utils::is_complex_v;
171 if constexpr (enable_sg_loadstore && !is_complex_v<T>) {
172 auto sg = ndit.get_sub_group();
173 const std::uint16_t sgSize = sg.get_max_local_range()[0];
174 const std::size_t base =
175 elems_per_wi * (ndit.get_group(0) * ndit.get_local_range(0) +
176 sg.get_group_id()[0] * sgSize);
177
178 if (base + elems_per_wi * sgSize < nelems_) {
179 using dpctl::tensor::sycl_utils::sub_group_load;
180 using dpctl::tensor::sycl_utils::sub_group_store;
181#pragma unroll
182 for (std::uint8_t it = 0; it < elems_per_wi; it += vec_sz) {
183 const std::size_t offset = base + it * sgSize;
184 auto a_multi_ptr = sycl::address_space_cast<
185 sycl::access::address_space::global_space,
186 sycl::access::decorated::yes>(&a_[offset]);
187 auto b_multi_ptr = sycl::address_space_cast<
188 sycl::access::address_space::global_space,
189 sycl::access::decorated::yes>(&b_[offset]);
190 auto out_multi_ptr = sycl::address_space_cast<
191 sycl::access::address_space::global_space,
192 sycl::access::decorated::yes>(&out_[offset]);
193
194 const sycl::vec<T, vec_sz> a_vec =
195 sub_group_load<vec_sz>(sg, a_multi_ptr);
196 const sycl::vec<T, vec_sz> b_vec =
197 sub_group_load<vec_sz>(sg, b_multi_ptr);
198
199 sycl::vec<resTy, vec_sz> res_vec;
200#pragma unroll
201 for (std::uint8_t vec_id = 0; vec_id < vec_sz; ++vec_id) {
202 res_vec[vec_id] = isclose(a_vec[vec_id], b_vec[vec_id],
203 rtol_, atol_, equal_nan_);
204 }
205 sub_group_store<vec_sz>(sg, res_vec, out_multi_ptr);
206 }
207 }
208 else {
209 const std::size_t lane_id = sg.get_local_id()[0];
210 for (std::size_t k = base + lane_id; k < nelems_; k += sgSize) {
211 out_[k] = isclose(a_[k], b_[k], rtol_, atol_, equal_nan_);
212 }
213 }
214 }
215 else {
216 const std::uint16_t sgSize =
217 ndit.get_sub_group().get_local_range()[0];
218 const std::size_t gid = ndit.get_global_linear_id();
219 const std::uint16_t elems_per_sg = sgSize * elems_per_wi;
220
221 const std::size_t start =
222 (gid / sgSize) * (elems_per_sg - sgSize) + gid;
223 const std::size_t end = std::min(nelems_, start + elems_per_sg);
224 for (std::size_t offset = start; offset < end; offset += sgSize) {
225 out_[offset] =
226 isclose(a_[offset], b_[offset], rtol_, atol_, equal_nan_);
227 }
228 }
229 }
230};
231
232template <typename T, typename scT>
233sycl::event
234 isclose_strided_scalar_impl(sycl::queue &exec_q,
235 const int nd,
236 std::size_t nelems,
237 const dpctl::tensor::ssize_t *shape_strides,
238 const scT rtol,
239 const scT atol,
240 const bool equal_nan,
241 const char *a_cp,
242 const dpctl::tensor::ssize_t a_offset,
243 const char *b_cp,
244 const dpctl::tensor::ssize_t b_offset,
245 char *out_cp,
246 const dpctl::tensor::ssize_t out_offset,
247 const std::vector<sycl::event> &depends)
248{
249 dpctl::tensor::type_utils::validate_type_for_device<T>(exec_q);
250
251 const T *a_tp = reinterpret_cast<const T *>(a_cp);
252 const T *b_tp = reinterpret_cast<const T *>(b_cp);
253
254 using resTy = bool;
255 resTy *out_tp = reinterpret_cast<resTy *>(out_cp);
256
257 using IndexerT =
258 typename dpctl::tensor::offset_utils::ThreeOffsets_StridedIndexer;
259 const IndexerT indexer{nd, a_offset, b_offset, out_offset, shape_strides};
260
261 sycl::event comp_ev = exec_q.submit([&](sycl::handler &cgh) {
262 cgh.depends_on(depends);
263
264 using IsCloseFunc =
265 IsCloseStridedScalarFunctor<T, scT, resTy, IndexerT>;
266 cgh.parallel_for<IsCloseFunc>(
267 {nelems},
268 IsCloseFunc(a_tp, b_tp, out_tp, indexer, rtol, atol, equal_nan));
269 });
270 return comp_ev;
271}
272
273template <typename T,
274 typename scT,
275 std::uint8_t vec_sz = 4u,
276 std::uint8_t n_vecs = 2u>
277sycl::event
278 isclose_contig_scalar_impl(sycl::queue &exec_q,
279 std::size_t nelems,
280 const scT rtol,
281 const scT atol,
282 const bool equal_nan,
283 const char *a_cp,
284 const char *b_cp,
285 char *out_cp,
286 const std::vector<sycl::event> &depends = {})
287{
288 constexpr std::uint8_t elems_per_wi = n_vecs * vec_sz;
289 const std::size_t n_work_items_needed = nelems / elems_per_wi;
290 const std::size_t empirical_threshold = std::size_t(1) << 21;
291 const std::size_t lws = (n_work_items_needed <= empirical_threshold)
292 ? std::size_t(128)
293 : std::size_t(256);
294
295 const std::size_t n_groups =
296 ((nelems + lws * elems_per_wi - 1) / (lws * elems_per_wi));
297 const auto gws_range = sycl::range<1>(n_groups * lws);
298 const auto lws_range = sycl::range<1>(lws);
299
300 const T *a_tp = reinterpret_cast<const T *>(a_cp);
301 const T *b_tp = reinterpret_cast<const T *>(b_cp);
302
303 using resTy = bool;
304 resTy *out_tp = reinterpret_cast<resTy *>(out_cp);
305
306 sycl::event comp_ev = exec_q.submit([&](sycl::handler &cgh) {
307 cgh.depends_on(depends);
308
309 using dpctl::tensor::kernels::alignment_utils::is_aligned;
310 using dpctl::tensor::kernels::alignment_utils::required_alignment;
311 if (is_aligned<required_alignment>(a_tp) &&
312 is_aligned<required_alignment>(b_tp) &&
313 is_aligned<required_alignment>(out_tp)) {
314 constexpr bool enable_sg_loadstore = true;
315 using IsCloseFunc =
316 IsCloseContigScalarFunctor<T, scT, resTy, vec_sz, n_vecs,
317 enable_sg_loadstore>;
318
319 cgh.parallel_for<IsCloseFunc>(
320 sycl::nd_range<1>(gws_range, lws_range),
321 IsCloseFunc(a_tp, b_tp, out_tp, nelems, rtol, atol, equal_nan));
322 }
323 else {
324 constexpr bool disable_sg_loadstore = false;
325 using IsCloseFunc =
326 IsCloseContigScalarFunctor<T, scT, resTy, vec_sz, n_vecs,
327 disable_sg_loadstore>;
328
329 cgh.parallel_for<IsCloseFunc>(
330 sycl::nd_range<1>(gws_range, lws_range),
331 IsCloseFunc(a_tp, b_tp, out_tp, nelems, rtol, atol, equal_nan));
332 }
333 });
334
335 return comp_ev;
336}
337
338} // namespace dpnp::kernels::isclose