|
| 1 | +//***************************************************************************** |
| 2 | +// Copyright (c) 2026, 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 | +//===----------------------------------------------------------------------===// |
| 30 | +/// |
| 31 | +/// \file |
| 32 | +/// This file defines functions of dpctl.tensor._tensor_sorting_impl |
| 33 | +/// extension. |
| 34 | +//===----------------------------------------------------------------------===// |
| 35 | + |
| 36 | +#include <cstddef> |
| 37 | +#include <cstdint> |
| 38 | +#include <type_traits> |
| 39 | +#include <utility> |
| 40 | +#include <vector> |
| 41 | + |
| 42 | +#include <sycl/sycl.hpp> |
| 43 | + |
| 44 | +#include "dpnp4pybind11.hpp" |
| 45 | +#include <pybind11/pybind11.h> |
| 46 | +#include <pybind11/stl.h> |
| 47 | + |
| 48 | +#include "utils/type_dispatch.hpp" |
| 49 | + |
| 50 | +#include "kernels/dpctl_tensor_types.hpp" |
| 51 | +#include "kernels/sorting/radix_sort.hpp" |
| 52 | +#include "kernels/sorting/sort_impl_fn_ptr_t.hpp" |
| 53 | + |
| 54 | +#include "py_argsort_common.hpp" |
| 55 | +#include "radix_argsort.hpp" |
| 56 | +#include "radix_sort_support.hpp" |
| 57 | + |
| 58 | +namespace dpctl::tensor::py_internal |
| 59 | +{ |
| 60 | + |
| 61 | +namespace py = pybind11; |
| 62 | +namespace td_ns = dpctl::tensor::type_dispatch; |
| 63 | +namespace impl_ns = dpctl::tensor::kernels::radix_sort_details; |
| 64 | + |
| 65 | +using dpctl::tensor::kernels::sort_contig_fn_ptr_t; |
| 66 | + |
| 67 | +static sort_contig_fn_ptr_t |
| 68 | + ascending_radix_argsort_contig_dispatch_table[td_ns::num_types] |
| 69 | + [td_ns::num_types]; |
| 70 | +static sort_contig_fn_ptr_t |
| 71 | + descending_radix_argsort_contig_dispatch_table[td_ns::num_types] |
| 72 | + [td_ns::num_types]; |
| 73 | + |
| 74 | +namespace |
| 75 | +{ |
| 76 | + |
| 77 | +template <bool is_ascending, typename T, typename I> |
| 78 | +sycl::event argsort_axis1_contig_caller(sycl::queue &q, |
| 79 | + std::size_t iter_nelems, |
| 80 | + std::size_t sort_nelems, |
| 81 | + const char *arg_cp, |
| 82 | + char *res_cp, |
| 83 | + ssize_t iter_arg_offset, |
| 84 | + ssize_t iter_res_offset, |
| 85 | + ssize_t sort_arg_offset, |
| 86 | + ssize_t sort_res_offset, |
| 87 | + const std::vector<sycl::event> &depends) |
| 88 | +{ |
| 89 | + using dpctl::tensor::kernels::radix_argsort_axis1_contig_impl; |
| 90 | + |
| 91 | + return radix_argsort_axis1_contig_impl<T, I>( |
| 92 | + q, is_ascending, iter_nelems, sort_nelems, arg_cp, res_cp, |
| 93 | + iter_arg_offset, iter_res_offset, sort_arg_offset, sort_res_offset, |
| 94 | + depends); |
| 95 | +} |
| 96 | + |
| 97 | +} // end of anonymous namespace |
| 98 | + |
| 99 | +template <typename fnT, typename argTy, typename IndexTy> |
| 100 | +struct AscendingRadixArgSortContigFactory |
| 101 | +{ |
| 102 | + fnT get() |
| 103 | + { |
| 104 | + if constexpr (RadixSortSupportVector<argTy>::is_defined && |
| 105 | + (std::is_same_v<IndexTy, std::int64_t> || |
| 106 | + std::is_same_v<IndexTy, std::int32_t>)) |
| 107 | + { |
| 108 | + return argsort_axis1_contig_caller< |
| 109 | + /*ascending*/ true, argTy, IndexTy>; |
| 110 | + } |
| 111 | + else { |
| 112 | + return nullptr; |
| 113 | + } |
| 114 | + } |
| 115 | +}; |
| 116 | + |
| 117 | +template <typename fnT, typename argTy, typename IndexTy> |
| 118 | +struct DescendingRadixArgSortContigFactory |
| 119 | +{ |
| 120 | + fnT get() |
| 121 | + { |
| 122 | + if constexpr (RadixSortSupportVector<argTy>::is_defined && |
| 123 | + (std::is_same_v<IndexTy, std::int64_t> || |
| 124 | + std::is_same_v<IndexTy, std::int32_t>)) |
| 125 | + { |
| 126 | + return argsort_axis1_contig_caller< |
| 127 | + /*ascending*/ false, argTy, IndexTy>; |
| 128 | + } |
| 129 | + else { |
| 130 | + return nullptr; |
| 131 | + } |
| 132 | + } |
| 133 | +}; |
| 134 | + |
| 135 | +void init_radix_argsort_dispatch_tables(void) |
| 136 | +{ |
| 137 | + using dpctl::tensor::kernels::sort_contig_fn_ptr_t; |
| 138 | + |
| 139 | + td_ns::DispatchTableBuilder<sort_contig_fn_ptr_t, |
| 140 | + AscendingRadixArgSortContigFactory, |
| 141 | + td_ns::num_types> |
| 142 | + dtb1; |
| 143 | + dtb1.populate_dispatch_table(ascending_radix_argsort_contig_dispatch_table); |
| 144 | + |
| 145 | + td_ns::DispatchTableBuilder<sort_contig_fn_ptr_t, |
| 146 | + DescendingRadixArgSortContigFactory, |
| 147 | + td_ns::num_types> |
| 148 | + dtb2; |
| 149 | + dtb2.populate_dispatch_table( |
| 150 | + descending_radix_argsort_contig_dispatch_table); |
| 151 | +} |
| 152 | + |
| 153 | +void init_radix_argsort_functions(py::module_ m) |
| 154 | +{ |
| 155 | + dpctl::tensor::py_internal::init_radix_argsort_dispatch_tables(); |
| 156 | + |
| 157 | + auto py_radix_argsort_ascending = |
| 158 | + [](const dpctl::tensor::usm_ndarray &src, |
| 159 | + const int trailing_dims_to_sort, |
| 160 | + const dpctl::tensor::usm_ndarray &dst, sycl::queue &exec_q, |
| 161 | + const std::vector<sycl::event> &depends) |
| 162 | + -> std::pair<sycl::event, sycl::event> { |
| 163 | + return dpctl::tensor::py_internal::py_argsort( |
| 164 | + src, trailing_dims_to_sort, dst, exec_q, depends, |
| 165 | + dpctl::tensor::py_internal:: |
| 166 | + ascending_radix_argsort_contig_dispatch_table); |
| 167 | + }; |
| 168 | + m.def("_radix_argsort_ascending", py_radix_argsort_ascending, |
| 169 | + py::arg("src"), py::arg("trailing_dims_to_sort"), py::arg("dst"), |
| 170 | + py::arg("sycl_queue"), py::arg("depends") = py::list()); |
| 171 | + |
| 172 | + auto py_radix_argsort_descending = |
| 173 | + [](const dpctl::tensor::usm_ndarray &src, |
| 174 | + const int trailing_dims_to_sort, |
| 175 | + const dpctl::tensor::usm_ndarray &dst, sycl::queue &exec_q, |
| 176 | + const std::vector<sycl::event> &depends) |
| 177 | + -> std::pair<sycl::event, sycl::event> { |
| 178 | + return dpctl::tensor::py_internal::py_argsort( |
| 179 | + src, trailing_dims_to_sort, dst, exec_q, depends, |
| 180 | + dpctl::tensor::py_internal:: |
| 181 | + descending_radix_argsort_contig_dispatch_table); |
| 182 | + }; |
| 183 | + m.def("_radix_argsort_descending", py_radix_argsort_descending, |
| 184 | + py::arg("src"), py::arg("trailing_dims_to_sort"), py::arg("dst"), |
| 185 | + py::arg("sycl_queue"), py::arg("depends") = py::list()); |
| 186 | + |
| 187 | + return; |
| 188 | +} |
| 189 | + |
| 190 | +} // namespace dpctl::tensor::py_internal |
0 commit comments