|
| 1 | +//=== cbrt.hpp - Unary function CBRT ------ *-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 CBRT(x) |
| 23 | +/// function that compute a square root. |
| 24 | +//===---------------------------------------------------------------------===// |
| 25 | + |
| 26 | +#pragma once |
| 27 | +#include <CL/sycl.hpp> |
| 28 | +#include <cmath> |
| 29 | +#include <cstddef> |
| 30 | +#include <cstdint> |
| 31 | +#include <type_traits> |
| 32 | + |
| 33 | +#include "kernels/elementwise_functions/common.hpp" |
| 34 | + |
| 35 | +#include "utils/offset_utils.hpp" |
| 36 | +#include "utils/type_dispatch.hpp" |
| 37 | +#include "utils/type_utils.hpp" |
| 38 | +#include <pybind11/pybind11.h> |
| 39 | + |
| 40 | +namespace dpctl |
| 41 | +{ |
| 42 | +namespace tensor |
| 43 | +{ |
| 44 | +namespace kernels |
| 45 | +{ |
| 46 | +namespace cbrt |
| 47 | +{ |
| 48 | + |
| 49 | +namespace py = pybind11; |
| 50 | +namespace td_ns = dpctl::tensor::type_dispatch; |
| 51 | + |
| 52 | +template <typename argT, typename resT> struct CbrtFunctor |
| 53 | +{ |
| 54 | + |
| 55 | + // is function constant for given argT |
| 56 | + using is_constant = typename std::false_type; |
| 57 | + // constant value, if constant |
| 58 | + // constexpr resT constant_value = resT{}; |
| 59 | + // is function defined for sycl::vec |
| 60 | + using supports_vec = typename std::false_type; |
| 61 | + // do both argTy and resTy support sugroup store/load operation |
| 62 | + using supports_sg_loadstore = typename std::true_type; |
| 63 | + |
| 64 | + resT operator()(const argT &in) const |
| 65 | + { |
| 66 | + return sycl::cbrt(in); |
| 67 | + } |
| 68 | +}; |
| 69 | + |
| 70 | +template <typename argTy, |
| 71 | + typename resTy = argTy, |
| 72 | + unsigned int vec_sz = 4, |
| 73 | + unsigned int n_vecs = 2> |
| 74 | +using CbrtContigFunctor = elementwise_common:: |
| 75 | + UnaryContigFunctor<argTy, resTy, CbrtFunctor<argTy, resTy>, vec_sz, n_vecs>; |
| 76 | + |
| 77 | +template <typename argTy, typename resTy, typename IndexerT> |
| 78 | +using CbrtStridedFunctor = elementwise_common:: |
| 79 | + UnaryStridedFunctor<argTy, resTy, IndexerT, CbrtFunctor<argTy, resTy>>; |
| 80 | + |
| 81 | +template <typename T> struct CbrtOutputType |
| 82 | +{ |
| 83 | + using value_type = typename std::disjunction< // disjunction is C++17 |
| 84 | + // feature, supported by DPC++ |
| 85 | + td_ns::TypeMapResultEntry<T, sycl::half, sycl::half>, |
| 86 | + td_ns::TypeMapResultEntry<T, float, float>, |
| 87 | + td_ns::TypeMapResultEntry<T, double, double>, |
| 88 | + td_ns::DefaultResultEntry<void>>::result_type; |
| 89 | +}; |
| 90 | + |
| 91 | +template <typename T1, typename T2, unsigned int vec_sz, unsigned int n_vecs> |
| 92 | +class cbrt_contig_kernel; |
| 93 | + |
| 94 | +template <typename argTy> |
| 95 | +sycl::event cbrt_contig_impl(sycl::queue &exec_q, |
| 96 | + size_t nelems, |
| 97 | + const char *arg_p, |
| 98 | + char *res_p, |
| 99 | + const std::vector<sycl::event> &depends = {}) |
| 100 | +{ |
| 101 | + return elementwise_common::unary_contig_impl< |
| 102 | + argTy, CbrtOutputType, CbrtContigFunctor, cbrt_contig_kernel>( |
| 103 | + exec_q, nelems, arg_p, res_p, depends); |
| 104 | +} |
| 105 | + |
| 106 | +template <typename fnT, typename T> struct CbrtContigFactory |
| 107 | +{ |
| 108 | + fnT get() |
| 109 | + { |
| 110 | + if constexpr (std::is_same_v<typename CbrtOutputType<T>::value_type, |
| 111 | + void>) { |
| 112 | + fnT fn = nullptr; |
| 113 | + return fn; |
| 114 | + } |
| 115 | + else { |
| 116 | + fnT fn = cbrt_contig_impl<T>; |
| 117 | + return fn; |
| 118 | + } |
| 119 | + } |
| 120 | +}; |
| 121 | + |
| 122 | +template <typename fnT, typename T> struct CbrtTypeMapFactory |
| 123 | +{ |
| 124 | + /*! @brief get typeid for output type of std::cbrt(T x) */ |
| 125 | + std::enable_if_t<std::is_same<fnT, int>::value, int> get() |
| 126 | + { |
| 127 | + using rT = typename CbrtOutputType<T>::value_type; |
| 128 | + return td_ns::GetTypeid<rT>{}.get(); |
| 129 | + } |
| 130 | +}; |
| 131 | + |
| 132 | +template <typename T1, typename T2, typename T3> class cbrt_strided_kernel; |
| 133 | + |
| 134 | +template <typename argTy> |
| 135 | +sycl::event |
| 136 | +cbrt_strided_impl(sycl::queue &exec_q, |
| 137 | + size_t nelems, |
| 138 | + int nd, |
| 139 | + const py::ssize_t *shape_and_strides, |
| 140 | + const char *arg_p, |
| 141 | + py::ssize_t arg_offset, |
| 142 | + char *res_p, |
| 143 | + py::ssize_t res_offset, |
| 144 | + const std::vector<sycl::event> &depends, |
| 145 | + const std::vector<sycl::event> &additional_depends) |
| 146 | +{ |
| 147 | + return elementwise_common::unary_strided_impl< |
| 148 | + argTy, CbrtOutputType, CbrtStridedFunctor, cbrt_strided_kernel>( |
| 149 | + exec_q, nelems, nd, shape_and_strides, arg_p, arg_offset, res_p, |
| 150 | + res_offset, depends, additional_depends); |
| 151 | +} |
| 152 | + |
| 153 | +template <typename fnT, typename T> struct CbrtStridedFactory |
| 154 | +{ |
| 155 | + fnT get() |
| 156 | + { |
| 157 | + if constexpr (std::is_same_v<typename CbrtOutputType<T>::value_type, |
| 158 | + void>) { |
| 159 | + fnT fn = nullptr; |
| 160 | + return fn; |
| 161 | + } |
| 162 | + else { |
| 163 | + fnT fn = cbrt_strided_impl<T>; |
| 164 | + return fn; |
| 165 | + } |
| 166 | + } |
| 167 | +}; |
| 168 | + |
| 169 | +} // namespace cbrt |
| 170 | +} // namespace kernels |
| 171 | +} // namespace tensor |
| 172 | +} // namespace dpctl |
0 commit comments