|
| 1 | +//=== isin.hpp - ---*-C++-*--/===// |
| 2 | +// Implementation of searching for membership in sorted array |
| 3 | +// |
| 4 | +// Data Parallel Control (dpctl) |
| 5 | +// |
| 6 | +// Copyright 2020-2025 Intel Corporation |
| 7 | +// |
| 8 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | +// you may not use this file except in compliance with the License. |
| 10 | +// You may obtain a copy of the License at |
| 11 | +// |
| 12 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | +// |
| 14 | +// Unless required by applicable law or agreed to in writing, software |
| 15 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | +// See the License for the specific language governing permissions and |
| 18 | +// limitations under the License. |
| 19 | +// |
| 20 | +//===----------------------------------------------------------------------===// |
| 21 | +/// |
| 22 | +/// \file |
| 23 | +/// This file defines kernels for tensor membership operations. |
| 24 | +//===----------------------------------------------------------------------===// |
| 25 | + |
| 26 | +#pragma once |
| 27 | + |
| 28 | +#include <algorithm> |
| 29 | +#include <cstddef> |
| 30 | +#include <cstdint> |
| 31 | +#include <sycl/sycl.hpp> |
| 32 | +#include <vector> |
| 33 | + |
| 34 | +#include "kernels/dpctl_tensor_types.hpp" |
| 35 | +#include "kernels/sorting/search_sorted_detail.hpp" |
| 36 | +#include "utils/offset_utils.hpp" |
| 37 | + |
| 38 | +namespace dpctl |
| 39 | +{ |
| 40 | +namespace tensor |
| 41 | +{ |
| 42 | +namespace kernels |
| 43 | +{ |
| 44 | + |
| 45 | +using dpctl::tensor::ssize_t; |
| 46 | + |
| 47 | +template <typename T, |
| 48 | + typename HayIndexerT, |
| 49 | + typename NeedlesIndexerT, |
| 50 | + typename OutIndexerT, |
| 51 | + typename Compare> |
| 52 | +struct IsinFunctor |
| 53 | +{ |
| 54 | +private: |
| 55 | + bool invert; |
| 56 | + const T *hay_tp; |
| 57 | + const T *needles_tp; |
| 58 | + bool *out_tp; |
| 59 | + std::size_t hay_nelems; |
| 60 | + HayIndexerT hay_indexer; |
| 61 | + NeedlesIndexerT needles_indexer; |
| 62 | + OutIndexerT out_indexer; |
| 63 | + |
| 64 | +public: |
| 65 | + IsinFunctor(const bool invert_, |
| 66 | + const T *hay_, |
| 67 | + const T *needles_, |
| 68 | + bool *out_, |
| 69 | + const std::size_t hay_nelems_, |
| 70 | + const HayIndexerT &hay_indexer_, |
| 71 | + const NeedlesIndexerT &needles_indexer_, |
| 72 | + const OutIndexerT &out_indexer_) |
| 73 | + : invert(invert_), hay_tp(hay_), needles_tp(needles_), out_tp(out_), |
| 74 | + hay_nelems(hay_nelems_), hay_indexer(hay_indexer_), |
| 75 | + needles_indexer(needles_indexer_), out_indexer(out_indexer_) |
| 76 | + { |
| 77 | + } |
| 78 | + |
| 79 | + void operator()(sycl::id<1> id) const |
| 80 | + { |
| 81 | + const Compare comp{}; |
| 82 | + |
| 83 | + const std::size_t i = id[0]; |
| 84 | + const T needle_v = needles_tp[needles_indexer(i)]; |
| 85 | + |
| 86 | + // position of the needle_v in the hay array |
| 87 | + std::size_t pos{}; |
| 88 | + |
| 89 | + static constexpr std::size_t zero(0); |
| 90 | + // search in hay in left-closed interval, give `pos` such that |
| 91 | + // hay[pos - 1] < needle_v <= hay[pos] |
| 92 | + |
| 93 | + // lower_bound returns the first pos such that bool(hay[pos] < |
| 94 | + // needle_v) is false, i.e. needle_v <= hay[pos] |
| 95 | + pos = static_cast<std::size_t>( |
| 96 | + search_sorted_detail::lower_bound_indexed_impl( |
| 97 | + hay_tp, zero, hay_nelems, needle_v, comp, hay_indexer)); |
| 98 | + bool out = (pos == hay_nelems ? false : hay_tp[pos] == needle_v); |
| 99 | + out_tp[out_indexer(i)] = (invert) ? !out : out; |
| 100 | + } |
| 101 | +}; |
| 102 | + |
| 103 | +typedef sycl::event (*isin_contig_impl_fp_ptr_t)( |
| 104 | + sycl::queue &, |
| 105 | + const bool, |
| 106 | + const std::size_t, |
| 107 | + const std::size_t, |
| 108 | + const char *, |
| 109 | + const ssize_t, |
| 110 | + const char *, |
| 111 | + const ssize_t, |
| 112 | + char *, |
| 113 | + const ssize_t, |
| 114 | + const std::vector<sycl::event> &); |
| 115 | + |
| 116 | +template <typename T> class isin_contig_impl_krn; |
| 117 | + |
| 118 | +template <typename T, typename Compare> |
| 119 | +sycl::event isin_contig_impl(sycl::queue &exec_q, |
| 120 | + const bool invert, |
| 121 | + const std::size_t hay_nelems, |
| 122 | + const std::size_t needles_nelems, |
| 123 | + const char *hay_cp, |
| 124 | + const ssize_t hay_offset, |
| 125 | + const char *needles_cp, |
| 126 | + const ssize_t needles_offset, |
| 127 | + char *out_cp, |
| 128 | + const ssize_t out_offset, |
| 129 | + const std::vector<sycl::event> &depends) |
| 130 | +{ |
| 131 | + const T *hay_tp = reinterpret_cast<const T *>(hay_cp) + hay_offset; |
| 132 | + const T *needles_tp = |
| 133 | + reinterpret_cast<const T *>(needles_cp) + needles_offset; |
| 134 | + |
| 135 | + bool *out_tp = reinterpret_cast<bool *>(out_cp) + out_offset; |
| 136 | + |
| 137 | + sycl::event comp_ev = exec_q.submit([&](sycl::handler &cgh) { |
| 138 | + cgh.depends_on(depends); |
| 139 | + |
| 140 | + using KernelName = class isin_contig_impl_krn<T>; |
| 141 | + |
| 142 | + sycl::range<1> gRange(needles_nelems); |
| 143 | + |
| 144 | + using TrivialIndexerT = dpctl::tensor::offset_utils::NoOpIndexer; |
| 145 | + |
| 146 | + constexpr TrivialIndexerT hay_indexer{}; |
| 147 | + constexpr TrivialIndexerT needles_indexer{}; |
| 148 | + constexpr TrivialIndexerT out_indexer{}; |
| 149 | + |
| 150 | + const auto fnctr = |
| 151 | + IsinFunctor<T, TrivialIndexerT, TrivialIndexerT, TrivialIndexerT, |
| 152 | + Compare>(invert, hay_tp, needles_tp, out_tp, hay_nelems, |
| 153 | + hay_indexer, needles_indexer, out_indexer); |
| 154 | + |
| 155 | + cgh.parallel_for<KernelName>(gRange, fnctr); |
| 156 | + }); |
| 157 | + |
| 158 | + return comp_ev; |
| 159 | +} |
| 160 | + |
| 161 | +typedef sycl::event (*isin_strided_impl_fp_ptr_t)( |
| 162 | + sycl::queue &, |
| 163 | + const bool, |
| 164 | + const std::size_t, |
| 165 | + const std::size_t, |
| 166 | + const char *, |
| 167 | + const ssize_t, |
| 168 | + const ssize_t, |
| 169 | + const char *, |
| 170 | + const ssize_t, |
| 171 | + char *, |
| 172 | + const ssize_t, |
| 173 | + int, |
| 174 | + const ssize_t *, |
| 175 | + const std::vector<sycl::event> &); |
| 176 | + |
| 177 | +template <typename T> class isin_strided_impl_krn; |
| 178 | + |
| 179 | +template <typename T, typename Compare> |
| 180 | +sycl::event isin_strided_impl( |
| 181 | + sycl::queue &exec_q, |
| 182 | + const bool invert, |
| 183 | + const std::size_t hay_nelems, |
| 184 | + const std::size_t needles_nelems, |
| 185 | + const char *hay_cp, |
| 186 | + const ssize_t hay_offset, |
| 187 | + // hay is 1D, so hay_nelems, hay_offset, hay_stride describe strided array |
| 188 | + const ssize_t hay_stride, |
| 189 | + const char *needles_cp, |
| 190 | + const ssize_t needles_offset, |
| 191 | + char *out_cp, |
| 192 | + const ssize_t out_offset, |
| 193 | + const int needles_nd, |
| 194 | + // packed_shape_strides is [needles_shape, needles_strides, |
| 195 | + // out_strides] has length of 3*needles_nd |
| 196 | + const ssize_t *packed_shape_strides, |
| 197 | + const std::vector<sycl::event> &depends) |
| 198 | +{ |
| 199 | + const T *hay_tp = reinterpret_cast<const T *>(hay_cp); |
| 200 | + const T *needles_tp = reinterpret_cast<const T *>(needles_cp); |
| 201 | + |
| 202 | + bool *out_tp = reinterpret_cast<bool *>(out_cp); |
| 203 | + |
| 204 | + sycl::event comp_ev = exec_q.submit([&](sycl::handler &cgh) { |
| 205 | + cgh.depends_on(depends); |
| 206 | + |
| 207 | + sycl::range<1> gRange(needles_nelems); |
| 208 | + |
| 209 | + using HayIndexerT = dpctl::tensor::offset_utils::Strided1DIndexer; |
| 210 | + const HayIndexerT hay_indexer( |
| 211 | + /* offset */ hay_offset, |
| 212 | + /* size */ hay_nelems, |
| 213 | + /* step */ hay_stride); |
| 214 | + |
| 215 | + using NeedlesIndexerT = dpctl::tensor::offset_utils::StridedIndexer; |
| 216 | + const ssize_t *needles_shape_strides = packed_shape_strides; |
| 217 | + const NeedlesIndexerT needles_indexer(needles_nd, needles_offset, |
| 218 | + needles_shape_strides); |
| 219 | + using OutIndexerT = dpctl::tensor::offset_utils::UnpackedStridedIndexer; |
| 220 | + |
| 221 | + const ssize_t *out_shape = packed_shape_strides; |
| 222 | + const ssize_t *out_strides = packed_shape_strides + 2 * needles_nd; |
| 223 | + const OutIndexerT out_indexer(needles_nd, out_offset, out_shape, |
| 224 | + out_strides); |
| 225 | + |
| 226 | + const auto fnctr = |
| 227 | + IsinFunctor<T, HayIndexerT, NeedlesIndexerT, OutIndexerT, Compare>( |
| 228 | + invert, hay_tp, needles_tp, out_tp, hay_nelems, hay_indexer, |
| 229 | + needles_indexer, out_indexer); |
| 230 | + using KernelName = class isin_strided_impl_krn<T>; |
| 231 | + |
| 232 | + cgh.parallel_for<KernelName>(gRange, fnctr); |
| 233 | + }); |
| 234 | + |
| 235 | + return comp_ev; |
| 236 | +} |
| 237 | + |
| 238 | +} // namespace kernels |
| 239 | +} // namespace tensor |
| 240 | +} // namespace dpctl |
0 commit comments