|
| 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 | +#include <complex> |
| 30 | +#include <cstddef> |
| 31 | +#include <cstdint> |
| 32 | +#include <type_traits> |
| 33 | +#include <vector> |
| 34 | + |
| 35 | +#include <oneapi/mkl.hpp> |
| 36 | +#include <sycl/sycl.hpp> |
| 37 | + |
| 38 | +#include "dpctl4pybind11.hpp" |
| 39 | + |
| 40 | +#include "common.hpp" |
| 41 | +#include "modf.hpp" |
| 42 | + |
| 43 | +// include a local copy of elementwise common header from dpctl tensor: |
| 44 | +// dpctl/tensor/libtensor/source/elementwise_functions/elementwise_functions.hpp |
| 45 | +// TODO: replace by including dpctl header once available |
| 46 | +#include "../elementwise_functions/elementwise_functions.hpp" |
| 47 | + |
| 48 | +#include "../elementwise_functions/common.hpp" |
| 49 | +#include "../elementwise_functions/type_dispatch_building.hpp" |
| 50 | + |
| 51 | +// dpctl tensor headers |
| 52 | +#include "utils/type_dispatch.hpp" |
| 53 | +#include "utils/type_utils.hpp" |
| 54 | + |
| 55 | +namespace dpnp::extensions::vm |
| 56 | +{ |
| 57 | +namespace py = pybind11; |
| 58 | +namespace py_int = dpnp::extensions::py_internal; |
| 59 | +namespace td_ns = dpctl::tensor::type_dispatch; |
| 60 | + |
| 61 | +namespace impl |
| 62 | +{ |
| 63 | +namespace ew_cmn_ns = dpnp::extensions::py_internal::elementwise_common; |
| 64 | +namespace mkl_vm = oneapi::mkl::vm; // OneMKL namespace with VM functions |
| 65 | +namespace td_int_ns = py_int::type_dispatch; |
| 66 | +namespace tu_ns = dpctl::tensor::type_utils; |
| 67 | + |
| 68 | +/** |
| 69 | + * @brief A factory to define pairs of supported types for which |
| 70 | + * MKL VM library provides support in oneapi::mkl::vm::modf<T> function. |
| 71 | + * |
| 72 | + * @tparam T Type of input vector `a` and of result vectors `y` and `z`. |
| 73 | + */ |
| 74 | +template <typename T> |
| 75 | +struct OutputType |
| 76 | +{ |
| 77 | + using table_type = |
| 78 | + std::disjunction<td_int_ns::TypeMapTwoResultsEntry<T, sycl::half>, |
| 79 | + td_int_ns::TypeMapTwoResultsEntry<T, float>, |
| 80 | + td_int_ns::TypeMapTwoResultsEntry<T, double>, |
| 81 | + td_int_ns::DefaultTwoResultsEntry<void>>; |
| 82 | + using value_type1 = typename table_type::result_type1; |
| 83 | + using value_type2 = typename table_type::result_type2; |
| 84 | +}; |
| 85 | + |
| 86 | +template <typename T> |
| 87 | +static sycl::event modf_contig_impl(sycl::queue &exec_q, |
| 88 | + std::size_t in_n, |
| 89 | + const char *in_a, |
| 90 | + char *out_y, |
| 91 | + char *out_z, |
| 92 | + const std::vector<sycl::event> &depends) |
| 93 | +{ |
| 94 | + tu_ns::validate_type_for_device<T>(exec_q); |
| 95 | + |
| 96 | + std::int64_t n = static_cast<std::int64_t>(in_n); |
| 97 | + const T *a = reinterpret_cast<const T *>(in_a); |
| 98 | + |
| 99 | + using fractT = typename OutputType<T>::value_type1; |
| 100 | + using intT = typename OutputType<T>::value_type2; |
| 101 | + fractT *y = reinterpret_cast<fractT *>(out_y); |
| 102 | + intT *z = reinterpret_cast<intT *>(out_z); |
| 103 | + |
| 104 | + return mkl_vm::modf(exec_q, |
| 105 | + n, // number of elements to be calculated |
| 106 | + a, // pointer `a` containing input vector of size n |
| 107 | + z, // pointer `z` to the output truncated integer values |
| 108 | + y, // pointer `y` to the output remaining fraction parts |
| 109 | + depends); |
| 110 | +} |
| 111 | + |
| 112 | +using ew_cmn_ns::unary_two_outputs_contig_impl_fn_ptr_t; |
| 113 | +using ew_cmn_ns::unary_two_outputs_strided_impl_fn_ptr_t; |
| 114 | + |
| 115 | +static std::pair<int, int> output_typeid_vector[td_ns::num_types]; |
| 116 | +static unary_two_outputs_contig_impl_fn_ptr_t |
| 117 | + contig_dispatch_vector[td_ns::num_types]; |
| 118 | + |
| 119 | +MACRO_POPULATE_DISPATCH_2OUTS_VECTORS(modf); |
| 120 | +} // namespace impl |
| 121 | + |
| 122 | +void init_modf(py::module_ m) |
| 123 | +{ |
| 124 | + using arrayT = dpctl::tensor::usm_ndarray; |
| 125 | + using event_vecT = std::vector<sycl::event>; |
| 126 | + |
| 127 | + impl::populate_dispatch_vectors(); |
| 128 | + using impl::contig_dispatch_vector; |
| 129 | + using impl::output_typeid_vector; |
| 130 | + |
| 131 | + auto modf_pyapi = [&](sycl::queue &exec_q, const arrayT &src, |
| 132 | + const arrayT &dst1, const arrayT &dst2, |
| 133 | + const event_vecT &depends = {}) { |
| 134 | + return py_int::py_unary_two_outputs_ufunc( |
| 135 | + src, dst1, dst2, exec_q, depends, output_typeid_vector, |
| 136 | + contig_dispatch_vector, |
| 137 | + // no support of strided implementation in OneMKL |
| 138 | + td_ns::NullPtrVector< |
| 139 | + impl::unary_two_outputs_strided_impl_fn_ptr_t>{}); |
| 140 | + }; |
| 141 | + m.def("_modf", modf_pyapi, |
| 142 | + "Call `modf` function from OneMKL VM library to compute " |
| 143 | + "a truncated integer value and the remaining fraction part " |
| 144 | + "for each vector elements", |
| 145 | + py::arg("sycl_queue"), py::arg("src"), py::arg("dst1"), |
| 146 | + py::arg("dst2"), py::arg("depends") = py::list()); |
| 147 | + |
| 148 | + auto modf_need_to_call_pyapi = [&](sycl::queue &exec_q, const arrayT &src, |
| 149 | + const arrayT &dst1, const arrayT &dst2) { |
| 150 | + return py_internal::need_to_call_unary_two_outputs_ufunc( |
| 151 | + exec_q, src, dst1, dst2, output_typeid_vector, |
| 152 | + contig_dispatch_vector); |
| 153 | + }; |
| 154 | + m.def("_mkl_modf_to_call", modf_need_to_call_pyapi, |
| 155 | + "Check input arguments to answer if `modf` function from " |
| 156 | + "OneMKL VM library can be used", |
| 157 | + py::arg("sycl_queue"), py::arg("src"), py::arg("dst1"), |
| 158 | + py::arg("dst2")); |
| 159 | +} |
| 160 | +} // namespace dpnp::extensions::vm |
0 commit comments