|
| 1 | +//=== copysign.hpp - Binary function COPYSIGN ------ *-C++-*--/===// |
| 2 | +// |
| 3 | +// Data Parallel Control (dpctl) |
| 4 | +// |
| 5 | +// Copyright 2020-2023 Intel Corporation |
| 6 | +// |
| 7 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +// you may not use this file except in compliance with the License. |
| 9 | +// You may obtain a copy of the License at |
| 10 | +// |
| 11 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +// |
| 13 | +// Unless required by applicable law or agreed to in writing, software |
| 14 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +// See the License for the specific language governing permissions and |
| 17 | +// limitations under the License. |
| 18 | +// |
| 19 | +//===---------------------------------------------------------------------===// |
| 20 | +/// |
| 21 | +/// \file |
| 22 | +/// This file defines kernels for elementwise evaluation of COPYSIGN(x1, x2) |
| 23 | +/// function. |
| 24 | +//===---------------------------------------------------------------------===// |
| 25 | + |
| 26 | +#pragma once |
| 27 | +#include <CL/sycl.hpp> |
| 28 | +#include <cstddef> |
| 29 | +#include <cstdint> |
| 30 | +#include <type_traits> |
| 31 | + |
| 32 | +#include "utils/offset_utils.hpp" |
| 33 | +#include "utils/type_dispatch.hpp" |
| 34 | +#include "utils/type_utils.hpp" |
| 35 | + |
| 36 | +#include "kernels/elementwise_functions/common.hpp" |
| 37 | +#include <pybind11/pybind11.h> |
| 38 | + |
| 39 | +namespace dpctl |
| 40 | +{ |
| 41 | +namespace tensor |
| 42 | +{ |
| 43 | +namespace kernels |
| 44 | +{ |
| 45 | +namespace copysign |
| 46 | +{ |
| 47 | + |
| 48 | +namespace py = pybind11; |
| 49 | +namespace td_ns = dpctl::tensor::type_dispatch; |
| 50 | +namespace tu_ns = dpctl::tensor::type_utils; |
| 51 | + |
| 52 | +template <typename argT1, typename argT2, typename resT> struct CopysignFunctor |
| 53 | +{ |
| 54 | + |
| 55 | + using supports_sg_loadstore = std::true_type; |
| 56 | + using supports_vec = std::true_type; |
| 57 | + |
| 58 | + resT operator()(const argT1 &in1, const argT2 &in2) const |
| 59 | + { |
| 60 | + return sycl::copysign(in1, in2); |
| 61 | + } |
| 62 | + |
| 63 | + template <int vec_sz> |
| 64 | + sycl::vec<resT, vec_sz> |
| 65 | + operator()(const sycl::vec<argT1, vec_sz> &in1, |
| 66 | + const sycl::vec<argT2, vec_sz> &in2) const |
| 67 | + { |
| 68 | + auto tmp = sycl::copysign(in1, in2); |
| 69 | + if constexpr (std::is_same_v<resT, |
| 70 | + typename decltype(tmp)::element_type>) { |
| 71 | + return tmp; |
| 72 | + } |
| 73 | + else { |
| 74 | + using dpctl::tensor::type_utils::vec_cast; |
| 75 | + |
| 76 | + return vec_cast<resT, typename decltype(tmp)::element_type, vec_sz>( |
| 77 | + tmp); |
| 78 | + } |
| 79 | + } |
| 80 | +}; |
| 81 | + |
| 82 | +template <typename argT1, |
| 83 | + typename argT2, |
| 84 | + typename resT, |
| 85 | + unsigned int vec_sz = 4, |
| 86 | + unsigned int n_vecs = 2> |
| 87 | +using CopysignContigFunctor = |
| 88 | + elementwise_common::BinaryContigFunctor<argT1, |
| 89 | + argT2, |
| 90 | + resT, |
| 91 | + CopysignFunctor<argT1, argT2, resT>, |
| 92 | + vec_sz, |
| 93 | + n_vecs>; |
| 94 | + |
| 95 | +template <typename argT1, typename argT2, typename resT, typename IndexerT> |
| 96 | +using CopysignStridedFunctor = elementwise_common::BinaryStridedFunctor< |
| 97 | + argT1, |
| 98 | + argT2, |
| 99 | + resT, |
| 100 | + IndexerT, |
| 101 | + CopysignFunctor<argT1, argT2, resT>>; |
| 102 | + |
| 103 | +template <typename T1, typename T2> struct CopysignOutputType |
| 104 | +{ |
| 105 | + using value_type = typename std::disjunction< // disjunction is C++17 |
| 106 | + // feature, supported by DPC++ |
| 107 | + td_ns::BinaryTypeMapResultEntry<T1, |
| 108 | + sycl::half, |
| 109 | + T2, |
| 110 | + sycl::half, |
| 111 | + sycl::half>, |
| 112 | + td_ns::BinaryTypeMapResultEntry<T1, float, T2, float, float>, |
| 113 | + td_ns::BinaryTypeMapResultEntry<T1, double, T2, double, double>, |
| 114 | + td_ns::DefaultResultEntry<void>>::result_type; |
| 115 | +}; |
| 116 | + |
| 117 | +template <typename argT1, |
| 118 | + typename argT2, |
| 119 | + typename resT, |
| 120 | + unsigned int vec_sz, |
| 121 | + unsigned int n_vecs> |
| 122 | +class copysign_contig_kernel; |
| 123 | + |
| 124 | +template <typename argTy1, typename argTy2> |
| 125 | +sycl::event copysign_contig_impl(sycl::queue &exec_q, |
| 126 | + size_t nelems, |
| 127 | + const char *arg1_p, |
| 128 | + py::ssize_t arg1_offset, |
| 129 | + const char *arg2_p, |
| 130 | + py::ssize_t arg2_offset, |
| 131 | + char *res_p, |
| 132 | + py::ssize_t res_offset, |
| 133 | + const std::vector<sycl::event> &depends = {}) |
| 134 | +{ |
| 135 | + return elementwise_common::binary_contig_impl< |
| 136 | + argTy1, argTy2, CopysignOutputType, CopysignContigFunctor, |
| 137 | + copysign_contig_kernel>(exec_q, nelems, arg1_p, arg1_offset, arg2_p, |
| 138 | + arg2_offset, res_p, res_offset, depends); |
| 139 | +} |
| 140 | + |
| 141 | +template <typename fnT, typename T1, typename T2> struct CopysignContigFactory |
| 142 | +{ |
| 143 | + fnT get() |
| 144 | + { |
| 145 | + if constexpr (std::is_same_v< |
| 146 | + typename CopysignOutputType<T1, T2>::value_type, |
| 147 | + void>) |
| 148 | + { |
| 149 | + fnT fn = nullptr; |
| 150 | + return fn; |
| 151 | + } |
| 152 | + else { |
| 153 | + fnT fn = copysign_contig_impl<T1, T2>; |
| 154 | + return fn; |
| 155 | + } |
| 156 | + } |
| 157 | +}; |
| 158 | + |
| 159 | +template <typename fnT, typename T1, typename T2> struct CopysignTypeMapFactory |
| 160 | +{ |
| 161 | + /*! @brief get typeid for output type of divide(T1 x, T2 y) */ |
| 162 | + std::enable_if_t<std::is_same<fnT, int>::value, int> get() |
| 163 | + { |
| 164 | + using rT = typename CopysignOutputType<T1, T2>::value_type; |
| 165 | + return td_ns::GetTypeid<rT>{}.get(); |
| 166 | + } |
| 167 | +}; |
| 168 | + |
| 169 | +template <typename T1, typename T2, typename resT, typename IndexerT> |
| 170 | +class copysign_strided_kernel; |
| 171 | + |
| 172 | +template <typename argTy1, typename argTy2> |
| 173 | +sycl::event |
| 174 | +copysign_strided_impl(sycl::queue &exec_q, |
| 175 | + size_t nelems, |
| 176 | + int nd, |
| 177 | + const py::ssize_t *shape_and_strides, |
| 178 | + const char *arg1_p, |
| 179 | + py::ssize_t arg1_offset, |
| 180 | + const char *arg2_p, |
| 181 | + py::ssize_t arg2_offset, |
| 182 | + char *res_p, |
| 183 | + py::ssize_t res_offset, |
| 184 | + const std::vector<sycl::event> &depends, |
| 185 | + const std::vector<sycl::event> &additional_depends) |
| 186 | +{ |
| 187 | + return elementwise_common::binary_strided_impl< |
| 188 | + argTy1, argTy2, CopysignOutputType, CopysignStridedFunctor, |
| 189 | + copysign_strided_kernel>(exec_q, nelems, nd, shape_and_strides, arg1_p, |
| 190 | + arg1_offset, arg2_p, arg2_offset, res_p, |
| 191 | + res_offset, depends, additional_depends); |
| 192 | +} |
| 193 | + |
| 194 | +template <typename fnT, typename T1, typename T2> struct CopysignStridedFactory |
| 195 | +{ |
| 196 | + fnT get() |
| 197 | + { |
| 198 | + if constexpr (std::is_same_v< |
| 199 | + typename CopysignOutputType<T1, T2>::value_type, |
| 200 | + void>) |
| 201 | + { |
| 202 | + fnT fn = nullptr; |
| 203 | + return fn; |
| 204 | + } |
| 205 | + else { |
| 206 | + fnT fn = copysign_strided_impl<T1, T2>; |
| 207 | + return fn; |
| 208 | + } |
| 209 | + } |
| 210 | +}; |
| 211 | + |
| 212 | +} // namespace copysign |
| 213 | +} // namespace kernels |
| 214 | +} // namespace tensor |
| 215 | +} // namespace dpctl |
0 commit comments