|
| 1 | +//=== HYPOT.hpp - Binary function HYPOT ------ *-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 HYPOT(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 hypot |
| 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 HypotFunctor |
| 53 | +{ |
| 54 | + |
| 55 | + using supports_sg_loadstore = std::negation< |
| 56 | + std::disjunction<tu_ns::is_complex<argT1>, tu_ns::is_complex<argT2>>>; |
| 57 | + using supports_vec = std::negation< |
| 58 | + std::disjunction<tu_ns::is_complex<argT1>, tu_ns::is_complex<argT2>>>; |
| 59 | + |
| 60 | + resT operator()(const argT1 &in1, const argT2 &in2) |
| 61 | + { |
| 62 | + return std::hypot(in1, in2); |
| 63 | + } |
| 64 | + |
| 65 | + template <int vec_sz> |
| 66 | + sycl::vec<resT, vec_sz> operator()(const sycl::vec<argT1, vec_sz> &in1, |
| 67 | + const sycl::vec<argT2, vec_sz> &in2) |
| 68 | + { |
| 69 | + auto res = sycl::hypot(in1, in2); |
| 70 | + if constexpr (std::is_same_v<resT, |
| 71 | + typename decltype(res)::element_type>) { |
| 72 | + return res; |
| 73 | + } |
| 74 | + else { |
| 75 | + using dpctl::tensor::type_utils::vec_cast; |
| 76 | + |
| 77 | + return vec_cast<resT, typename decltype(res)::element_type, vec_sz>( |
| 78 | + res); |
| 79 | + } |
| 80 | + } |
| 81 | +}; |
| 82 | + |
| 83 | +template <typename argT1, |
| 84 | + typename argT2, |
| 85 | + typename resT, |
| 86 | + unsigned int vec_sz = 4, |
| 87 | + unsigned int n_vecs = 2> |
| 88 | +using HypotContigFunctor = |
| 89 | + elementwise_common::BinaryContigFunctor<argT1, |
| 90 | + argT2, |
| 91 | + resT, |
| 92 | + HypotFunctor<argT1, argT2, resT>, |
| 93 | + vec_sz, |
| 94 | + n_vecs>; |
| 95 | + |
| 96 | +template <typename argT1, typename argT2, typename resT, typename IndexerT> |
| 97 | +using HypotStridedFunctor = |
| 98 | + elementwise_common::BinaryStridedFunctor<argT1, |
| 99 | + argT2, |
| 100 | + resT, |
| 101 | + IndexerT, |
| 102 | + HypotFunctor<argT1, argT2, resT>>; |
| 103 | + |
| 104 | +template <typename T1, typename T2> struct HypotOutputType |
| 105 | +{ |
| 106 | + using value_type = typename std::disjunction< // disjunction is C++17 |
| 107 | + // feature, supported by DPC++ |
| 108 | + td_ns::BinaryTypeMapResultEntry<T1, |
| 109 | + sycl::half, |
| 110 | + T2, |
| 111 | + sycl::half, |
| 112 | + sycl::half>, |
| 113 | + td_ns::BinaryTypeMapResultEntry<T1, float, T2, float, float>, |
| 114 | + td_ns::BinaryTypeMapResultEntry<T1, double, T2, double, double>, |
| 115 | + td_ns::DefaultResultEntry<void>>::result_type; |
| 116 | +}; |
| 117 | + |
| 118 | +template <typename argT1, |
| 119 | + typename argT2, |
| 120 | + typename resT, |
| 121 | + unsigned int vec_sz, |
| 122 | + unsigned int n_vecs> |
| 123 | +class hypot_contig_kernel; |
| 124 | + |
| 125 | +template <typename argTy1, typename argTy2> |
| 126 | +sycl::event hypot_contig_impl(sycl::queue exec_q, |
| 127 | + size_t nelems, |
| 128 | + const char *arg1_p, |
| 129 | + py::ssize_t arg1_offset, |
| 130 | + const char *arg2_p, |
| 131 | + py::ssize_t arg2_offset, |
| 132 | + char *res_p, |
| 133 | + py::ssize_t res_offset, |
| 134 | + const std::vector<sycl::event> &depends = {}) |
| 135 | +{ |
| 136 | + return elementwise_common::binary_contig_impl< |
| 137 | + argTy1, argTy2, HypotOutputType, HypotContigFunctor, |
| 138 | + hypot_contig_kernel>(exec_q, nelems, arg1_p, arg1_offset, arg2_p, |
| 139 | + arg2_offset, res_p, res_offset, depends); |
| 140 | +} |
| 141 | + |
| 142 | +template <typename fnT, typename T1, typename T2> struct HypotContigFactory |
| 143 | +{ |
| 144 | + fnT get() |
| 145 | + { |
| 146 | + if constexpr (std::is_same_v< |
| 147 | + typename HypotOutputType<T1, T2>::value_type, void>) |
| 148 | + { |
| 149 | + fnT fn = nullptr; |
| 150 | + return fn; |
| 151 | + } |
| 152 | + else { |
| 153 | + fnT fn = hypot_contig_impl<T1, T2>; |
| 154 | + return fn; |
| 155 | + } |
| 156 | + } |
| 157 | +}; |
| 158 | + |
| 159 | +template <typename fnT, typename T1, typename T2> struct HypotTypeMapFactory |
| 160 | +{ |
| 161 | + /*! @brief get typeid for output type of std::hypot(T1 x, T2 y) */ |
| 162 | + std::enable_if_t<std::is_same<fnT, int>::value, int> get() |
| 163 | + { |
| 164 | + using rT = typename HypotOutputType<T1, T2>::value_type; |
| 165 | + ; |
| 166 | + return td_ns::GetTypeid<rT>{}.get(); |
| 167 | + } |
| 168 | +}; |
| 169 | + |
| 170 | +template <typename T1, typename T2, typename resT, typename IndexerT> |
| 171 | +class hypot_strided_strided_kernel; |
| 172 | + |
| 173 | +template <typename argTy1, typename argTy2> |
| 174 | +sycl::event |
| 175 | +hypot_strided_impl(sycl::queue exec_q, |
| 176 | + size_t nelems, |
| 177 | + int nd, |
| 178 | + const py::ssize_t *shape_and_strides, |
| 179 | + const char *arg1_p, |
| 180 | + py::ssize_t arg1_offset, |
| 181 | + const char *arg2_p, |
| 182 | + py::ssize_t arg2_offset, |
| 183 | + char *res_p, |
| 184 | + py::ssize_t res_offset, |
| 185 | + const std::vector<sycl::event> &depends, |
| 186 | + const std::vector<sycl::event> &additional_depends) |
| 187 | +{ |
| 188 | + return elementwise_common::binary_strided_impl< |
| 189 | + argTy1, argTy2, HypotOutputType, HypotStridedFunctor, |
| 190 | + hypot_strided_strided_kernel>( |
| 191 | + exec_q, nelems, nd, shape_and_strides, arg1_p, arg1_offset, arg2_p, |
| 192 | + arg2_offset, res_p, res_offset, depends, additional_depends); |
| 193 | +} |
| 194 | + |
| 195 | +template <typename fnT, typename T1, typename T2> struct HypotStridedFactory |
| 196 | +{ |
| 197 | + fnT get() |
| 198 | + { |
| 199 | + if constexpr (std::is_same_v< |
| 200 | + typename HypotOutputType<T1, T2>::value_type, void>) |
| 201 | + { |
| 202 | + fnT fn = nullptr; |
| 203 | + return fn; |
| 204 | + } |
| 205 | + else { |
| 206 | + fnT fn = hypot_strided_impl<T1, T2>; |
| 207 | + return fn; |
| 208 | + } |
| 209 | + } |
| 210 | +}; |
| 211 | + |
| 212 | +} // namespace hypot |
| 213 | +} // namespace kernels |
| 214 | +} // namespace tensor |
| 215 | +} // namespace dpctl |
0 commit comments