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