|
| 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 <type_traits> |
| 31 | +#include <vector> |
| 32 | + |
| 33 | +#include <sycl/sycl.hpp> |
| 34 | + |
| 35 | +#include "dpctl4pybind11.hpp" |
| 36 | + |
| 37 | +#include "divmod.hpp" |
| 38 | +#include "kernels/elementwise_functions/divmod.hpp" |
| 39 | +#include "populate.hpp" |
| 40 | + |
| 41 | +// include a local copy of elementwise common header from dpctl tensor: |
| 42 | +// dpctl/tensor/libtensor/source/elementwise_functions/elementwise_functions.hpp |
| 43 | +// TODO: replace by including dpctl header once available |
| 44 | +#include "../../elementwise_functions/elementwise_functions.hpp" |
| 45 | + |
| 46 | +#include "../../elementwise_functions/common.hpp" |
| 47 | +#include "../../elementwise_functions/type_dispatch_building.hpp" |
| 48 | + |
| 49 | +// utils extension header |
| 50 | +#include "ext/common.hpp" |
| 51 | + |
| 52 | +// dpctl tensor headers |
| 53 | +#include "kernels/elementwise_functions/common.hpp" |
| 54 | +#include "utils/type_dispatch.hpp" |
| 55 | + |
| 56 | +namespace dpnp::extensions::ufunc |
| 57 | +{ |
| 58 | +namespace py = pybind11; |
| 59 | +namespace py_int = dpnp::extensions::py_internal; |
| 60 | + |
| 61 | +namespace impl |
| 62 | +{ |
| 63 | +namespace ew_cmn_ns = dpnp::extensions::py_internal::elementwise_common; |
| 64 | +namespace td_int_ns = py_int::type_dispatch; |
| 65 | +namespace td_ns = dpctl::tensor::type_dispatch; |
| 66 | + |
| 67 | +using dpnp::kernels::divmod::DivmodFunctor; |
| 68 | + |
| 69 | +template <typename T1, typename T2> |
| 70 | +struct OutputType |
| 71 | +{ |
| 72 | + using table_type = typename std::disjunction< // disjunction is C++17 |
| 73 | + // feature, supported by DPC++ |
| 74 | + td_int_ns:: |
| 75 | + BinaryTypeMapTwoResultsEntry<T1, std::uint8_t, T2, std::uint8_t>, |
| 76 | + td_int_ns:: |
| 77 | + BinaryTypeMapTwoResultsEntry<T1, std::int8_t, T2, std::int8_t>, |
| 78 | + td_int_ns:: |
| 79 | + BinaryTypeMapTwoResultsEntry<T1, std::uint16_t, T2, std::uint16_t>, |
| 80 | + td_int_ns:: |
| 81 | + BinaryTypeMapTwoResultsEntry<T1, std::int16_t, T2, std::int16_t>, |
| 82 | + td_int_ns:: |
| 83 | + BinaryTypeMapTwoResultsEntry<T1, std::uint32_t, T2, std::uint32_t>, |
| 84 | + td_int_ns:: |
| 85 | + BinaryTypeMapTwoResultsEntry<T1, std::int32_t, T2, std::int32_t>, |
| 86 | + td_int_ns:: |
| 87 | + BinaryTypeMapTwoResultsEntry<T1, std::uint64_t, T2, std::uint64_t>, |
| 88 | + td_int_ns:: |
| 89 | + BinaryTypeMapTwoResultsEntry<T1, std::int64_t, T2, std::int64_t>, |
| 90 | + td_int_ns::BinaryTypeMapTwoResultsEntry<T1, sycl::half, T2, sycl::half>, |
| 91 | + td_int_ns::BinaryTypeMapTwoResultsEntry<T1, float, T2, float>, |
| 92 | + td_int_ns::BinaryTypeMapTwoResultsEntry<T1, double, T2, double>, |
| 93 | + td_int_ns::DefaultTwoResultsEntry<void>>; |
| 94 | + using value_type1 = typename table_type::result_type1; |
| 95 | + using value_type2 = typename table_type::result_type2; |
| 96 | +}; |
| 97 | + |
| 98 | +template <typename argTy1, |
| 99 | + typename argTy2, |
| 100 | + typename resTy1, |
| 101 | + typename resTy2, |
| 102 | + unsigned int vec_sz = 4, |
| 103 | + unsigned int n_vecs = 2, |
| 104 | + bool enable_sg_loadstore = true> |
| 105 | +using ContigFunctor = ew_cmn_ns::BinaryTwoOutputsContigFunctor< |
| 106 | + argTy1, |
| 107 | + argTy2, |
| 108 | + resTy1, |
| 109 | + resTy2, |
| 110 | + DivmodFunctor<argTy1, argTy2, resTy1, resTy2>, |
| 111 | + vec_sz, |
| 112 | + n_vecs, |
| 113 | + enable_sg_loadstore>; |
| 114 | + |
| 115 | +template <typename argTy1, |
| 116 | + typename argTy2, |
| 117 | + typename resTy1, |
| 118 | + typename resTy2, |
| 119 | + typename IndexerT> |
| 120 | +using StridedFunctor = ew_cmn_ns::BinaryTwoOutputsStridedFunctor< |
| 121 | + argTy1, |
| 122 | + argTy2, |
| 123 | + resTy1, |
| 124 | + resTy2, |
| 125 | + IndexerT, |
| 126 | + DivmodFunctor<argTy1, argTy2, resTy1, resTy2>>; |
| 127 | + |
| 128 | +using ew_cmn_ns::binary_two_outputs_contig_impl_fn_ptr_t; |
| 129 | +using ew_cmn_ns::binary_two_outputs_strided_impl_fn_ptr_t; |
| 130 | + |
| 131 | +static binary_two_outputs_contig_impl_fn_ptr_t |
| 132 | + divmod_contig_dispatch_table[td_ns::num_types][td_ns::num_types]; |
| 133 | +static std::pair<int, int> divmod_output_typeid_table[td_ns::num_types] |
| 134 | + [td_ns::num_types]; |
| 135 | +static binary_two_outputs_strided_impl_fn_ptr_t |
| 136 | + divmod_strided_dispatch_table[td_ns::num_types][td_ns::num_types]; |
| 137 | + |
| 138 | +MACRO_POPULATE_DISPATCH_2OUTS_TABLES(divmod); |
| 139 | +} // namespace impl |
| 140 | + |
| 141 | +void init_divmod(py::module_ m) |
| 142 | +{ |
| 143 | + using arrayT = dpctl::tensor::usm_ndarray; |
| 144 | + using event_vecT = std::vector<sycl::event>; |
| 145 | + { |
| 146 | + impl::populate_divmod_dispatch_tables(); |
| 147 | + using impl::divmod_contig_dispatch_table; |
| 148 | + using impl::divmod_output_typeid_table; |
| 149 | + using impl::divmod_strided_dispatch_table; |
| 150 | + |
| 151 | + auto divmod_pyapi = [&](const arrayT &src1, const arrayT &src2, |
| 152 | + const arrayT &dst1, const arrayT &dst2, |
| 153 | + sycl::queue &exec_q, |
| 154 | + const event_vecT &depends = {}) { |
| 155 | + return py_int::py_binary_two_outputs_ufunc( |
| 156 | + src1, src2, dst1, dst2, exec_q, depends, |
| 157 | + divmod_output_typeid_table, divmod_contig_dispatch_table, |
| 158 | + divmod_strided_dispatch_table); |
| 159 | + }; |
| 160 | + m.def("_divmod", divmod_pyapi, "", py::arg("src1"), py::arg("src2"), |
| 161 | + py::arg("dst1"), py::arg("dst2"), py::arg("sycl_queue"), |
| 162 | + py::arg("depends") = py::list()); |
| 163 | + |
| 164 | + auto divmod_result_type_pyapi = [&](const py::dtype &dtype1, |
| 165 | + const py::dtype &dtype2) { |
| 166 | + return py_int::py_binary_two_outputs_ufunc_result_type( |
| 167 | + dtype1, dtype2, divmod_output_typeid_table); |
| 168 | + }; |
| 169 | + m.def("_divmod_result_type", divmod_result_type_pyapi); |
| 170 | + } |
| 171 | +} |
| 172 | +} // namespace dpnp::extensions::ufunc |
0 commit comments