|
| 1 | +//***************************************************************************** |
| 2 | +// Copyright (c) 2024, 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 | +#pragma once |
| 30 | + |
| 31 | +#include <sycl/sycl.hpp> |
| 32 | + |
| 33 | +#include "kernels/dpctl_tensor_types.hpp" |
| 34 | + |
| 35 | +namespace dpnp::kernels::choose |
| 36 | +{ |
| 37 | +using dpctl::tensor::ssize_t; |
| 38 | + |
| 39 | +template <typename ProjectorT, |
| 40 | + typename IndOutIndexerT, |
| 41 | + typename ChoicesIndexerT, |
| 42 | + typename IndT, |
| 43 | + typename T> |
| 44 | +class ChooseFunctor |
| 45 | +{ |
| 46 | +private: |
| 47 | + const IndT *ind = nullptr; |
| 48 | + T *dst = nullptr; |
| 49 | + char **chcs = nullptr; |
| 50 | + ssize_t n_chcs; |
| 51 | + const IndOutIndexerT ind_out_indexer; |
| 52 | + const ChoicesIndexerT chcs_indexer; |
| 53 | + |
| 54 | +public: |
| 55 | + ChooseFunctor(const IndT *ind_, |
| 56 | + T *dst_, |
| 57 | + char **chcs_, |
| 58 | + ssize_t n_chcs_, |
| 59 | + const IndOutIndexerT &ind_out_indexer_, |
| 60 | + const ChoicesIndexerT &chcs_indexer_) |
| 61 | + : ind(ind_), dst(dst_), chcs(chcs_), n_chcs(n_chcs_), |
| 62 | + ind_out_indexer(ind_out_indexer_), chcs_indexer(chcs_indexer_) |
| 63 | + { |
| 64 | + } |
| 65 | + |
| 66 | + void operator()(sycl::id<1> id) const |
| 67 | + { |
| 68 | + const ProjectorT proj{}; |
| 69 | + |
| 70 | + ssize_t i = id[0]; |
| 71 | + |
| 72 | + auto ind_dst_offsets = ind_out_indexer(i); |
| 73 | + ssize_t ind_offset = ind_dst_offsets.get_first_offset(); |
| 74 | + ssize_t dst_offset = ind_dst_offsets.get_second_offset(); |
| 75 | + |
| 76 | + IndT chc_idx = ind[ind_offset]; |
| 77 | + // proj produces an index in the range of n_chcs |
| 78 | + ssize_t projected_idx = proj(n_chcs, chc_idx); |
| 79 | + |
| 80 | + ssize_t chc_offset = chcs_indexer(i, projected_idx); |
| 81 | + |
| 82 | + T *chc = reinterpret_cast<T *>(chcs[projected_idx]); |
| 83 | + |
| 84 | + dst[dst_offset] = chc[chc_offset]; |
| 85 | + } |
| 86 | +}; |
| 87 | +} // namespace dpnp::kernels::choose |
0 commit comments