|
| 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 | +// |
| 13 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 14 | +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 16 | +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 17 | +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 | +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 | +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 | +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 | +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 | +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 23 | +// THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | +//***************************************************************************** |
| 25 | + |
| 26 | +#include "kaiser.hpp" |
| 27 | +#include "common.hpp" |
| 28 | + |
| 29 | +#include "utils/output_validation.hpp" |
| 30 | +#include "utils/type_dispatch.hpp" |
| 31 | +#include "utils/type_utils.hpp" |
| 32 | + |
| 33 | +#include <sycl/sycl.hpp> |
| 34 | + |
| 35 | +/** |
| 36 | + * Version of SYCL DPC++ 2025.1 compiler where an issue with |
| 37 | + * sycl::ext::intel::math::cyl_bessel_i0(x) is fully resolved. |
| 38 | + */ |
| 39 | +#ifndef __SYCL_COMPILER_BESSEL_I0_SUPPORT |
| 40 | +#define __SYCL_COMPILER_BESSEL_I0_SUPPORT 20241208L |
| 41 | +#endif |
| 42 | + |
| 43 | +#if __SYCL_COMPILER_VERSION >= __SYCL_COMPILER_BESSEL_I0_SUPPORT |
| 44 | +#include <sycl/ext/intel/math.hpp> |
| 45 | +#endif |
| 46 | + |
| 47 | +#include "../kernels/elementwise_functions/i0.hpp" |
| 48 | + |
| 49 | +namespace dpnp::extensions::window |
| 50 | +{ |
| 51 | +namespace dpctl_td_ns = dpctl::tensor::type_dispatch; |
| 52 | + |
| 53 | +typedef sycl::event (*kaiser_fn_ptr_t)(sycl::queue &, |
| 54 | + char *, |
| 55 | + const std::size_t, |
| 56 | + const py::object &, |
| 57 | + const std::vector<sycl::event> &); |
| 58 | + |
| 59 | +static kaiser_fn_ptr_t kaiser_dispatch_vector[dpctl_td_ns::num_types]; |
| 60 | + |
| 61 | +template <typename T> |
| 62 | +class KaiserFunctor |
| 63 | +{ |
| 64 | +private: |
| 65 | + T *data = nullptr; |
| 66 | + const std::size_t N; |
| 67 | + const T beta; |
| 68 | + |
| 69 | +public: |
| 70 | + KaiserFunctor(T *data, const std::size_t N, const T beta) |
| 71 | + : data(data), N(N), beta(beta) |
| 72 | + { |
| 73 | + } |
| 74 | + |
| 75 | + void operator()(sycl::id<1> id) const |
| 76 | + { |
| 77 | +#if __SYCL_COMPILER_VERSION >= __SYCL_COMPILER_BESSEL_I0_SUPPORT |
| 78 | + using sycl::ext::intel::math::cyl_bessel_i0; |
| 79 | +#else |
| 80 | + using dpnp::kernels::i0::impl::cyl_bessel_i0; |
| 81 | +#endif |
| 82 | + |
| 83 | + const auto i = id.get(0); |
| 84 | + const T alpha = (N - 1) / T(2); |
| 85 | + const T tmp = (i - alpha) / alpha; |
| 86 | + data[i] = cyl_bessel_i0(beta * sycl::sqrt(1 - tmp * tmp)) / |
| 87 | + cyl_bessel_i0(beta); |
| 88 | + } |
| 89 | +}; |
| 90 | + |
| 91 | +template <typename T, template <typename> class Functor> |
| 92 | +sycl::event kaiser_impl(sycl::queue &q, |
| 93 | + char *result, |
| 94 | + const std::size_t nelems, |
| 95 | + const py::object &py_beta, |
| 96 | + const std::vector<sycl::event> &depends) |
| 97 | +{ |
| 98 | + dpctl::tensor::type_utils::validate_type_for_device<T>(q); |
| 99 | + |
| 100 | + T *res = reinterpret_cast<T *>(result); |
| 101 | + const T beta = py::cast<const T>(py_beta); |
| 102 | + |
| 103 | + sycl::event kaiser_ev = q.submit([&](sycl::handler &cgh) { |
| 104 | + cgh.depends_on(depends); |
| 105 | + |
| 106 | + using KaiserKernel = Functor<T>; |
| 107 | + cgh.parallel_for<KaiserKernel>(sycl::range<1>(nelems), |
| 108 | + KaiserKernel(res, nelems, beta)); |
| 109 | + }); |
| 110 | + |
| 111 | + return kaiser_ev; |
| 112 | +} |
| 113 | + |
| 114 | +template <typename fnT, typename T> |
| 115 | +struct KaiserFactory |
| 116 | +{ |
| 117 | + fnT get() |
| 118 | + { |
| 119 | + if constexpr (std::is_floating_point_v<T>) { |
| 120 | + return kaiser_impl<T, KaiserFunctor>; |
| 121 | + } |
| 122 | + else { |
| 123 | + return nullptr; |
| 124 | + } |
| 125 | + } |
| 126 | +}; |
| 127 | + |
| 128 | +std::pair<sycl::event, sycl::event> |
| 129 | + py_kaiser(sycl::queue &exec_q, |
| 130 | + const py::object &py_beta, |
| 131 | + const dpctl::tensor::usm_ndarray &result, |
| 132 | + const std::vector<sycl::event> &depends) |
| 133 | +{ |
| 134 | + dpctl::tensor::validation::CheckWritable::throw_if_not_writable(result); |
| 135 | + |
| 136 | + int nd = result.get_ndim(); |
| 137 | + if (nd != 1) { |
| 138 | + throw py::value_error("Array should be 1d"); |
| 139 | + } |
| 140 | + |
| 141 | + if (!dpctl::utils::queues_are_compatible(exec_q, {result.get_queue()})) { |
| 142 | + throw py::value_error( |
| 143 | + "Execution queue is not compatible with allocation queue."); |
| 144 | + } |
| 145 | + |
| 146 | + const bool is_result_c_contig = result.is_c_contiguous(); |
| 147 | + if (!is_result_c_contig) { |
| 148 | + throw py::value_error("The result input array is not c-contiguous."); |
| 149 | + } |
| 150 | + |
| 151 | + size_t nelems = result.get_size(); |
| 152 | + if (nelems == 0) { |
| 153 | + return std::make_pair(sycl::event{}, sycl::event{}); |
| 154 | + } |
| 155 | + |
| 156 | + int result_typenum = result.get_typenum(); |
| 157 | + auto array_types = dpctl_td_ns::usm_ndarray_types(); |
| 158 | + int result_type_id = array_types.typenum_to_lookup_id(result_typenum); |
| 159 | + auto fn = kaiser_dispatch_vector[result_type_id]; |
| 160 | + |
| 161 | + if (fn == nullptr) { |
| 162 | + throw std::runtime_error("Type of given array is not supported"); |
| 163 | + } |
| 164 | + |
| 165 | + char *result_typeless_ptr = result.get_data(); |
| 166 | + sycl::event kaiser_ev = |
| 167 | + fn(exec_q, result_typeless_ptr, nelems, py_beta, depends); |
| 168 | + sycl::event args_ev = |
| 169 | + dpctl::utils::keep_args_alive(exec_q, {result}, {kaiser_ev}); |
| 170 | + |
| 171 | + return std::make_pair(args_ev, kaiser_ev); |
| 172 | +} |
| 173 | + |
| 174 | +void init_kaiser_dispatch_vectors() |
| 175 | +{ |
| 176 | + init_window_dispatch_vectors<kaiser_fn_ptr_t, KaiserFactory>( |
| 177 | + kaiser_dispatch_vector); |
| 178 | +} |
| 179 | + |
| 180 | +} // namespace dpnp::extensions::window |
0 commit comments